How to use for and if?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use for and if?
# 1  
Old 06-06-2017
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

HTML Code:
cat 123.txt

ram
suresh
John
PHP Code:
for chat in $(cat /tmp/t123.txthead -1)
do
call=`grep -i incoming /home/$chat | wc -l `
if [ -
"$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

HTML Code:
got incoming call from ram
got incoming calls from suresh
got incoming calls from John
Note: new to shell scripting , trying afte longtime Smilie ... so appericiate your help here
# 2  
Old 06-06-2017
I can show you some of it:
Code:
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?
# 3  
Old 06-06-2017
PHP Code:
for chat in $(cat /tmp/123.txthead -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 [ -"$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 

               fi  

done 
# 4  
Old 06-06-2017
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?
# 5  
Old 06-06-2017
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.
This User Gave Thanks to natraj005 For This Post:
# 6  
Old 06-06-2017
OK. Have you tried the code I posted? It reads line by line without any need for grep or ed.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 06-06-2017
tried below one and no o/p from the script

PHP Code:
#!/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 [ -"$call]
       
then
            
if [ -"$search]
               echo 
"$chat have incoming "
       
else
               echo 
"$chat dont have incoming "
       
fi
       fi

done 
< /tmp/123.txt 
anything am missing here
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question