Error in excuting while loop

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Error in excuting while loop
# 1  
Old 11-18-2016
Error in excuting while loop

can you please support me to understand what is wrong in this code, I am getting error as
Code:
sytnax error near unexpected token if

Code:
 if [ -n $word ] then

code is
Code:
cat text.txt | /path/mapper.sh

data in file is
Code:
hi hi how how are you

Code:
while read line
do
 for word in $line do
 if [ -n $word ] then
	wcount=`echo $word | wc -m`;
	wlength=`expr $wcount - 1`;
	letter=`echo $word | head -c1`;
	echo -e "$lettert$wlength";
 fi
done
done

# 2  
Old 11-19-2016
Hi,

Code:
 for word in $line do
 if [ -n $word ] then

should be
Code:
 for word in $line
 do
   if [ -n $word ]
   then

a newline may also be replace by a semicolon
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 11-19-2016
Further quote $word, at least in
Code:
 if [ -n "$word" ]; then

This is because the shell evaluates it first then passes it to the [ ] test that will not see an empty string and might give a syntax error.
BTW most shells understand
Code:
 if [[ -n $word ]]; then

where parsing and syntax check happens before $word is evaluated.
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 11-19-2016
Quote:
Originally Posted by mirwasim
Code:
while read line
do
 for word in $line do
 if [ -n $word ] then
	wcount=`echo $word | wc -m`;
	wlength=`expr $wcount - 1`;
	letter=`echo $word | head -c1`;
	echo -e "$lettert$wlength";
 fi
done

You have already been explained what led to the error you got. This is a side issue:
Code:
wcount=`echo $word | wc -m`

You should NOT use backticks for this kind of operation. If you want to run a series of commands and feed the output of these into a variable do it with modern POSIX means instead of outdated (and in the meanwhile deprecated) backticks:

Code:
wcount=$(echo $word | wc -m)

This will do the same but - in sharp contrast to backticks - can even be nested:

Code:
var=$(command1 $(command2 | command3) | command4)

Another thing is this:

Code:
letter=`echo $word | head -c1`

Use parameter substitution for this, which is way faster and uses far less system resources:

Code:
letter="${word%${word#?}}"

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 5  
Old 11-19-2016
Quote:
Originally Posted by mirwasim
can you please support me to understand what is wrong in this code [...]
Code:
while read line
do
 for word in $line do
 if [ -n $word ] then
	wcount=`echo $word | wc -m`;
	wlength=`expr $wcount - 1`;
	letter=`echo $word | head -c1`;
	echo -e "$lettert$wlength";
 fi
done
done

No variable $lettert

I would like to suggest the following snip:

Code:
while IFS= read line; do
  for word in $line; do
      if [ -n $word ]; then
          echo "${word}: ${#word}"
      fi
  done
done < "$@"

After making the script executable it can be invoked as:
Code:
/path/to/mapper.sh /path/to/text.txt

This other one could be an evolution of it:

Code:
while IFS=  read line; do
    for word in $line; do
        [ -n $word ] && echo "${word}: ${#word}"
    done
done < "$@"

It doesn't hurt but it doesn't matter to double quote the variable word (in this case). There is no need to protect against variable word splitting because of the for loop

In fact, this could be one of those occasions where the following design might be acceptable.

Code:
for word in $(cat text.txt); do
   [ -n $word ] && echo "$word ${#word}"
done

Please, do not use a for loop to read from a $(cat file) to just read from a file. Normally, you want to prevent exactly what you want this time.

Last edited by Aia; 11-19-2016 at 03:43 PM..
This User Gave Thanks to Aia For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Not getting colorcode when excuting html in UNIX

Hi i have below code which i am running and the out i want should be in red color echo "<html>" >> ERROR_FILE.html echo "<Body>" >> ERROR_FILE.html nawk 'BEGIN{print "<table border="1">"} {print "<tr>"; print "<TD colspan="0" bgcolor="#DC143C">"; for(i=1;i<NF;i++)... (5 Replies)
Discussion started by: mirwasim
5 Replies

2. UNIX for Dummies Questions & Answers

Crontab jobs excuting

Hi i want to run the cronjob from 7 Am till 11.30Am for every 5 minutes from monday to friday. can anyone help? (15 Replies)
Discussion started by: satheesh_charle
15 Replies

3. Programming

How to make C++ give me a chance to inter a number while excuting?

Iam Trying to simulate a hardware "pci" by visual c++ program..... " i don't have the hardware right now so i want to test my program" the hardware is about to inter a number for example from switches compare it to other number also from switches.."which iam gonna take it from 1 textbox"... 1-... (0 Replies)
Discussion started by: fwrlfo
0 Replies

4. Shell Programming and Scripting

excuting the following code

hi i am trying the below code for the following |_ | |_ | |_ |_ | |_ |_ |_ | |_ |_ |_ |_ and for this code also * * * * * * * * * * * * * * * !/bin/bash #i = "*" (2 Replies)
Discussion started by: kullu
2 Replies

5. Shell Programming and Scripting

Error Using an if Loop Within a While Loop

Hello All, I am having a problem with an “if loop” within a “while loop” in my Korn Shell program. The basic concept of the program is that it searches for the existence of a series of load files in a load directory, and once it finds one of these files, it begins the following process: · Creates... (4 Replies)
Discussion started by: jonesdk5
4 Replies

6. Shell Programming and Scripting

Problem in excuting my First Shell Script

Hi i am a newbie to unix /Linux .Please help I have a following script which says that there is a syntax error :( My program is echo Enter two Numbers read a b if echo First is greater else echo Second is greater fi (3 Replies)
Discussion started by: Ravi Pavanv
3 Replies

7. AIX

crontab isn't excuting some commands

Greetings everybody, I have an IBM P520 AIX 5.3 server machine and trying to use crontab to periodically excute a script that contains a command belongs to my software (Fast/Tools SCADA software) I added the following line after using crontab -e 01 * * * * /mypath/myscript I have two... (3 Replies)
Discussion started by: ayman metwally
3 Replies

8. Shell Programming and Scripting

Excuting UNIX Functions under several username and host

Hi, I am facing a issue in one of my script, Please help me on the same. Below I have the example. Example: I have two functions(host(),user()) in a single file named test1.ksh File Name: test1.ksh host () { HOST=`hostname` echo... (1 Reply)
Discussion started by: samvino
1 Replies

9. UNIX for Dummies Questions & Answers

Excuting Multiple shell files in sequesnce

Hi, I have two shell scripts each executing a java process independently. These are two independent processes and I need to sequence them using another shell script. What I did was created another shell script and called these independent shell scripts in it. It runs fine, however from time... (3 Replies)
Discussion started by: gupta_arunesh
3 Replies

10. UNIX for Dummies Questions & Answers

excuting a shell script within ftp script

Novice here... I need help with excuting a shell script on a flat file that I've transfered over from a Windows XP machine for manipulation through an auto FTP script... so that after it is transfers it excutes the shell script and then returns it back to XP machine... Any ideas... (2 Replies)
Discussion started by: Gerry405
2 Replies
Login or Register to Ask a Question