I would create a bash script than parse like this:
test.sh -p (protocol) -i (address) -d (directory)
I need retrive the value after -p for example...
understand???
I hope...
thanks
I would create a bash script than parse like this:
test.sh -p (protocol) -i (address) -d (directory)
I need retrive the value after -p for example...
understand???
I hope...
thanks
#!/bin/bash
while getopts p:i:d: opt
do
case $opt in
p) protocol="$OPTARG";;
i) address="$OPTARG";;
d) directory="$OPTARG";;
?) bad=1;;
esac
done
echo "protcol -> $protocol" ;
echo "addres -> $address" ;
echo "directory -> $directory" ;
..........
..............
like this ?
# ./test.sh -p TCP -i 10.100.100.100 -d /tmp/
protcol -> TCP
addres -> 10.100.100.100
directory -> /tmp/
exactly!!!!
but, I don't understand completly... could you explain me more please???
thanks
./test.sh -p TCP -i 10.100.100.100 -d /tmp/
We use the getopts command for take the arguments..
OPTARG is the value of the last option argument processed by the getopts built-in command.
while loop goes for as the number of options (so its --> 3 ; p ,i and d ) , so while executed for three 3 times..
protocol , address and directory values assings to $OPTARG at the every case loop process..
HOW?
in the first while loop OPTARG equals to "TCP" and opt equals to p (so protocol value = "TCP" in the case loop)
second while loop OPTARG equals to "10.100.100.100" and opt equals to i ( address value = "10.100.100.100" in the case loop)
last while loop OPTARG equals to "/tmp/" and opt equals to d (directory value = "/tmp/" in the case loop)
regards
ygemici
thanks, but I test this script and the options must be exactly in this order... other then it's wrong result...
Can I turn over this implementation?
like:
test.sh -i 10.10.10.10 -p tcp -d /tmp
I don't know what you tested but it doesn't sound like the code he posted. It works perfectly fine in any order for me.
You can also roll your own easily enough for simple things:
#!/bin/sh
while [ "$#" -gt 0 ]
do
case "$1" in
-p) PROTOCOL=$2 ; shift ;;
-i) IP=$2 ; shift ;;
-d) DIR=$2 ; shift ;;
*) break ;;
esac
shift
done
sorry... I get mistake... now is ok, thanks