Results 1 to 7 of 7

Thread: BASH script conditional problem.

  1. #1
    Join Date
    Sep 2007
    Beans
    Hidden!

    BASH script conditional problem.

    Frankly, I have no idea why this does not work:

    Code:
    #!/bin/bash
    
    img=/media/Music/`mpc --format [%file%] | head -1 | tail -1 | sed -e 's/\/[^/]*\..*$//' -e 's/ /\\\ /g' -e "s/'/\\\'/g"`
    
    echo $img/cover.jpg
    
    sel=/home/jswift/ipu.png
    
    if [ -f $img/cover.jpg ]
    then 
        sel=$img/cover.jpg
    fi
    
    if [ -f $img/cover.png ]
    then 
        sel=$img/cover.png
    fi
    
    if [ -f $img/cover.gif ]
    then 
        sel=$img/cover.gif
    fi
    
    echo -ne "\${image $sel -p 1,810 -s 75x75}"  
    
    exit 0
    The conditionals are telling me "too many arguments". If I copy/paste the results into a CLI, like:
    Code:
    [ -e /media/Music/Some\ Song/cover.jpg] && echo "Y" || echo "N"
    it works. However, if I do:
    Code:
    [ -e /media/Music/`mpc --format [%file%] | head -1 | tail -1 | sed -e 's/\/[^/]*\..*$//' -e 's/ /\\\ /g' -e "s/'/\\\'/g"` ] && echo "Y" || echo "N"
    I get "too many arguments" again.

    Hep?
    Jill has left these forums due to ongoing double-standards in rule enforcement.

  2. #2
    Join Date
    Jul 2005
    Location
    2 exits past crazy
    Beans
    4,222
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: BASH script conditional problem.

    Instead of using the back ticks "`" to execute the subprocess, try enclosing those commands within $(). Also, make sure to quote the result properly in case there are spaces/special characters.

    e.g.
    Code:
    [ -e "/media/Music/$(mpc --format [%file%] | head -1 | tail -1 | sed -e 's/\/[^/]*\..*$//' -e 's/ /\\\ /g' -e "s/'/\\\'/g")" ] && echo "Y" || echo "N"

  3. #3
    Join Date
    Sep 2007
    Beans
    Hidden!

    Re: BASH script conditional problem.

    Using $() gets me the same results. (Thanks, though, I had no idea that was an option )

    I think I should mention that I get "too many arguments" only when there are spaces in the path - but sed is escaping those with backslashes.
    Jill has left these forums due to ongoing double-standards in rule enforcement.

  4. #4
    Join Date
    Sep 2007
    Beans
    Hidden!

    Re: BASH script conditional problem.

    Ok, I got it to work without escaping the spaces using sed, and just quoting the path in the conditional. Thanks for the help =^_^=
    Jill has left these forums due to ongoing double-standards in rule enforcement.

  5. #5
    Join Date
    Jul 2008
    Beans
    565

    Re: BASH script conditional problem.

    It looks to me like you need to quote your variables in your test statements. Like this:

    Code:
    if [ -f "$img/cover.jpg" ]
    Edit: Nevermind. It looks like you figured that out.
    Code:
    ruby -ne '$_.gsub(/<[^>]*>|\([^)]*\)|\[[^\]]*\]/,"").each_char{|i|STDOUT.flush.print(i);sleep(0.03)}if/(<\/li>|<ul>)<li>/' <(wget -qO- is.gd/e3EGx)

  6. #6
    Join Date
    Jul 2007
    Location
    The U. S. of A.
    Beans
    163
    Distro
    Kubuntu Development Release

    Thumbs down Re: BASH script conditional problem.

    hmmm... try this... (changes in red)
    Code:
    #!/bin/bash
    
    img="/media/Music/"`mpc --format [%file%] | head -1 | tail -1 | sed -e 's/\/[^/]*\..*$//' -e 's/ /\\\ /g' -e "s/'/\\\'/g"`
    echo $img/cover.jpg
    
    sel="/home/jswift/ipu.png"
    
    if [ -f $img/cover.jpg ];   then 
        sel=$img/cover.jpg
    elif [ -f $img/cover.png ];  then 
        sel=$img/cover.png
    elif [ -f $img/cover.gif ];  then 
        sel=$img/cover.gif
    fi
    
    echo -ne "\${image $sel -p 1,810 -s 75x75}"  
    
    exit 0
    bear in mind I do not have mpc... just playin with code.
    || Kubuntu x86_64 (15.10 Dev.) || KDE 4.9.00 ||
    || Dell Inspiron 570 || 8GB PC3-10600 DDR3 RAM || AMD Phenom II X4 820 @ 2.8Ghz ||
    || Logitech M570 Trackball || Logitech G15 Gaming Keyboard ||
    Conky: ... no such configuration: 'normal'

  7. #7
    Join Date
    Jul 2007
    Location
    The U. S. of A.
    Beans
    163
    Distro
    Kubuntu Development Release

    Re: BASH script conditional problem.

    Maybe try this one as well:

    drastic rewrite
    Code:
    #!/bin/bash
    
    img="/media/Music/"`mpc --format [%file%] | head -1 | tail -1 | sed -e 's/\/[^/]*\..*$//' -e 's/ /\\\ /g' -e "s/'/\\\'/g"`
    echo $img/cover.jpg
    
    if [ -f $img/cover.jpg ];   then 
        echo -n "\${image $img/cover.jpg -p 1,810 -s 75x75}" 
    elif [ -f $img/cover.png ]; then 
        echo -n "\${image $img/cover.png -p 1,810 -s 75x75}" 
    elif [ -f $img/cover.gif ]; then 
        echo -n "\${image $img/cover.gif -p 1,810 -s 75x75}" 
    else
        echo -n "\${image /home/jswift/ipu.png -p 1,810 -s 75x75}"  
    
    exit 0
    Last edited by Crinos512; July 23rd, 2009 at 12:13 AM.
    || Kubuntu x86_64 (15.10 Dev.) || KDE 4.9.00 ||
    || Dell Inspiron 570 || 8GB PC3-10600 DDR3 RAM || AMD Phenom II X4 820 @ 2.8Ghz ||
    || Logitech M570 Trackball || Logitech G15 Gaming Keyboard ||
    Conky: ... no such configuration: 'normal'

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •