Getting error in command line arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting error in command line arguments
# 1  
Old 04-03-2008
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.
# 2  
Old 04-03-2008
how are you executing?

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 -
Code:
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 -
Code:
./1.ksh dm5admin


Last edited by ranj@chn; 04-03-2008 at 05:50 AM.. Reason: typo
# 3  
Old 04-03-2008
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.
# 4  
Old 04-03-2008
Quote:
Originally Posted by 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

Code:
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


Last edited by vino; 04-03-2008 at 06:32 AM..
# 5  
Old 04-03-2008
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.
# 6  
Old 04-03-2008
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
# 7  
Old 04-03-2008
$#

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

Code:
if [[ $# -ne 2 ]]
then
..
fi

So, as you keep on adding parameters, change the count here. I hope it is clear now.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Loop with command line arguments

Ubuntum, Bash version: 4.3.46 Hi, how can I create a loop where the command line arguments change (increase) and every time the number of arguments is different ? ### I have many gene names... that mean gene1=$2, gene2=$3, ...... geneN=$N+1 ### some time the number of gene is 25, other... (7 Replies)
Discussion started by: echo manolis
7 Replies

2. Shell Programming and Scripting

Command line arguments for addition

Hi all, I am trying to write a code for addition of n numbers which will be passed by the user as command line arguments. I wrote the following code. add=0 for (( i = 1 ; i <= $# ; i++ )) do add=`expr $i + $add` done #echo "sum is : $add" input : $./add.sh 12 32 14... (7 Replies)
Discussion started by: PranavEcstasy
7 Replies

3. Shell Programming and Scripting

command line arguments

hi,,,, I want to create a command prompt, for example "prompt>", so my prompt need to handle commands, for example "prompt>cmd", so i want to know how to get arguments for my own commands cmd, i.e. default argc should contain arguments count and argv should point to the argument vector i.e, for... (2 Replies)
Discussion started by: vins_89
2 Replies

4. Shell Programming and Scripting

Command Line Arguments - not working

I am new to the world of Unix and shell scripting and have been trying to get the following simple script to work: #!/bin/bash echo "what is your age?" echo "you are $1 years old"I want to be able to enter my age on the command line, when prompted, and it return the... (1 Reply)
Discussion started by: meursault
1 Replies

5. UNIX for Dummies Questions & Answers

command line arguments

hi, can someone how to accept command line arguments as a variable using in script? like: ./scriptname arguments by accept arguments, I can use it in my script? thx! (1 Reply)
Discussion started by: ikeQ
1 Replies

6. Shell Programming and Scripting

Maximum command line arguments

Hi, Can anyone please help me to know what is the maximum number of command line arguments that we can pass in unix shell script? Thanks in advance, Punitha.S (2 Replies)
Discussion started by: puni
2 Replies

7. UNIX for Dummies Questions & Answers

Command line arguments.

I am working on a script wherein i need the user to enter the Build ID for eg:the command line will show enter the build ID Now on entering the build ID it should be assigned to @ARGV. How can this be done.? (1 Reply)
Discussion started by: Varghese
1 Replies

8. Shell Programming and Scripting

command line arguments

-------------------------------------------------------------------------------- I have this while loop and at the end I am trying to get it to tell me the last argument I entered. And with it like this all I get is the sentence with no value for $1. Now I tried moving done after the sentence... (1 Reply)
Discussion started by: skooly5
1 Replies

9. UNIX for Dummies Questions & Answers

arguments in command line

Hi all, How many arguments can we pass while testing a prgm at command line.. I encountered an issue while passing 10 arguments. For $10 its taking argument passed for $1 followed by 'zero'. can we pass more than 9 arguments /Is there any other way. Thanks, rrs (6 Replies)
Discussion started by: rrs
6 Replies

10. Programming

command line arguments

Hi How to pass multi line text as a command line argument to a program. (i.e) ./a.out hi this is sample 0 file1 where hi this is sample should be stored in argv 0 in argv and so on... (3 Replies)
Discussion started by: bankpro
3 Replies
Login or Register to Ask a Question