Wednesday, August 27, 2008

echo $?

echo $? have the return value of the command. Therefore this is used to check that the command properly worked or not. If command properly worked this returns 0.

#!/bin/bash
cat $1 >/dev/null 2>/dev/null
if [ $? -eq 0 ]
then
echo "Cat worked"
else
echo "File Not Found"
fi


The above program use the command line argument as a file name.

* In unix /dev/null folder is always empty. If we want to delete something we can move that in to /dev/null folder

eg:
ls -l /etc >/dev/null 2> /dev/null
This command doesn't output anything to the screen.
The purpose of this is to make sure that the command is properly working. For that we have to type echo $? immediately after executed that command.

No comments: