How to use for and if?

I have a secinario like , want to cat a file until the 123.txt gets empty ....for that i made below syntax, but looks not correct

cat 123.txt

ram
suresh
John
for chat in $(cat /tmp/t123.txt| head -1)
do
call=`grep -i incoming /home/$chat | wc -l `
if [ -n "$call" ]
then 
echo " got incoming call from $chat "
else 
echo "no incoming calls from $chat "
CHK=$(cat /tmp/t123.txt )
printf "1d\nw\nq\n" | ed -s $CHK
                  if [ $CHK -ge 1 ]
			then
			chat ++
			else 
			    exit 0
	           fi 

done

My required o/p would be

got incoming call from ram
got incoming calls from suresh
got incoming calls from John

Note: new to shell scripting , trying afte longtime :frowning: ... so appericiate your help here

I can show you some of it:

while read chat # Read lines one by one
do
        call=$(grep -c incoming /home/$chat)
        if [ "$call" -gt 0 ]
       then
               echo "$call > 0"
       else
               echo "$call = 0"
       fi

done < /tmp/t123.txt

...but I have absolutely no idea what CHK is supposed to be doing. More information please?

for chat in $(cat /tmp/123.txt| head -1)  # cutting the 1st line of the file which should be ram in my case
do 
call=`grep -i incoming /home/$chat | wc -l ` # search for incoming in /home/ram file and taking a count of it 
if [ -n "$call" ] 
then  
echo " got incoming call from $chat " 
else  
echo "no incoming calls from $chat " 
CHK=$(cat /tmp/123.txt ) 
printf "1d\nw\nq\n" | ed -s $CHK # removing teh first line of my file so that ram will get remove and suresh will come at the top
                  if [ $CHK -ge 1 ]  # will check wther 123.txt is ge to 1 value
            then 
            chat ++  # then i have to perform the same for action until the file gets empty
            else  
                exit 0 
               fi  

done  

That code is all wrong so explains very little of what you actually want.

What do the contents of /tmp/123.txt look like? Are they just names, line by line, one by one?

yes only names in /tmp/123.txt line by line as below

ram
suresh
John

my requirment is want to seach a pattern called " incoming" in file called /home/ram and then have to displayed that if there is incoming word in the file and then have to serach the same word in /home/suresh and display it and then have to serach the same word in /home/John file and display it and come out of the script.

Hope am making some sense now.

1 Like

OK. Have you tried the code I posted? It reads line by line without any need for grep or ed.

1 Like

tried below one and no o/p from the script

#!/bin/ksh
 while read chat # Read lines one by one
do
        call=$(/home/$chat)
		search=$(grep  -c incoming $call) # need to have two variable and in second variable first variable will get subtuted. 
        if [ -z "$call" ]
       then
			if [ -z "$search" ]
               echo "$chat have incoming "
       else
               echo "$chat dont have incoming "
       fi
	   fi

done < /tmp/123.txt

anything am missing here

From what you have said, presumably /home/$chat expands to the home directory for the user named by the expansion of $chat ( /home/ram , /home/suresh , and /home/John , respectively for the three lines in /tmp/123.txt ).

Therefore, you should get diagnostics from the Korn shell similar to:

-ksh: /home/ram: cannot execute [Is a directory]
-ksh: /home/suresh: cannot execute [Is a directory]
-ksh: /home/John: cannot execute [Is a directory]

while setting the variable call to an empty string. Directories are not regular files. (Therefore, executing a directory does not work.) They are not text files. (Therefore, grep ing a directory probably won't give you anything useful.) Filenames (including directories) on UNIX and UNIX-like systems are case-sensitive. User login names on UNIX and UNIX-like systems are almost always entirely lower-case. John might actually be a login name on your system, but it is highly suspicious.

Having done that, the 1st if would always fail and the 2nd if would not be executed, but that doesn't matter because the 2nd if doesn't have a then . So before even starting the for loop, you would have gotten a diagnostic similar to:

-ksh: syntax error: `else' unexpected

and your script would terminate with a non-zero exit code.

Diagnostics and non-zero exit codes do not qualify as "no o/p from the script".

What is the name of your script?

What output do you get from the command:

ls -l name

where name is the name of your script?

Exactly what command did you use to invoke your script?

1 Like

You've fallen into the same trap as in your other thread. Don Cragun pointed the syntax error out (amongst others). The logical one is making the -z test, inverting the desired behaviour.
Plus, on the second sight, you seem to want to evaluate some variables numerically, so -z or -n wouldn't be the correct test.

1 Like

Thanks guys, its worked for me.... both "for" and "while" worked for me, its my bad early i used it wrong.