[Solved] FOR loop / IF statement returning error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] FOR loop / IF statement returning error
# 1  
Old 08-30-2013
[Solved] FOR loop / IF statement returning error

The code at the bottom is a simplified example of what we have.

If I use the following:
Code:
 [ $? != 0 ] && echo "echo failed"

$? returns 1

When I use
Code:
if [ $? != 0 ] ; then  echo "echo failed" ; fi

$? returns 0

Does anyone know what's wrong with this?

Using AIX 6.1 and KSH

Code:
for NUM in 1 2 3
do
    echo $NUM
    for LET in A B C
    do
        echo $LET
        [ $? != 0 ] && echo "echo failed"
        echo $?
    done
done

Thankyou.

Last edited by Scott; 08-30-2013 at 12:17 PM.. Reason: More code tags
# 2  
Old 08-30-2013
You're using a string comparison operator [ $? != 0 ].

Try -ne instead of !=
# 3  
Old 08-30-2013
Thanks for the quick response.

Using -ne also doesn't work
# 4  
Old 08-30-2013
Because you cant use $? like that...
Code:
        let DID=$?
        echo DID: $DID
        #let DID=1
        [ $DID != 0 ] && echo "echo failed"
        echo DID: $DID

I set DID to 1 so to see your condition work... in other words your script will run with DID=0 always since the command producing the result of it ($?) at that point is successful...
I know Im not very clear but its friday...

So your script now :
Code:
for NUM in 1 2 3
do
    echo $NUM
    for LET in A B C
    do
        echo $LET
        let DID=$?
        echo DID: $DID
        [ $DID != 0 ] && echo "echo failed"
        echo $?
    done
done


Last edited by vbe; 08-30-2013 at 11:30 AM.. Reason: added modified script
# 5  
Old 08-30-2013
Quote:
Originally Posted by jfxdavies
If I use the following:
[ $? != 0 ] && echo "echo failed"
$? returns 1

When I use
if [ $? != 0 ] ; then echo "echo failed" ; fi
$? returns 0

Does anyone know what's wrong with this?
There is nothing wrong. What you are seeing is correct behavior. In each case you are seeing the exit status of the last command to run.

In the first case, the last command before echoing $? is [ $? != 0 ]. The test failed and so $? is set to 1.

In the second case, you're seeing the exit status of the if-statement itself. Since the conditional test fails, the if-statement does not execute further, the list of commands after then does not execute. So the if-statement itself sets $? to 0.

That following quote is from POSIX: Shell Command Language, but your KSH man page should contain similar text.

Quote:
The exit status of the if command shall be the exit status of the then or else compound-list that was executed, or zero, if none was executed.
Regards,
Alister

Last edited by alister; 08-30-2013 at 12:22 PM..
This User Gave Thanks to alister For This Post:
# 6  
Old 09-02-2013
Alister,

Thanks for the explanation - makes perfect sense.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Statement returning error only launching the sh script via crontab

hi all, I created a sh script to import some tables from mysql to hive. No problem launching it manually, but if I schedule via crontab it returns me an error in the following part: #create an array containing all the tables for $dbname query="SELECT table_name FROM information_schema.tables'... (10 Replies)
Discussion started by: mfran2002
10 Replies

2. Shell Programming and Scripting

[Solved] If statement in bash

I have the following code in bash, however "set red frmt" is not displayed. echo "iarg_rd = $iarg_rd" iarg_rd="2" if ; then echo "Hello World" fi if ; then frmt="${gap}${!frmt_titl_yl}" elif ; then frmt="${gap}${!frmt_titl_bk}" elif ; then echo... (2 Replies)
Discussion started by: kristinu
2 Replies

3. Shell Programming and Scripting

[Solved] While loop error when add sqlplus command

Hi gurus, I hit a block when write the script. I need do while loop, in the loop I have code below. sqlplus -s abc/abc@abc <<EOF spool testwhile select * from dual; spool off exit; EOF when I run script with only this code, it works fine. if I add code as below: #!/bin/ksh ... (5 Replies)
Discussion started by: ken6503
5 Replies

4. Shell Programming and Scripting

[Solved] 0403-057 Syntax error for if statement

I am getting the following error when I am running a script in ksh when trying to execute an if statement comparing two numerical values tstmb.sh: 1.5321e+08: 0403-057 Syntax error Below is my code snippet. #!/bin/ksh set -x TODAY=$(date +%y%m%d) for file in $(ls -rt *.log | tail... (11 Replies)
Discussion started by: kiran1112
11 Replies

5. Shell Programming and Scripting

[SOLVED] Problem in wait statement

Iam having a script which is used to load users and dumpfile in any given schema.Iam trying to autolog the script and have added two fucntion in it. function init_stdout_redirect { OUT_LOG=$1 OUT_PIPE=$(mktemp -u) # Create the output pipe mkfifo $OUT_PIPE # Save stdout and... (15 Replies)
Discussion started by: Vikram_Tanwar12
15 Replies

6. Shell Programming and Scripting

for loop with internal unix command in statement throwing error

Hi I've gotten a plugin script that won't run. I keeps throwing an error at the following line. for BARCODE_LINE in `cat ${TSP_FILEPATH_BARCODE_TXT} | grep "^barcode"` do #something done The error reads ... (3 Replies)
Discussion started by: jdilts
3 Replies

7. UNIX for Dummies Questions & Answers

[Solved] Syntax error for awk in a loop

can some one please tell me what is the problem with my syntax:confused: I have 100 files in one folder 1. want to read each of the line by line 2. calculate their number of the words between the first word and the last word of each line 3. create file for each file with number of words... (8 Replies)
Discussion started by: A-V
8 Replies

8. UNIX for Dummies Questions & Answers

For loop returning more values

Hi All, Thanks all of you for the help you provide to me. Well, I have one more problem, where I am trying to pull file system information in the loop and display the filesystem percentege. I am using following code to achive this, nut it's giving the weired output. My file system is ... (1 Reply)
Discussion started by: alok.behria
1 Replies

9. UNIX for Dummies Questions & Answers

Loop on array variable returning error: 0 not found

I'm guessing i have a syntax error. I'm not sure it get's past the the while condition. I get an error 0 not found. Simple loop not sure what I'm doing wrong. #!/usr/bin/ksh set -A MtPtArray /u03 /u06 tUbound=${#MtPtArray } echo $tUbound i=0 while ($i -lt $tUbound) do print... (4 Replies)
Discussion started by: KME
4 Replies

10. Shell Programming and Scripting

For loop statement - catch error

I'm having a question about for loops. (bash) I have the following for example: for file in `ls *.txt` do read file ... done Now when there is a file present there is no problem, now when there is no file present I get the following output in my standard mail box : "No such... (4 Replies)
Discussion started by: lumdev
4 Replies
Login or Register to Ask a Question