need help in IF condition!!

Hi All ,,

This is my code .. i am checcking a file temp66.txt , when i execute this
shell it checks whether the file is 0 byte file or not ..

if it is a 0 byte file , then Program exists out else it Prints the Echo Statement ..

if [ -s temp66.txt] then
RET_CODE=$?
if [ $RET_CODE -ne 0 ] then
echo 'File is Zero '
exit 99
fi

else
echo "Shell Worked"

Please help me on this !! i am working on something which will help me when this is resolved !!

fi

Correct your code as:

if [ -s temp66.txt]
then
   echo 'File is Zero '
   exit 99
else
   echo "Shell Worked"
fi

Note: The indentations are only for readability

if tests the exit status of [. In the "if" branch, by definition, $? will be 0. It is the exit code of the command [ -s temp.txt ]. Yes, it is confusing to newbies that "open square bracket" is the name of a command, but that's what it is. You can call it test if you think that's less confusing.

test -s file66.txt || { echo "It's zero"; exit 99; }

unilover: you have the logic backwards, it's zero in the else branch.

i am not sure of this above statement !

when i execute the above command , if the file size is not zero will it move on to the other line like (( echo "shell script worked"))

Thanks a lot !!

Yes, it will not exit if the file's size is non-zero.

Thanks !!!

but i donot want to Print any lines like "Its zero"

test -s file66.txt || {exit 99; }
echo "Shell Script Worked"

Thanks a lot !!

You don't need the braces then; they were necessary when there were multiple commands after the "||" (that's "or" to you).

Thanks for your Help !! it worked

just addon to it .. i want to remove some of the files (more than one file ) once the file size is zero

is it possible .. can you please let me know on this one alone !!

Thanks again

test -s file || rm file

Yeah i tried this one .. i wanted to add exit 99

will it work

like this

test -s file || rm file* rm tes* exit99

Thanks!!

Nope, you'll need to put the braces back in, sorry! (-:

rm file* rm tes* exit99 is a single command, it would try to remove file*, rm, tes*, and exit99. To put multiple commands on the same line, separate them with semicolons.

test -s file || { rm file* tes*; exit 99; }

Can i add a Mailx option in it .. can you please let me know the syntax in it ??

thanks

test -s file66.txt || { mailx -s "file66.txt size zero" you@example.net </dev/null; exit 99; }
echo "Shell Script Worked"

The braces are used when there are multiple commands you want to execute after || but you could also write this as an "if then else" if that's more convenient. Then it's easier to add commands, too.

if test -s file66.txt
then
    mailx -s "file66.txt size zero" you@example.net </dev/null
    exit 99
fi
echo "Shell script worked"

The braces and the semicolons are not necessary here, because everything up to "fi" (or "else") is executed when the condition matches. The || { stuff; } syntax is just basically a shorthand for when you want to cram stuff into a one-liner.

Just need a quick info on this . if we need to break exit 1 or exit 0 will work instead of exit 99

Thanks!!

This should also work

test -s file66.txt && echo "Shell Script Worked" || exit 99

or

[ -s file66.txt ] && echo "Shell Script Worked" || exit 99

Thanks .. But just need to confirm whether exit 0 or exit 1 will work ..

I dont want to use exit 99 ..

Please let me know exit 0 or exit 1 will work instead of exit 99.

Thanks!!

"exit" will always exit, the number is the result code returned by the script from which you exited. An exit code of zero conventionally means "no error", any other number is treated as an error. This doesn't really matter if you are simply ignoring the exit code; but returning a meaningful exit code is useful so that you can use the script from another script.

if yourscripthere; then
  # your script did "exit" or "exit 0"
else
  # your script did "exit 1" or some other nonzero number
  # the precise number will be in $?
fi

The maximum number is 255 (as per POSIX, etc).