简体   繁体   中英

shell scripting multiple conditions in single if condition

In the code below, in /bin/sh, why is second condition of -o evaluated when first condition is satisfied. The scenario is that $output has multiple "ORA-" strings in separate lines, but it doesnot have "INVALID_OBJECTS" string. So first part is true, but second part becomes

"" -gt 0

which fails with

sh: [: missing `]'

if [ "`echo "$output" | grep ORA-`" -o "`echo "$output" | awk '/INVALID_OBJECTS/{getline;getline;print $0}' | sed 's/\s//g'`" -gt 0 ]; then
        echo -e "\n*** ERROR:  ***\n"
fi

UPDATE: The $output holds stdout of pl-sql block. The end-user had entered Ctrl+C during execution of the pl-sql block, which resulted in loss of connection to SQL* Plus. So the stdout ended up having multiple "ORA-nnnnn" errors. If there was no loss of connection, I would have got the following lines in $output which is checked in left side of the -o check.

INVALID_OBJECTS  
_ _ _ _ _ _ _ _ _ _  
                   3

Well, let's simplify your expression first a bit, by removing unnecessary quotes. The test command basically boils down to:

[ `echo $output | grep ORA-` -o `echo $output | awk '/INVALID_OBJECTS/{getline;getline;print $0}' | sed 's/\s//g'` ]

So you have basically zero or more words (depending on what the grep yields) on the left of the -o , followed by zero or one word (depending on what awk yields) to the right of the -o .

Now I have no idea WHAT you want to achieve here, but you can see from this analysis, that your command does not make sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM