Visit Our UNIX and Linux User Community


Check condition inside the loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check condition inside the loop
# 1  
Old 07-12-2011
Check condition inside the loop

Hi,

I am in trouble. I can get inside my condition test inside a loop :

I am in ksh (solaris)

Code:
while read file
do
<commande to retrieve file> >> ${LOG_RETRIEVE_FILE.log}
msg_err=$(cat ${LOG_RETRIEVE_FILE.log} | grep "error retrieve")
if [ ${msg_err} -gt 0 ] ; then
<sendmail>
exit 1
fi
done

I tried different options, but none were working.

thanks for your help
# 2  
Old 07-12-2011
Code:
msg_err=$(cat ${LOG_RETRIEVE_FILE.log} | grep "error retrieve")

replace it to 

msg_err=$(grep -c "error retrieve" ${LOG_RETRIEVE_FILE.log})

# 3  
Old 07-12-2011
Thnaks for your help, but this is not working ....

It does return value if i ma using it manually but not in the shell script ... Smilie
# 4  
Old 07-12-2011
Code:
while read file
do
<commande to retrieve file> >> ${LOG_RETRIEVE_FILE.log}  #what exactly you doing here? 
msg_err=`grep -c "error retrieve" $LOG_RETRIEVE_FILE.log`
echo $msg_err
if [ "$msg_err" -gt "0" ] ; then
echo "send email"
exit 1
fi
done < inputFile  #where is your input for the while loop ?

This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 07-12-2011
Quote:
Originally Posted by itkamaraj
Code:
while read file
do
<commande to retrieve file> >> ${LOG_RETRIEVE_FILE.log}  #what exactly you doing here? 
msg_err=`grep -c "error retrieve" $LOG_RETRIEVE_FILE.log`
echo $msg_err
if [ "$msg_err" -gt "0" ] ; then
echo "send email"
exit 1
fi
done < inputFile  #where is your input for the while loop ?

Maybe he's reading file from keyboard input?

Code:
msg_err=$(cat ${LOG_RETRIEVE_FILE.log} | grep "error retrieve")
if [ ${msg_err} -gt 0 ] ; then

msg_err will most likely be a string, not a number, something along error retrieve [a number maybe...?]
So you can't use -gt to test if error retrieve [something] is greater than 0
Can you post some lines of your log file? (namely those with 'error retrieve' )

Last edited by tukuyomi; 07-12-2011 at 12:30 PM.. Reason: Typo
This User Gave Thanks to tukuyomi For This Post:
# 6  
Old 07-12-2011
If you are only interested about grep true/false, not count then
Code:
if grep "error retrieve" $LOG_RETRIEVE_FILE.log >/dev/null 2>&1
then
     <sendmail>
     exit 1
fi

Because grep exit status is 0 if find something.

Variable name LOG_RETRIEVE_FILE or LOG_RETRIEVE_FILE.log ?
${LOG_RETRIEVE_FILE}.log or ${LOG_RETRIEVE_FILE.log}
This User Gave Thanks to kshji For This Post:
# 7  
Old 07-13-2011
Thanks all for your help.

I found the error on my script. As I check the return code while connecting to the archive system to retrieve files on the top of the code, I was unable to get inside of the condition in my loop.

I am trying to find a way to solve this on my own, but thanks again for your help.

Previous Thread | Next Thread
Test Your Knowledge in Computers #918
Difficulty: Medium
Unix time is a single signed number which increments every millisecond.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check two condition in while loop

Hi, I Have to check two condition in while loop every 2 minutes. while loop is accompanied with number of times it will check.Please help in putting the two condition in while loop as appropriate. z= input value, A=1 while do 1.check the file output,if the file output is N then keep on... (2 Replies)
Discussion started by: netdbaind
2 Replies

2. Shell Programming and Scripting

If else condition inside for loop of awk command in UNIX shell scripting

Hi , Please excuse me for opening a new thread i am unable to find out the syntax error in my if else condition inside for loop in awk command , my actual aim is to print formatted html td tag when if condition (True) having string as "failed", could anyone please advise what is the right... (2 Replies)
Discussion started by: karthikram
2 Replies

3. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

4. Shell Programming and Scripting

WHILE LOOP CONDITION CHECK

Hello I want to compare values of two variables as CHECK condition in a while loop. eg: var1=0 var2=10 while do echo " $var1 " var1=`expr $var1 + 1` done However this is giving error.How to do it in a proper manner? Thanks. (3 Replies)
Discussion started by: dashing201
3 Replies

5. Shell Programming and Scripting

csh if loop variable condition check peroblem

Hi, I have variables like var1 var2 var3 var4 in if loop i am trying to check the condition if(variable == "var") then echo $variable endif (0 Replies)
Discussion started by: vasanth.vadalur
0 Replies

6. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

7. Shell Programming and Scripting

using flag inside a for loop to check condition

I have a logic like this It initializes the flag variable as "T" at the beginning of the loop everytime Inside each loop it checks for two conditions and updates the flag variable as "A" or "B" In the end of the loop it checks for the value of the variable flag for "A" or "B" and execute... (4 Replies)
Discussion started by: codeman007
4 Replies

8. Shell Programming and Scripting

condition inside a for loop

I have a for loop in my script as shown below. for file_path in $file_list ; do ........my code .......... ...... done Can i restrict the number of files parsing to the variable file_path as 50? That is, even if I have some 100 files in file_list, I need to take only 50 files for... (7 Replies)
Discussion started by: Vijay06
7 Replies

9. Shell Programming and Scripting

Checking condition inside the loop

Hi all, I have one clarification i am using the loop which will process for each record .suppose there is f ailure in the first record it need to send mail and process the next .my code: defcount=`cat <filename>|wc -l` while ] do if <some condiotion> then echo "mail" fi done so... (1 Reply)
Discussion started by: ithirak17
1 Replies

10. Shell Programming and Scripting

script to check for a condition inside a file

Hi I am writing a script file which sends the log files along with their size in a folder named log to a file called temp.log using the following cmd: ls -st 190_GSTV_HUX_003QISCGSK026** >> /home/user/temp.log the temp.log looks like this: 16 190_GSTV_HUX_003QISCGSK026_message070321.log ... (11 Replies)
Discussion started by: kiran1112
11 Replies

Featured Tech Videos