When i am running the following script 1.sh (without giving the command line arguments) then i am getting the following error.
if [ $1 = dm5admin ]
then
echo "UID and PWD are correct"
elif [ $1 != dm5admin ]
then
echo "Either UID or PWD is wrong. Please check your UID and PWD"
else
echo "UID and PWD can't be blank"
exit;
fi
by running this 1.sh script i am getting the following error.
>1.sh
./1.sh: [: =: unary operator expected
./1.sh: [: !=: unary operator expected
UID and PWD can't be blank
Are you passing any arguments when running the script? The error points to no args passed to the script. Instead of the below logic, see if this helps -
if [[ $# -ne 1 ]]
then
echo "Wrong usage"
echo "USAGE: $0 [user-id]"
exit -1
fi
#now check if the correct id was passed or not
if [[ $1 != "dm5admin" ]]
then
echo "Invalid id passed"
exit 2
fi
#carry on with processing now
You can use what ranj@chn has provided. If you want to stick with your script, then
if [ x$1 -eq xdm5admin ]
then
echo "UID and PWD are correct"
elif [ x$1 -ne xdm5admin ]
then
echo "Either UID or PWD is wrong. Please check your UID and PWD"
else
echo "UID and PWD can't be blank"
exit;
fi
this is wrong
if [[ $1 != "dm5admin" ]]
here's what is going wrong: shell cant understand the following when u dont pass anything:
if [[ != "dm5admin" ]]
this is what you need
if [ "$1" != "dm5admin" ]
the shell will see this when u dont pass anything
if [ "" != "dm5admin" ]
which it will understand.
*Always* use double quotes when using shell script variable, unless you have a *very* good reason not to.
i dont know what you're trying to do with this script, but putting a password in it is not a great idea. anyway, thats your business.
now i have two arguments like when i am trying to run the following
1.sh scu51 sunita
contents of 1.sh are
if [ $# -ne 1 ]
then
echo "UID cann't be blank"
exit -1
fi
if [ $# -ne 2 ]
then
echo "PWD cann't be blank"
exit -1
fi
if [[ $1 != "scu51" ]]
then
echo "Invalid UID passed"
exit 2
fi
$# tells the no of arguments passed to the script. Since, in your case you have passed 2 arguments, the first "if" condition gets satisfied and the error message gets thrown.
So, if you want to check that the script should take only 2 arguments, use the second if..then in your script.
if [[ $# -ne 2 ]]
then
..
fi
So, as you keep on adding parameters, change the count here. I hope it is clear now.
If anyone has worked on capturing the errors the script when the COBOL program abends. I am calling a COBOL program from the UNIX script, for some reason if the COBOL program abends the script should not proceed further. Any ideas or any sample code would be highly appreciated.