检查shell命令的参数是否存在

Check if the parameter of a shell command exists

在我的程序中,我必须检查用户输入的命令是否存在,如果存在,程序需要检查该命令的参数是否正确。

例如:

如果我这样做:

我该怎么做?这是我的脚本:

while true
do
echo "Insert the command"
read comm
if [ "$(type -t $comm)" != "" ]; then
    echo "Insert the parameters of the command ";
    read par;
    echo "Insert the time of watch";
    read time;
    if [ $t -le 0 ]; then
        echo "Value not correct";
    else     
        clear;
        while true
        do
            echo "$comm"
            date
            echo ""
            $comm $par
            sleep $((time))
            clear
       done
    fi;
    else
        echo "Command not found, retry.";
        echo "";
    fi
done

您可以将命令调用替换为:

if ! $comm $par; then
    exit 1
fi

使其在出错后停止。也有一个名为 watch 的工具,但我想你已经知道了。