shell脚本if判断语句报错[: too many arguments的两种原因
shell脚本,if判断语句报错[: too many arguments
我遇到过两种情况:
1、第一中情况就是网上大家说的,字符串变量中可能存在空格,shell解析时将其认为是多个参数,再进行判断时,无法知道该获取哪个值,例如:
脚本ee.sh:
strings1="hello world"
strings2="hello world"
if [ $strings1 = $strings2 ]
then
echo "strings1 is same as strings2"
else
echo "strings1 is different from strings2"
fi
执行结果中报错:
ee.sh: line 3: [: too many arguments
strings1 is different from strings2
并自动执行了else分支中的语句。
而下面这段语句就不会这样:
脚本ee.sh:
strings1="hello world"
strings2="hello world"
if [ "$strings1" = "$strings2" ]
then
echo "strings1 is same as strings2"
else
echo "strings1 is different from strings2"
fi
执行结果:strings1 is same as strings2
2、第二种是我自己初步接触shell脚本,很多语法规则不太熟悉时遇到的,我将其记为——多重判断:
还是以上面的例子,将第二段稍微改一下:
脚本ee.sh:
strings1="hello world"
strings2="hello world"
if [ "$strings1" = "$strings2" -eq 1 ]
then
echo "strings1 is same as strings2"
else
echo "strings1 is different from strings2"
fi
执行结果:
ee.sh: line 3: [: too many arguments
strings1 is different from strings2
这里是按照常规的思路来写的,“ "$strings1" = "$strings2"先判断两个字符串是否相等,如果相等这个结果就是true,而true在一般语言中通常可以等价于1,因此又将这个结果跟1进行比较”,其实在这个逻辑中,我忽略了"$strings1" = "$strings2"本身就是个判断条件,执行结果就是true或false,而不需要再对这个结果判断它是否是1.
shell脚本if判断语句报错[: too many arguments的两种原因的更多相关文章
- shell 脚本之判断语句 if 详解
使用 Linux 系统这么长时间,对 shell 脚本也算是比较熟悉.其实不管是搞开发,还是搞运维,shell 脚本都是必备的基本技能.这次抽时间好好总结一下 shell 方面的知识,综合的再学习一下 ...
- Jenkins在shell脚本运行docker权限报错解决
报错环境 系统信息 Distributor ID: Ubuntu Description: Ubuntu 16.04.1 LTS Release: 16.04 Codename: xenial doc ...
- 【亲测有效】Centos安装完成docker后启动docker报错docker: unrecognized service的两种解决方案
今天在学习Docker的时候 使用yum install docker安装完后启动不了,报错如下: [root@Sakura ~]# service docker start docker: unre ...
- Linux下安装Docker,报错docker: unrecognized service的两种解决方案
转自(方法1):https://www.cnblogs.com/ECJTUACM-873284962/p/9362840.html
- Shell脚本中判断输入参数个数的方法投稿:junjie 字体:[增加 减小] 类型:转载
Shell脚本中判断输入参数个数的方法 投稿:junjie 字体:[增加 减小] 类型:转载 这篇文章主要介绍了Shell脚本中判断输入参数个数的方法,使用内置变量$#即可实现判断输入了多少个参数 ...
- shell脚本中判断上一个命令是否执行成功
shell脚本中判断上一个命令是否执行成功 shell中使用符号“$?”来显示上一条命令执行的返回值,如果为0则代表执行成功,其他表示失败.结合if-else语句实现判断上一个命令是否执行成功. 示例 ...
- shell脚本-循环选择语句
shell脚本-循环选择语句 过程式编程语言: 顺序执行 选择执行 循环执行 注:条件中的变量,可以在执行语句中使用,不用在加上"$". if语句 根据命令的退出状态来执行命令 单 ...
- [shell]上一个命令执行完成,才执行下一个操作 | shell脚本中判断上一个命令是否执行成功
shell脚本中判断上一个命令是否执行成功 shell中使用符号“$?”来显示上一条命令执行的返回值,如果为0则代表执行成功,其他表示失败.结合if-else语句实现判断上一个命令是否执行成功. 场 ...
- shell执行${var:m:n}报错Bad substitution解决办法
Ubuntu系统下,执行字符串截取脚本时,总是报错:Bad substitution,脚本非常简单如下: #!/bin/sh str1="hello world!" :} 执行后报 ...
随机推荐
- Helm chart仓库官方仓库不能使用解决方法
Helm chart仓库官方仓库不能使用解决方法 k8s中的官方helm chart仓库在国内可能使用不了,但是我们又需要使用,这里推荐几个方法. 使用其他的chart仓库 微软的chart仓库 ht ...
- WPF Slider Tickbar 中显示数值
class CustomTickBar : TickBar { protected override void OnRender(System.Windows.Media.DrawingContext ...
- 解决 ElementTree 无法处理中文
解决 ElementTree 无法处理中文,UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 76-99: o ...
- Ubuntu16.04使用apt安装完nginx常见问题
1.安装完并remove掉后重新install后没nginx.conf文件 解决办法: apt-get -y --purge remove nginx* apt-get -y autoremove a ...
- Delphi让控件随着窗口的大小而改变
Delphi让控件随着窗口的大小而改变方法1:设置Anchors属性,把akLeft,akTop,akRight,akBottom都设为True.方法2:设置Align属性,其值为alClient.
- git 参考手册-简明指南
很久没发文了,来头条以后更忙了,也没精力去分享一些有营养的内容了.这次分享的 git 的方方面面,基本来自于我的笔记.git 这东西算是为数不多每天都要用的东西了,但是我觉得也不至于从头至尾去了解他的 ...
- numpy的logspace产生等比数列
转载至:https://blog.csdn.net/shenpengjianke/article/details/29356755 上一篇介绍了numpy.linspace用于创建等差数列,现在介绍l ...
- 应用安全 - 社工 - 大数据 - Fofa - 汇总
搜索语法 title=”abc” header=”abc” body=”abc” domain=”xx.com” host=”.xx.cn” port=”443” ip=”1.1. ...
- python笔记之元祖
元祖创建使用圆括号括起来,中间元素使用逗号隔开 如:tuple1 = (1,2,3,4) tuple2 = () 空元祖 #!/usr/bin/env python #-*-coding:utf-8- ...
- Nginx网络负载均衡,负载均衡,网络负载,网络均衡
本节就聊聊采用Nginx负载均衡之后碰到的问题: Session问题 文件上传下载 通常解决服务器负载问题,都会通过多服务器分载来解决.常见的解决方案有: 网站入口通过分站链接负载(天空软件站,华军软 ...