LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need another pair of eyes (BASH script throwing a "s-e-n-u-t" error) (https://www.linuxquestions.org/questions/linux-newbie-8/need-another-pair-of-eyes-bash-script-throwing-a-s-e-n-u-t-error-913937/)

SilversleevesX 11-16-2011 05:48 PM

Need another pair of eyes (BASH script throwing a "s-e-n-u-t" error)
 
"s-e-n-u-t"? syntax error near unexpected token.
(I wasn't sure it rated acronym status, but setting off the first letter of each word with hyphens seemed appropriate.)

Anyway, here's the script:
Code:

#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
survey=$(echo ${PWD##*/})
:>$survey-captions.txt
pic=0
function gettext () {
echo -e "What list will I be using?"
read thelist
}

gettext
d=1
while read 'line';
do
        file=${line##*/}
        s=$(exiv2 -g Iptc.Application2.Caption -Pv $line)
        t=$(exiv2 -g Xmp.dc.description -Pv $line)
        v=$(exiv2 -g Exif.Image.ImageDescription -Pv $line)
        echo -ne "Extracting Caption data from JPEG file $file.\nItem #$d.\n"
        if [ -n "$s" ] && [ -z "$t" ] && [ -z "$v" ]; then
                z=$s
        fi
        if [ -z "$s" ] && [ -n "$t" ] && [ -z "$v" ]; then
                z=$t
        fi
        if [ -z "$s" ] && [ -z "$t" ] && [ -n "$v" ]; then
                z=$v
        fi
        if [ -n "$s" ] && [ -n "$t" ] && [ -z "$v" ]; then
                z=$t
        if [ -z "$s" ] && [ -n "$t" ] && [ -n "$v" ]; then
                z=$v
        fi
        echo -e "$line:$z">>$survey-captions.txt
        d=$[d+1]
done<$thelist
SAVEIFS=$IFS

And the particular "s-e-n-u-t" I get is
Quote:

line 37: syntax error near unexpected token `done'
line 37: `done<$thelist'
I don't see what could be keeping BASH from seeing the
Code:

while read 'line';
up in line 14. Maybe somebody else does?

And as a side question: is there any way I can cut down on the number of if/fi's I have in this script. I'm not too fluent with case loops, but those would seem to be appropriate here.

Thanks for looking at this OP.

BZT

kinneyd 11-16-2011 05:57 PM

Looks like you're missing a "fi" on line 32. :) That should fix the syntax error.

chrism01 11-16-2011 05:57 PM

1. you've skipped a fi
Code:

        if [ -n "$s" ] && [ -n "$t" ] && [ -z "$v" ]; then
                z=$t
        if [ -z "$s" ] && [ -n "$t" ] && [ -n "$v" ]; then

Here's some good bash HOWTO links; consider using case ... esac or 'elif'
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

2. If you want to empty a file
Code:

>$survey-captions.txt
is sufficient ie drop the ':'

3. a good cmd to use for debugging is
Code:

set -xv
usually next after '#!/bin/bash'

SilversleevesX 11-16-2011 06:32 PM

Many thanks to both of you...
 
I thought all my "ifs" were paired with "fi's" but evidently not.

Thanks for the links to tips on case loops. I haven't had all that much luck with 'elif', though maybe I wasn't using it quite right, either. I'll read up on both asap.


BZT

chrism01 11-16-2011 07:09 PM

Try this page http://tldp.org/LDP/Bash-Beginners-G...ect_07_02.html.
Also recommend [[ ]] http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS

grail 11-16-2011 07:30 PM

I would also question the use of quotes around the read variable. Not sure if it causes any issues but there is no value to it.

David the H. 11-17-2011 09:52 AM

Some comments on your script, starting with a few unnecessary patterns:

Code:

SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
survey=$(echo ${PWD##*/})
d=$[d+1]

1) You shouldn't generally need to save or restore IFS inside a script, unless you need to use the default value again in it. All environment settings inside the script only affect the script itself, and will be discarded on exit.

2) Bash has a special $'' quoting style (usually called ansi-c style quotes, see the QUOTING section of the man page) for inserting non-printing characters. You can use this instead of echo -e:

Code:

IFS=$'\n\b'
But why would you need to set IFS to backspace?

3) You don't need to echo a parameter expansion. Just set the new variable directly:

Code:

survey=${PWD##*/}
4) $[..] is a very old, outdated, and deprecated syntax. Do not use it. The modern version is $((..)). But there are also ways to do arithmetic operations without expansion, in particular the let keyword and bash's ((..)) arithmetic brackets. You can also increment a variable directly with var++ when in an arithmetic context.

Code:

let d++
(( d++ ))

http://mywiki.wooledge.org/ArithmeticExpression

5) Always remember to quote your variable expansions! Remember the links I gave you in your last thread?

6) You probably want to use a single if..elif...else statement, rather than a series of separate ifs.

7) Also consider using bash's new [[..]] test instead of [..]. It's safer and more powerful. You could combine all your tests into one command, for one thing.

Code:

if [[ -n "$s" && -z "$t" && -z "$v" ]]; then
                z=$s
elif [[ -z "$s" && -n "$t" && -z "$v" ]]; then
                z=$t
elif ...

http://mywiki.wooledge.org/BashFAQ/031

By the way, assuming I understand it correctly, I believe you can replace the whole thing with this:
Code:

z=${v:-${t:-$s}}
If v doesn't exist, then it uses t, if t doesn't exist, it uses s.

parameter substitution

Finally, I just posted a comment in that thread with some scripting suggestions. You may want to take a look at it.


All times are GMT -5. The time now is 04:18 PM.