Input parameters to shell script

August 11, 2005 at 15:44:19
Specs: UNIX, 1GB

Hi,

I have a shell script in which I'm hardcoding all the directory paths and using them in the script.
Ex:
SourceDir=/opt/adw/source
FinalDir=/opt/adw/target
DataDir=..... like this.

Instead of hardcoding these directory paths, I want to send them as input parameters. Somebody suggested me to use getopts() function to parse for the input parameters. I don't have much idea on this. Please suggest a way to get the dir paths as parameters and how to use getopts() function to parse the input parameters.

Thanks,
Anil


See More: Input parameters to shell script

Report •


#1
August 11, 2005 at 20:31:38

Hi,

You can use question and answer type:
echo "Enter the source directory:"
read SourceDir
echo "Enter the final directory:"
read FinalDir

OR

By using arguments

SourceDir=$1
FinalDir=$2
and so on...

When you run this script, put all the directory as a argument, like this:
myscript /opt/adw/source /opt/adw/target
so the first argument is $1 and second argument is $2 and so on ...


Report •

#2
August 11, 2005 at 20:58:43

Here's a getopts example with no error checking ....

#!/bin/ksh

# execute as such
# script_name -s /optadw/source -d /opt/adw/target

while getopts s:d: option
do
case $option in
s)
sourcedir=$OPTARG
;;
d)
finaldir=$OPTARG
;;
esac
done

echo $sourcedir
echo $finaldir


Report •

Related Solutions


Ask Question