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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What is shell geeksforgeeks?

757


What is scripting autism?

899


Is shell and terminal the same?

778


What is an inode block?

779


How will you connect to a database server from linux?

732


Explain about the exit command?

739


What is c in shell script?

790


What does egrep mean?

724


What is the purpose of scripting?

735


What is shell and shell script?

762


How to print all array elements and their respective indexes?

698


How to find duplicate record in file using shell script?

1048


How do we delete all blank lines in a file?

771


Set up a Sev 2 alert when the Primary WA service fails. A windows batch script needs to be created that will monitor the WA service on the Primary and when the service stops/fails a Sev 2 TT is generated for a particular team ?

1905


How to print pid of the current shell?

750