getopts
以下のように引数を取得するスクリプトがあります
#!/bin/bash
while getopts ":d:l:f:o:" OPT;
do
echo 'In Test Script - Got Options '$OPT ' with' $OPTIND ' and ' $OPTARG
case $OPT in
d)
echo $OPTARG;;
f)
echo $OPTARG;;
l)
echo $OPTARG;;
?)
echo $OPTARG;;
esac
done
別のスクリプトで解析されてスクリプトに渡される引数を受け取り、getopts
単一のエントリに対しては正常に機能します。12345,-d somedesc -l somelabel
#!/bin/bash
INFO="12345,-d somedesc -l somelabel"
ID=`echo "$INFO" | awk -F "," "{ print $"1" }"`
OPTIONS=`echo "$INFO" | awk -F "," "{ print $"2" }"`
sh test.sh $OPTIONS
ただし、複数のエントリを受け取り、さらに引数を分割するために and を使用している場合、12345,-d somedesc -l somelabel:6789, -d anotherdesc -l anotherlabel
OPTIONSが正しく渡されても がトリガーされません。loop
awk
getopts
#!/bin/bash
INFO="12345,-d somedesc -l somelabel:6789, -d anotherdesc -l anotherlabel"
IFS=":"
set $INFO
echo 'Parsing INFO '$INFO
for item
do
echo 'Item is '$item
#parsing each item to separate id and options
ID=`echo "$item" | awk -F "," "{ print $"1" }"`
echo 'ID is '$ID
OPTIONS=`echo "$item" | awk -F "," "{ print $"2" }"`
echo 'Invoking Test Script with '$OPTIONS
sh test.sh $OPTIONS
done
getopts
OPTIONS を認識できない理由はありますか?