bash 脚本:如果参数等于这个字符串,定义一个像这个字符串一样的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9727695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash scripting: if arguments is equal to this string, define a variable like this string
提问by Alejandro
I am doing some bash script and now I got one variable call source
and one array called samples
, like this:
我正在做一些 bash 脚本,现在我得到了一个变量调用source
和一个名为 的数组samples
,如下所示:
source='country'
samples=(US Canada Mexico...)
as I want to expand the number of sources (and each source has its own samples) I tried to add some arguments to do this. I tried this:
因为我想扩大来源的数量(并且每个来源都有自己的样本),所以我尝试添加一些参数来做到这一点。我试过这个:
source=""
samples=("")
if [ ="country" ]; then
source="country"
samples="US Canada Mexico..."
else
echo "try again"
fi
but when I ran my script source countries.sh country
it didn't work.
What am I doing wrong?
但是当我运行我的脚本时source countries.sh country
它不起作用。我究竟做错了什么?
回答by Alex L
Don't forget about spaces:
不要忘记空格:
source=""
samples=("")
if [ = "country" ]; then
source="country"
samples="US Canada Mexico..."
else
echo "try again"
fi
回答by Vyke
You can use either "=" or "==" operators for string comparison in bash. The important factor is the spacing within the brackets. The proper method is for brackets to contain spacing within, and operators to contain spacing around. In some instances different combinations work; however, the following is intended to be a universal example.
您可以在 bash 中使用“=”或“==”运算符进行字符串比较。重要的因素是括号内的间距。正确的方法是括号内包含间距,操作符包含周围间距。在某些情况下,不同的组合起作用;但是,以下内容旨在作为通用示例。
if [ "" == "something" ]; then ## GOOD
if [ "" = "something" ]; then ## GOOD
if [ ""="something" ]; then ## BAD (operator spacing)
if ["" == "something"]; then ## BAD (bracket spacing)
Also, note double brackets are handled slightly differently compared to single brackets ...
另外,请注意,与单括号相比,双括号的处理方式略有不同......
if [[ $a == z* ]]; then # True if $a starts with a "z" (pattern matching).
if [[ $a == "z*" ]]; then # True if $a is equal to z* (literal matching).
if [ $a == z* ]; then # File globbing and word splitting take place.
if [ "$a" == "z*" ]; then # True if $a is equal to z* (literal matching).
I hope that helps!
我希望这有帮助!
回答by ioneyed
It seems that you are looking to parse commandline arguments into your bash script. I have searched for this recently myself. I came across the following which I think will assist you in parsing the arguments:
您似乎希望将命令行参数解析到 bash 脚本中。我最近自己搜索了这个。我遇到了以下我认为可以帮助您解析参数的内容:
http://rsalveti.wordpress.com/2007/04/03/bash-parsing-arguments-with-getopts/
http://rsalveti.wordpress.com/2007/04/03/bash-parsing-arguments-with-getopts/
I added the snippet below as a tl;dr
我将下面的代码段添加为 tl;dr
#using : after a switch variable means it requires some input (ie, t: requires something after t to validate while h requires nothing.
while getopts “ht:r:p:v” OPTION
do
case $OPTION in
h)
usage
exit 1
;;
t)
TEST=$OPTARG
;;
r)
SERVER=$OPTARG
;;
p)
PASSWD=$OPTARG
;;
v)
VERBOSE=1
;;
?)
usage
exit
;;
esac
done
if [[ -z $TEST ]] || [[ -z $SERVER ]] || [[ -z $PASSWD ]]
then
usage
exit 1
fi
./script.sh -t test -r server -p password -v
./script.sh -t 测试 -r 服务器 -p 密码 -v