Getting error in command line arguments

Hi,

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

Can anybody rectify this?????

Thanks.

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

Call the script as -

./1.ksh dm5admin 

yes i am running the script by using following arguments

1.sh dm5admin

but when i am not giving any arguments then i am getting error.

My requirement is that if i won't give any arguments the it will print

echo "UID can't be blank"

But when i am giving no arguments the i am getting the above error.

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.

Hi ranj@chn,

thanks for your cooperate.

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

then i am not getting my expecting results.

I am getting the following output

> 1.sh scu51 sunita
UID cann't be blank

$# 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.

Hi ag79,

ya i also don't want to put my password.

Is there any way to hide the password. Actually i want to do the validation of my id and password before logining into FTP

Can anybody help me??????

if you want to automate an ftp securely, you'll need non interactive authentication.
see man pages for sshd, ssh-keygen, and sftp for more details.

there are already several hundred pages on the web that'll help you set up secure ftp with public/private key authentication - use google.

of course there are other compromised solutions like denying read access to file(give only exec option), etc..

Hi Guys,

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.

Kannan :):):slight_smile: