[Discuss] How to keep '-' in bash args
pw
p.willis at telus.net
Tue May 1 10:48:22 PDT 2007
Justin Rebelo wrote:
> pw wrote:
>> Can anyone tell me how to ensure (sh flags ??) that
>> bash doesn't strip '-' from the front end of
>> arguments passed to a shell script?
>>
>> ie: sh script.sh -myarg
>>
>> #!/bin/bash
>> #script.sh
>> echo "$1"
>>
>>
>> returns:
>> myarg instead of -myarg
>
> man bash
> search for "--"
>
On second thought/look the problem does not appear
to be what I thought it was since echoing back the
arguments by value (ie: echo $1 ; echo $2) produces '-'.
The problem seems to be with ${N} where N is the ordinal of the
argument.
ie:
for ARG in `seq 1 256`; do
echo "${ARG}";
done
Apparently this doesn't work very well with bash
and has become inconsistent with several other shells.
It appears that I should be using 'getopts' instead
of this.
ie:
#!/bin/bash
#test2.sh
while getopts .flag1:flag2:flag3:flagn. OPTION
case $OPTION in
flag1)
echo $OPTARG;
;;
flag2)
echo $OPTARG;
;;
flag3)
echo $OPTARG;
;;
flagn)
echo $OPTARG;
;;
?)
echo "bad arg";
esac
done
test2.sh -flag1 one -flag2 two -flag3 three -flagn en -flagz
outputs:
one
two
three
en
bad arg
Peter
More information about the Discuss
mailing list