`do' unexpected -> Error message


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting `do' unexpected -> Error message
# 1  
Old 05-05-2015
`do' unexpected -> Error message

Hi Folks,

I got the below given error message while i try to run the script.


Code:
!/bin/bash
echo 9.9
`for dir in \ `logins -ox | awk -F: '($8 == "PS") { print $6 }'`; do 
find ${dir}/.netrc -type f \( \
 -perm -g+r -o -perm -g+w -o -perm -g+x -o \ 
-perm -o+r -o -perm -o+w -o -perm -o+x \) \ 
-ls 2>/dev/null 
done`

error message:

Code:
9.9.sh: line 3: syntax error at line 3: `do' unexpected
9.9.sh: line 3: syntax error at line 3: `for' unmatched

----------------------------------
Am I missing something ???

Last edited by Scrutinizer; 05-05-2015 at 07:32 AM.. Reason: CODE tags, removed quote tags
# 2  
Old 05-05-2015
For starters:
1. she-bang line starts with a hash symbol #! /bin/bash
2. The entire for loop is in backticks. Whyyyyy??????
# 3  
Old 05-05-2015
Dont use backquotes for a for statement.
Instead of:
Code:
`for dir in \ `logins -ox | awk -F: '($8 == "PS") { print $6 }'`; do

Try:
Code:
for dir in logins -ox | awk -F: '($8 == "PS") { print $6 }'; do

hth
# 4  
Old 05-05-2015
Hi Sea,

Now, I got the below error message:

Code:
test.sh: line 3: syntax error at line 3: `|' unexpected


Last edited by Scrutinizer; 05-05-2015 at 07:33 AM.. Reason: code tags
# 5  
Old 05-05-2015
Right, happend during review ^^

if its bash, its suggested to use $() rather than ``.
So it'll be:
Code:
$(logins -ox | awk -F: '($8 == "PS") { print $6 }')

One can simplify to:
Code:
for ITEM in $LIST
do <stuff>
done

$LIST can be expanded to anything that be output'ed from within a $() or ``, just as you do.
Depending on the content of imagined $LIST, you would need to put quotes around: "$( code )".
Saying: Quoteing is just around the $LIST, not around the for.

hth
# 6  
Old 05-05-2015
Quote:
Originally Posted by gsiva
Am I missing something ???
As it is: yes. You have been given ample help already on how to construct the for-loop syntactically correct. IMHO this is one side of the story. My advice is to avoid for-loops for things like yours altogether and use while-loops instead:

instead of

Code:
for VAR in $(command -producing | some | list) ; do
     something $VAR
done

do

Code:
command -producing | some | list |\
while read VAR ; do
     something $VAR
done

There are two reasons for this:

first, "for" parses words, not lines. If one of the list of items the command produces contains a whitespace it will break. The "while read ..." will parse on lines and as long as on each line is only one value you are fine even if the values themselves contain whitespace.

second, there is a maximum line length and a maximum number of arguments a command can take in place. These values have been increased with the advent of 64-bit OSes but they are still there and whatever you feed into "for" is an argument to it. For instance:

Code:
 for i in 1 2 3 4 5 ; do .... done

gives 5 arguments, "1", "2", etc., to "for". Because your process substitution ("$(...)" or backticks) can produce any number of arguments without you having control over the process. It may never really hit you but why the take the risk? The while-loop will have no restriction in the number of elements it can process because it handles one item at a time.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Getting this error syntax error near unexpected token `)'

Hi Everyone, my script was running Ok, but suddenly it started giving this error. ./update_env_bi.sh: line 54: syntax error near unexpected token `)' ./update_env_bi.sh: line 54: `sed -i "s/PORT=*1/PORT=$2/" repository.xml' The line 54 has this code. sed -i "s/PORT=*1/PORT=$2/"... (2 Replies)
Discussion started by: shajay12
2 Replies

2. Shell Programming and Scripting

Syntax error `(' unexpected

I have written this in my script but while running i am getting syntax error `(' unexpected. unload to "$BACKUP_DIR/n_fac_fid-$clliname" select * from n_fac_fid where fac_accesskey in (select fac_accesskey From n_fac_ap_fid where ap_clli="$clliname"); Any help appreciated. (3 Replies)
Discussion started by: suryanmi
3 Replies

3. Shell Programming and Scripting

Syntax error: 'fi' unexpected

unzip file.zip if ] ; then echo "Success" else echo "Some failure." fi ; I tried many time to detect the unzip error, but it keep show the syntax error wherever how I change the syntac. Hope someone can help me fix the issue, thanks. Please use code tags next time for your code and... (5 Replies)
Discussion started by: duncanyy
5 Replies

4. Shell Programming and Scripting

IF section problem. syntax error: unexpected end of file error

Hello, I have another problem with my script. Please accept my apologies, but I am really nooby in sh scripts. I am writing it for first time. My script: returned=`tail -50 SapLogs.log | grep -i "Error"` echo $returned if ; then echo "There is no errors in the logs" fi And after... (10 Replies)
Discussion started by: jedzio
10 Replies

5. Shell Programming and Scripting

Getting unexpected error

Hi All, I am writing one script to automate one of the process and for that I am using below command at one line : uncompress `cat tmp` content in tmp file are : /home/vas/aqbatch/newbatch/archive/output/RIM_GUIDEPRICE_AQ_02108.DAT.Z... (5 Replies)
Discussion started by: NirajThakar
5 Replies

6. Solaris

Unexpected error

When I typed #svcs -x sma I received this following error, svcs: svcs.c:335: Unexpected libscf error: invalid argument. Exiting. I have googled around but could not find a solution. IS this a bug? I am using Solaris 5.10 Generic_137137-09 sun4v sparc... (2 Replies)
Discussion started by: sundar63
2 Replies

7. Shell Programming and Scripting

sed error : Syntax error: redirection unexpected

My script is throwing the error 'Syntax error: redirection unexpected' My line of code.. cat nsstatustest.html | sed s/<tr><td align="left">/<tr><td align="left" bgcolor="#000000"><font color="white">/ > ztmp.Ps23zp2s.2-Fpps3-wmmm0dss3 HTML tags are getting in the way but they're needed to... (3 Replies)
Discussion started by: phpfreak
3 Replies

8. UNIX for Dummies Questions & Answers

unexpected error

./sample.5: syntax error: `(' unexpected I get this error and Im trying to solve it but the line that it refering to is a notation so I don't understand why it is affecting the program. (2 Replies)
Discussion started by: helpme2008
2 Replies

9. UNIX for Dummies Questions & Answers

awk Shell Script error : "Syntax Error : `Split' unexpected

hi there i write one awk script file in shell programing the code is related to dd/mm/yy to month, day year format but i get an error please can anybody help me out in this problem ?????? i give my code here including error awk ` # date-month -- convert mm/dd/yy to month day,... (2 Replies)
Discussion started by: Herry
2 Replies

10. Shell Programming and Scripting

"syntax error at line 21 :'done' unexpected." error message"

I am trying to run the script bellow but its given me "syntax error at line 20 :'done' unexpected." error message" can someone check to see if the script is ok? and correct me pls. Today is my first day with scripting. Gurus should pls help out #!/bin/ksh # Purpose: Check to see if file... (3 Replies)
Discussion started by: ibroxy
3 Replies
Login or Register to Ask a Question