# Getting error in command line arguments

**URL:** <https://community.unix.com/t/getting-error-in-command-line-arguments/191464>\
**Category:** Shell Programming and Scripting\
**Created:** [April 3, 2008, 2:29am UTC](https://community.unix.com/t/getting-error-in-command-line-arguments/191464 "2008-04-03T02:29:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunitachoudhury](https://community.unix.com/letter_avatar/sunitachoudhury/32/5_5575768a8748004e209b776fc1b2916d.png) [@sunitachoudhury](https://community.unix.com/u/sunitachoudhury)\
**Post date:** [April 3, 2008, 2:29am UTC](https://community.unix.com/t/getting-error-in-command-line-arguments/191464/1 "2008-04-03T02:29:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ranj1](https://community.unix.com/letter_avatar/ranj1/32/5_5575768a8748004e209b776fc1b2916d.png) [@ranj1](https://community.unix.com/u/ranj1)\
**Post date:** [April 3, 2008, 2:36am UTC](https://community.unix.com/t/getting-error-in-command-line-arguments/191464/2 "2008-04-03T02:36:16Z")

</div>

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 -

```nohighlight
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 -

```nohighlight
./1.ksh dm5admin 

```

---

<div class="post-metadata">

**Author:** ![sunitachoudhury](https://community.unix.com/letter_avatar/sunitachoudhury/32/5_5575768a8748004e209b776fc1b2916d.png) [@sunitachoudhury](https://community.unix.com/u/sunitachoudhury)\
**Post date:** [April 3, 2008, 3:07am UTC](https://community.unix.com/t/getting-error-in-command-line-arguments/191464/3 "2008-04-03T03:07:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vino](https://community.unix.com/user_avatar/community.unix.com/vino/32/156_2.png) [@vino](https://community.unix.com/u/vino)\
**Post date:** [April 3, 2008, 3:54am UTC](https://community.unix.com/t/getting-error-in-command-line-arguments/191464/4 "2008-04-03T03:54:00Z")

</div>

> [@sunitachoudhury](#):
>
> 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

You can use what ranj@chn has provided. If you want to stick with your script, then

```plaintext
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

```

---

<div class="post-metadata">

**Author:** ![ag79](https://community.unix.com/letter_avatar/ag79/32/5_5575768a8748004e209b776fc1b2916d.png) [@ag79](https://community.unix.com/u/ag79)\
**Post date:** [April 3, 2008, 4:58am UTC](https://community.unix.com/t/getting-error-in-command-line-arguments/191464/5 "2008-04-03T04:58:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sunitachoudhury](https://community.unix.com/letter_avatar/sunitachoudhury/32/5_5575768a8748004e209b776fc1b2916d.png) [@sunitachoudhury](https://community.unix.com/u/sunitachoudhury)\
**Post date:** [April 3, 2008, 4:59am UTC](https://community.unix.com/t/getting-error-in-command-line-arguments/191464/6 "2008-04-03T04:59:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ranj1](https://community.unix.com/letter_avatar/ranj1/32/5_5575768a8748004e209b776fc1b2916d.png) [@ranj1](https://community.unix.com/u/ranj1)\
**Post date:** [April 3, 2008, 5:15am UTC](https://community.unix.com/t/getting-error-in-command-line-arguments/191464/7 "2008-04-03T05:15:11Z")

</div>

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

```nohighlight
if [[$# -ne 2]]
then
..
fi

```

So, as you keep on adding parameters, change the count here. I hope it is clear now.

---

<div class="post-metadata">

**Author:** ![sunitachoudhury](https://community.unix.com/letter_avatar/sunitachoudhury/32/5_5575768a8748004e209b776fc1b2916d.png) [@sunitachoudhury](https://community.unix.com/u/sunitachoudhury)\
**Post date:** [April 3, 2008, 6:29am UTC](https://community.unix.com/t/getting-error-in-command-line-arguments/191464/8 "2008-04-03T06:29:23Z")

</div>

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??????

---

<div class="post-metadata">

**Author:** ![ag79](https://community.unix.com/letter_avatar/ag79/32/5_5575768a8748004e209b776fc1b2916d.png) [@ag79](https://community.unix.com/u/ag79)\
**Post date:** [April 3, 2008, 6:45am UTC](https://community.unix.com/t/getting-error-in-command-line-arguments/191464/9 "2008-04-03T06:45:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kany2k](https://community.unix.com/letter_avatar/kany2k/32/5_5575768a8748004e209b776fc1b2916d.png) [@kany2k](https://community.unix.com/u/kany2k)\
**Post date:** [January 14, 2009, 8:13pm UTC](https://community.unix.com/t/getting-error-in-command-line-arguments/191464/10 "2009-01-14T20:13:46Z")

</div>

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 :):)🙂
