Operating System - HP-UX
1753795 Members
6813 Online
108799 Solutions
New Discussion юеВ

what is command line arguments.. ??

 
SOLVED
Go to solution
shan_7
Advisor

what is command line arguments.. ??

Hi experts,

Could you tell me about comman line arugments while creating script. Then how set variable name like $0,$1,..$9,${10}..etc.
Atfer how to call this in program..

Explain in details.

Thanks In advance

Shankar

7 REPLIES 7
KapilRaj
Honored Contributor

Re: what is command line arguments.. ??

You need not set them it is done by the shell itself..

It is something like ..

useradd kapil

useradd command's $1 argument is "kapil" if u r to write a shell script for useradd i would write it as follows

=================
#!/usr/bin/sh
username=$1 # Storing the first command line argument on to another variable called username

/usr/bin/useradd $username
=================

Regds,

Kaps
Nothing is impossible
KapilRaj
Honored Contributor

Re: what is command line arguments.. ??

write a script for ur self and learn it

================
#!/usr/bin/sh
echo $0's first argument is $1
echo $0's second argument is $2
echo $0's third argument is $3
================
Save this script as script1.sh

chmod 755 script.sh
./script.sh kapil you yourgf

Now look at the output and you should be clear

Kaps
Nothing is impossible
Manish Srivastava
Trusted Contributor

Re: what is command line arguments.. ??

Hi,

Have a look at the ksh manpage:
Positional parameters, parameters denoted by a number, can be assigned
values with the set special command. Parameter $0 is set from
argument zero when the shell is invoked.


as explained by Kapil you can run his script and see the output. This mechanism of parameter passing holds true even in case of functions called in the shell script.

manish
Jeroen Peereboom
Honored Contributor

Re: what is command line arguments.. ??

An importrant detail: use double quotes to preserve spaces within arguments and variables (to prevent sh errors!).

tst:
#!/usr/bin/sh
# A=$1 will fail!!!
B="$1"
C="$2"
echo "First arg: $B"
echo "Second arg: $C"

Example: tst "1 2 3" two

JP
Bharat Katkar
Honored Contributor

Re: what is command line arguments.. ??

Hi shankar,
Simple way to analyse is create script as follows with name say "scr1" and let us assume you are passing 4 arguments to it:

# vi scr1

#!/usr/bin/sh
echo $0
echo $1
echo $2
echo $3
echo $4

:wq!

# chmod +x scr1
# ./scr1 one two three four

The output will be
scr1 one two three four

Now $0 is the command itself i.e. scr1 and $1 is the first argument, $2 second and so on.
Hope that helps.
Regards,



You need to know a lot to actually know how little you know
Muthukumar_5
Honored Contributor
Solution

Re: what is command line arguments.. ??

hai,

Command line arguments as like $1... ${N} can be used in the shell scripts,
as it is. By default $0 indicates in the shell script to denote it's filename, If we use the path name in the file execution as like

ksh /home/test/user.ksh 1 2 "hai bye test" "hello"

$0 is the /home/test/user.ksh

If we use the shell funtions,then $0 denotes the function name except test. If we use the funtion with test,it won't print the funtion name.

$# is used to give the number of command line arguments in the shell script execution.
$* is used to give the command line arguments as "$1 $2" like a string of all arguments
$@ is used to give the command line arguments as "$1" $2" .. "$N" like a individual strings
$! is used to get the running script's process ID
$? is used to get the return value of last executed shell command

See ksh man page for more details for command line arguments

Regards,
Muthukumar
Easy to suggest when don't know about the problem!
Sanjay Kumar Suri
Honored Contributor

Re: what is command line arguments.. ??

Dear Shankar

I suggest read page 50-52 of the follwing guide:

http://www.docs.hp.com/hpux/pdf/B2355-90046.pdf

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.