how to specify number of argument in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to specify number of argument in shell
# 1  
Old 01-20-2012
how to specify number of argument in shell

Hi I am new in shell,

I am trying to create a small script that can do exit if a script is executed when argument not 2
Code:
#!/bin/sh
if [ $# -gt 2]; then
        echo greater
        exit 1;
elif [ $# -le]; then
        echo less
        exit 1;
fi

it keeps returning me
[: 10: missing ]
[: 10: missing ]
whatever number of argument I specify..

Can anyone point out?

Thank you
# 2  
Old 01-20-2012
A space is missing 2 and ]
The operand is missing in the second elif statement

Try this... -ne means "not equal to"...
Code:
#!/bin/sh
if [ $# -ne 2 ]; then
    echo "Invalid no. of arguments... Exiting..."
    exit 1
fi

--ahamed
This User Gave Thanks to ahamed101 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python : Problem with " TypeError: float() argument must be a string or a number "

Hello ! I'm creating a CGI which allow to display graph from some data. The datas looks like : 2020-01-13-00-00,384.00,350.00 2020-01-13-06-00,384.00,350.00 2020-01-13-12-00,384.00,350.00 2020-01-13-18-00,384.00,350.00 2020-01-14-00-00,384.00,350.00... (1 Reply)
Discussion started by: Tim2424
1 Replies

2. Shell Programming and Scripting

Getting number of argument passed to a shell script

Hi Experts, I have been trying to work on a simple shell script that will just add the two argument passed to it. Here is what i tried : #!/bin/bash welcome(){ echo "Welcome to this Progg. which will accept two parameter" } main_logic(){ arg=$# echo "Number of argument passed is... (4 Replies)
Discussion started by: mukulverma2408
4 Replies

3. Shell Programming and Scripting

How to accept command line argument as character or text if number is entered?

Hello Does the unix korn shell provide a function to convert number entered in command line argument to text or Character so that in next step i will convert Chr to Hex (6 Replies)
Discussion started by: aadityapatel198
6 Replies

4. Shell Programming and Scripting

passing either of argument, not both in shell

Hi, I have a requirement to work on script, it should take either of arguments. wrote it as below. #!/bin/bash usage() { echo "$0: missing argument OR invalid option ! Usage : $0 -m|-r|-d } while getopts mrdvh opt; do case "$opt" in m) monitor_flag=monitor;;... (1 Reply)
Discussion started by: ramanaraoeee
1 Replies

5. UNIX for Dummies Questions & Answers

How to print number before argument?

Hey everyone, I need to write a script that will display the number of the argument before displaying the argument itself. Here's what I have so far: for arg in "$@" do echo $#:$arg done This returns the sum of all arguments preceding the arguments themselves. $(script) a b c 3:a... (1 Reply)
Discussion started by: unclepickle1
1 Replies

6. UNIX for Dummies Questions & Answers

help with shell scripting argument

#!/bin/bash echo "enter a file or directory name" read name if then echo " argument is file " ls -l $name | awk '{print $1,}' elif echo " argument is a directory" ls -l $name | awk '{print $1}' fi what i am trying to do. get input... (3 Replies)
Discussion started by: iluvsushi
3 Replies

7. Shell Programming and Scripting

Shell argument

I need to create a Kash script that will read two arguments. So if the user enters anything but 2 arguments then they will get and error message. If they enter the two arguments then it will print them out in reverse order. Does anyone know how i can do this? (7 Replies)
Discussion started by: vthokiefan
7 Replies

8. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

9. UNIX for Dummies Questions & Answers

Shell script $0 argument

Hi, If not running a shell script file in current shell (. ./fileName) then $0 represents the executable file name. But in case of invoking shell script file in current shell then i m getting "$0 as -bash" . In such case how can i get the program name (running shell script file name)? Thanks, (2 Replies)
Discussion started by: painulyarun
2 Replies

10. Shell Programming and Scripting

port number as an argument

What is the script that takes a port number as parameter and displays status, whether it is available or is already used by other process pls help (1 Reply)
Discussion started by: saikiran
1 Replies
Login or Register to Ask a Question