what does if[$? -ne 0] mean in unix

Hi,

what does this mean in unix
if [$? -ne 0]

I understand ne is not equal to zero but how abt $?

Also, what does exit 1 or exit 0 mean.

$? is the return code form a previous command or function
in unix, 0 is the standard return code for a successful completion

if [$? -ne 0]
The whole line means

'if the previous return code was not equal to 0 ' ....

It will then be followed by

then
statement # do something
(optional) else
(optional) statement # do something else
fi # closure of the 'if' conditional

Steve

1 Like

Thanks Steve.

but another question.

I have a FTP process happening and the file from remote m/c is FTPed to local m/c's file called result file, now how can I check if the FTP process was fine and if a result file was created.

This is not a technical forum. Mods please move this thread to the appropriate forum.

thanks for the information.....but can u tell me what is the previous return code............
is the command or code above the if statement ?

im very new to shell scripting..........any help ?

and also can u tell me what does this means if [ $# -ne 3 ];

Is the exit code of the previous command.

If the number of arguments is not equal to 3 ... do something.

You should begin by reading An Introduction To Shell Programming
... and when you finish proceed to: http://www.unix.com/answers-frequently-asked-questions/13774-unix-tutorials-programming-tutorials-shell-scripting-tutorials.html

Hello Shree,

To verify whether data copying is happened properly, you can use "md5sum" check. get the original file md5sum and again get it after copying it to local machine. Compare them. If same then file has been copied properly else issue while copying file.

And to check if result file has been created use,

if [ -e "filename" ]
then
echo "File exist"
else
echo "File does not exist"
fi

Please let me know if u need more help.

  • nilesh

thanks for info and links........will go through that