How do you read arguments in a shell program - $1, $2 ..?

Answers were Sorted based on User's Feedback



How do you read arguments in a shell program - $1, $2 ..?..

Answer / madhavi

to read command line argments we hav to use echo $1
$2........whre $0-gives name of program,$#-gives number of
arguments passed and $* gives all the arguments.

Is This Answer Correct ?    8 Yes 0 No

How do you read arguments in a shell program - $1, $2 ..?..

Answer / guest

read command

Is This Answer Correct ?    2 Yes 0 No

How do you read arguments in a shell program - $1, $2 ..?..

Answer / seshadri sethi

Shell script accepts parameters in following format…
$1 would be the first command line argument, $2 the second,
and so on
$0 is the name of the script or function

If your script has more than 9 params then accept in
following way…
${12} : 12th param
${18} : 18th param

Is This Answer Correct ?    2 Yes 1 No

How do you read arguments in a shell program - $1, $2 ..?..

Answer / pitambar mishra

### Script for greater between 2 numbers
### name : greater_of_2_numbers.ksh

#! /bin/ksh

### If you won't supply 2 numbers then it will prompt you to 2 numbers. ###

if [ $# -ne 2 ] ($# : number of positional parameters)
then
echo "Usage: $0 <Enter 2 numbers>" ($0 : name of script)
exit 1
fi

### main script
if [ $1 -gt $2 ] ($1 and $2 : Two positional parameters)
then
echo "$1 is greater than $2"
elif [ $2 -gt $1 ]
then
echo "$2 is greater than $1"
else
echo "Two numbers are equal"
fi

To execute :
ksh greater_of_2_numbers.ksh 5 4

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Shell Script Interview Questions

one folder contains lot of students name but I want to fetch hello with every student name individually using shell script

0 Answers  


What is shell and shell script?

0 Answers  


How to print all the arguments provided to the script?

0 Answers  


How do I run a script from command prompt?

0 Answers  


What does .sh file contain?

0 Answers  


How to print some text on to the screen?

2 Answers  


What is the difference between a shell variable that is exported and the one that is not exported?

6 Answers  


In my bash shell I want my prompt to be of format '$"present working directory":"hostname"> and load a file containing a list of user-defined functions as soon as I log in, how will you automate this?

0 Answers  


What is the difference between running a script as ./scriptname.sh and sh scriptname.sh

1 Answers  


In a file , how to retrieve the lines which are the multiples of 50 ? like 50,100,150th lines etc.

9 Answers   Amazon,


How to print pid of the current shell?

0 Answers  


Why do we use shell scripting?

0 Answers  


Categories