检查是否有一个bash脚本给至少有两个参数 [英] Check that there are at least two arguments given in a bash script
本文介绍了检查是否有一个bash脚本给至少有两个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想编写一个脚本,模拟CP那里有一个源和目的地输入。我怎么能算的命令行上给出的参数个数
I am trying to write a script that mimics cp where there is a source and destination input. how can I count the number of arguments given on the command line
例如:
./的MyScript SRC DEST
./myscript src dest
检查并确保至少两件事情给予。
check that at least 2 things were given.
推荐答案
使用 $#
特殊变量。它的值是参数的个数。所以,如果你只有一个包含脚本:
Use the $#
special variable. Its value is the number of arguments. So if you have a script that contains only:
echo $#
和执行它是这样的:
thatscript foo bar baz quux
它会打印4。
在你的情况,你可能想要做这样的事情:
In your case you may want to do something like:
if [ $# -ne 2 ]; then
# TODO: print usage
exit 1
fi
这篇关于检查是否有一个bash脚本给至少有两个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文