如果参数等于此字符串,则定义一个类似于此字符串的变量 [英] if arguments is equal to this string, define a variable like this string

查看:111
本文介绍了如果参数等于此字符串,则定义一个类似于此字符串的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些bash脚本,现在我得到一个变量调用source和一个名为samples的数组,如下所示:

I am doing some bash script and now I got one variable call source and one array called samples, like this:

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 [ $1="country" ]; then
   source="country"
   samples="US Canada Mexico..."
else
   echo "try again"
fi

,但是当我运行脚本source countries.sh country时,它不起作用. 我在做什么错了?

but when I ran my script source countries.sh country it didn't work. What am I doing wrong?

推荐答案

不要忘记空格:

source=""
samples=("")
if [ $1 = "country" ]; then
   source="country"
   samples="US Canada Mexico..."
else
  echo "try again"
fi

这篇关于如果参数等于此字符串,则定义一个类似于此字符串的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆