Shell argument


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell argument
# 1  
Old 08-30-2009
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?
# 2  
Old 08-30-2009
Code:
if [ $# -eq 2 ]; then
        echo $2 $1
else
        echo "Please introduce 2 arguments"
fi

# 3  
Old 08-30-2009
How would you also enter the script name after the arguments?
# 4  
Old 08-31-2009
./script arg1 arg2
# 5  
Old 08-31-2009
Quote:
Originally Posted by vthokiefan
script name
Code:
echo $0

# 6  
Old 08-31-2009
Code:
 
$ cat arg.sh
#!/bin/ksh
[ $# -ne 2 ] && echo "Usage ${0} input1 input2 " && exit 1;
echo $2 $1
 
$ ./arg.sh 1 2
2 1
 
$ ./arg.sh
Usage ./arg.sh input1 input2
$

# 7  
Old 08-31-2009
Sorry to be a pain again scripter.online.
The trailing semicolon is not needed and can cause some shells to misbehave.
In this context it is harmless.
I'm only being pedantic because you are building a useful resource.

Code:
#!/bin/ksh
[ $# -ne 2 ] && echo "Usage ${0} input1 input2 " && exit 1;
echo $2 $1

Personally I might use "&&" to test the success of a command when writing a dependent cron, but rarely in scripts.
However in Oracle scripting the semicolon is critical.

Last edited by methyl; 08-31-2009 at 06:19 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Strange third argument in shell function

In one of my shell function I found the following set_default_values () { prog=$1 PROC_DT=$2 RESET_ALL="${3-N}" #echo "Processing date as passed = , Program name = " ...... I understand the first and second arguments, but don't understand... (1 Reply)
Discussion started by: digioleg54
1 Replies

2. Shell Programming and Scripting

Pass file as one of argument in shell

Hi, Is there any way that we can pass one file as one of the argument in shell script ? (1 Reply)
Discussion started by: Selva_2507
1 Replies

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

4. Shell Programming and Scripting

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 #!/bin/sh if ; then echo greater exit 1; elif ; then echo less exit 1; fiit keeps returning me whatever number of argument I... (1 Reply)
Discussion started by: peuceul
1 Replies

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

6. Shell Programming and Scripting

Argument in shell script never blank

Hi All, I have a script which accepts a parameter which can either be blank, a specific value, or a wildcard value. But it never seems to be blank and the wildcard option seems to return the names of matching files in my directory. This happens even with the worlds simplest script that just... (1 Reply)
Discussion started by: cdines
1 Replies

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

8. Shell Programming and Scripting

printing last argument in shell script

All, I am having a shell script and i will pass different argument diferent time . Please tell me how can i find the last argument that i passsed each time when i exec the script. Thanks, Arun. (5 Replies)
Discussion started by: arunkumar_mca
5 Replies

9. Shell Programming and Scripting

Argument list too long - Shell error

Trying to tar specific files from a directory causes problems when the number of files is too large. ls ~/logs | wc -l 5928 In the logs directory - I have 5928 files If I want to include all files with today's date - I run the following command tar cf ~/archive/LoadLogs_20060302.tar... (8 Replies)
Discussion started by: dad5119
8 Replies

10. Shell Programming and Scripting

shell script argument parsing

how to parse the command line argument to look for '@' sign and the following with '.'. In my shell script one of the argument passed is email address. I want to parse this email address to look for correct format. rmjoe123@hotmail.com has '@' sign and followed by a '.' to be more... (1 Reply)
Discussion started by: rmjoe
1 Replies
Login or Register to Ask a Question