如果参数存在的Bash测试 [英] Bash test if an argument exists
本文介绍了如果参数存在的Bash测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想测试一个扩充(如-h)被传递到我的bash脚本或没有。
I want to test if an augment (e.g. -h) was passed into my bash script or not.
在一个Ruby脚本,这将是:
In a Ruby script that would be:
#!/usr/bin/env ruby
puts "Has -h" if ARGV.include? "-h"
如何最好地做到这一点在Bash的?
How to best do that in Bash?
推荐答案
有小幅复杂。最快的方法是也靠不住:
It is modestly complex. The quickest way is also unreliable:
case "$*" in
(*-h*) echo "Has -h";;
esac
不幸的是,也将发现此命令,在这里
作为有 -h
通常你会使用 getopts的
解析为您所期望的参数:
Normally you'd use getopts
to parse for arguments that you expect:
while getopts habcf: opt
do
case "$opt" in
(h) echo "Has -h";;
([abc])
echo "Got -$opt";;
(f) echo "File: $OPTARG";;
esac
done
shift (($OPTIND - 1))
# General (non-option) arguments are now in "$@"
等
这篇关于如果参数存在的Bash测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文