【发布时间】:2016-03-10 03:34:45
【问题描述】:
这是我最简单形式的整个脚本。
#!/bin/bash
src=""
targ=${PWD}
while getopts "s:t:" opt; do
case $opt in
s)
src=$OPTARG
;;
t)
targ=$OPTARG
;;
esac
shift $((OPTIND-1))
done
echo "Source: $src"
echo "Target: $targ"
我以getopts_test -s a -t b 运行此脚本
但是,它总是在Target: 前面打印pwd 而从不打印b
我在这里错过了什么?
【问题讨论】:
-
尝试将
shift移出while 循环:将其移至done之后 -
当您在循环内进行移位时,您会从参数列表中删除
-s和a参数,但getopts仍然认为它处理了参数 1 和 2(现在包含 @ 987654332@ 和b),所以它看起来超越了它们,并说“无事可做”。 -
知道了。感谢您的详细解释