我正在尝试为多个输入数据文件制作脚本。最好的方法是什么,如何处理这些争论?脚本的用法应该是:
./script.sh -a sth -b sth - c sth -d sth input1 input2 input3
我可以使用 getopts 处理参数和参数,但我不知道如何处理这些输入文件,因为它们没有标志。 谢谢
最佳答案
while getopts ":a:b:c:d:" opt; do
case "$opt" in
a) i=$OPTARG ;;
b) j=$OPTARG ;;
c) k=$OPTARG ;;
d) l=$OPTARG ;;
esac
done
shift $(( OPTIND - 1 ))
for file in "$@"; do
# your stuff here
done
请尝试一下,这可能会解决您的问题
我自己的评论促使我扩展答案:
如果您想仅通过 getopts 执行此操作: 您必须将脚本调用为
./script -a hj -b op -c zx -d some -f“列表中的文件以空格分隔”
while getopts ":a:b:c:d:f:" opt; do
case "$opt" in
a) i=$OPTARG ;;
b) j=$OPTARG ;;
c) k=$OPTARG ;;
d) l=$OPTARG ;;
f) files=$OPTARG ;;
esac
done
#no shift is required now, since we have file list in $files
for file in $files; do
# your stuff here
done
关于bash - 如何使用 getopts 处理多个输入文件参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23360148/