Computing.Net > Forums > Unix > Input parameters to shell script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Input parameters to shell script

Reply to Message Icon

Name: anilcgowda
Date: August 11, 2005 at 15:44:19 Pacific
OS: UNIX
CPU/Ram: 1GB
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: vicchai
Date: August 11, 2005 at 20:31:38 Pacific
Reply:

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 ...


0

Response Number 2
Name: nails
Date: August 11, 2005 at 20:58:43 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Input parameters to shell script

Process files using shell script www.computing.net/answers/unix/process-files-using-shell-script/3669.html

Giving an input value to a script www.computing.net/answers/unix/giving-an-input-value-to-a-script/8387.html

C Shell script help! www.computing.net/answers/unix/c-shell-script-help/3108.html