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

How do we create command aliases in a shell?

534


How do I run a bin bash script?

565


Which shell is the best?

568


What lives in a shell?

531


How do I run a powershell script?

569






What is bash eval?

541


What does $0 mean in shell script?

552


Can you write a script to portray how set –x works?

546


What is inside a seashell?

633


What is $1 in shell scripting?

585


How do I stop script errors?

571


How will you emulate wc –l using awk?

902


What is awk in shell scripting?

548


What are "c" and "b" permission fields of a file?

556


How does path variable work?

542