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

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

问题描述

我正在做一些 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天全站免登陆