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
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.
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
Summary: Hi all, Do anybody knows how to get of parameter in shell script. Like : In my program i have some parameters i was to redirect the output based on parameters. Thanks Raja ...
Summary: Hi Experts, I want to pass one input to a script. I tried the below command: echo "1" | script.sh arg1 arg2 means script.sh will take two arguments arg1 and arg2. 1 is the input to the script which i...
Summary: I need help on writing 2 shell scripts: 1) Write a shell script, to be called `keep', that will take any number of file names as arguments. It should remove all files from the current directory...