检查shell命令的参数是否存在
Check if the parameter of a shell command exists
在我的程序中,我必须检查用户输入的命令是否存在,如果存在,程序需要检查该命令的参数是否正确。
例如:
ls
(正确)
-al
(正确)
- 做手表
如果我这样做:
ls
(正确)
-kala
(不正确)
- 不做手表。
我该怎么做?这是我的脚本:
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
的工具,但我想你已经知道了。
在我的程序中,我必须检查用户输入的命令是否存在,如果存在,程序需要检查该命令的参数是否正确。
例如:
ls
(正确)-al
(正确)- 做手表
如果我这样做:
ls
(正确)-kala
(不正确)- 不做手表。
我该怎么做?这是我的脚本:
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
的工具,但我想你已经知道了。