如何基于上一个命令比较[bash-script]获取下一个输入命令



我的代码是这样的

#!/bin/bash 
for i in $1 $2 $3 $4; do 
if [[("$i" == '-f')]] ; then #I'm searching if the user input the -f switch 
if [ -f "$((i+1))" ]; then # I'm trying to increment the position of $i to get the input that follows the -f switch and check the existence of the file 
echo "$((i+1)) file found" 
else 
echo "$((i+!)) file not found" 
fi 
fi 
done

我的问题是如何在我的情况-f的特定输入之后从用户那里获取输入。这可以在bash脚本中做。任何有关如何做的提示。谢谢。

您几乎应该使用getopts,但是您可以在迭代时设置标志:

#!/bin/bash
unset FLAG
for x; do
        if test -n "$FLAG"; then
                FILENAME=$x
                unset FLAG
                continue
        fi
        case $x in
        -f) FLAG=1;;  # Set flag for next iteration
        *) echo "processing argument $x"
        esac
done
echo "FILENAME=$FILENAME"

这非常脆弱,我感到有些内gui暗示。确实,使用getopts而不是尝试滚动自己的参数解析。

相关内容

最新更新