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的两种原因的更多相关文章

  1. shell 脚本之判断语句 if 详解

    使用 Linux 系统这么长时间,对 shell 脚本也算是比较熟悉.其实不管是搞开发,还是搞运维,shell 脚本都是必备的基本技能.这次抽时间好好总结一下 shell 方面的知识,综合的再学习一下 ...

  2. Jenkins在shell脚本运行docker权限报错解决

    报错环境 系统信息 Distributor ID: Ubuntu Description: Ubuntu 16.04.1 LTS Release: 16.04 Codename: xenial doc ...

  3. 【亲测有效】Centos安装完成docker后启动docker报错docker: unrecognized service的两种解决方案

    今天在学习Docker的时候 使用yum install docker安装完后启动不了,报错如下: [root@Sakura ~]# service docker start docker: unre ...

  4. Linux下安装Docker,报错docker: unrecognized service的两种解决方案

    转自(方法1):https://www.cnblogs.com/ECJTUACM-873284962/p/9362840.html

  5. Shell脚本中判断输入参数个数的方法投稿:junjie 字体:[增加 减小] 类型:转载

    Shell脚本中判断输入参数个数的方法 投稿:junjie 字体:[增加 减小] 类型:转载   这篇文章主要介绍了Shell脚本中判断输入参数个数的方法,使用内置变量$#即可实现判断输入了多少个参数 ...

  6. shell脚本中判断上一个命令是否执行成功

    shell脚本中判断上一个命令是否执行成功 shell中使用符号“$?”来显示上一条命令执行的返回值,如果为0则代表执行成功,其他表示失败.结合if-else语句实现判断上一个命令是否执行成功. 示例 ...

  7. shell脚本-循环选择语句

    shell脚本-循环选择语句 过程式编程语言: 顺序执行 选择执行 循环执行 注:条件中的变量,可以在执行语句中使用,不用在加上"$". if语句 根据命令的退出状态来执行命令 单 ...

  8. [shell]上一个命令执行完成,才执行下一个操作 | shell脚本中判断上一个命令是否执行成功

    shell脚本中判断上一个命令是否执行成功  shell中使用符号“$?”来显示上一条命令执行的返回值,如果为0则代表执行成功,其他表示失败.结合if-else语句实现判断上一个命令是否执行成功. 场 ...

  9. shell执行${var:m:n}报错Bad substitution解决办法

    Ubuntu系统下,执行字符串截取脚本时,总是报错:Bad substitution,脚本非常简单如下: #!/bin/sh str1="hello world!" :} 执行后报 ...

随机推荐

  1. flutter showModalBottomSheet max height

    static void showBuyServiceDialog(BuildContext context) { showModalBottomSheet( context: context, isS ...

  2. net.sf.json.JSONObject处理 "null" 字符串的一些坑

    转: net.sf.json.JSONObject处理 "null" 字符串的一些坑 2018年05月02日 16:41:25 大白能 阅读数:7026   版权声明:本文为博主原 ...

  3. Java生成三位随机数

    转: [转]Java生成三位随机数 public class Test2 { public static void main(String [] srgs) { int i=(int)(Math.ra ...

  4. [git]查看一个git项目的仓库位置

    项目文件夹的根目录下查找.git文件夹.打开config文件,里面的remote部分就是: [core] repositoryformatversion = 0 filemode = false ba ...

  5. 转: 动态加载、移除js、css文件

    function loadjscssfile(filename, filetype){ if (filetype=="js"){ var fileref=document.crea ...

  6. 将json文件转化为可视化

    利用python转化为可视化: python -mjson.tool

  7. yum安装etcd集群

       前一篇文章介绍了如何yum安装简单的kubernetes集群,其中etcd是单点部署.本篇我们来搭建etcd集群,方便日后搭建kubernetes HA集群架构. 1,环境配置说明 etcd1 ...

  8. 【HANA系列】SAP HANA LEFT/RIGHT字符串截取

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA LEFT/RI ...

  9. configparser模块,subprocess 模块,xlrd,xlwt ,xml 模块,面向对象

    1. configparser模块 2.subprocess 模块 3.xlrd,xlwt 4.xml 模块 5.面向对象 面向对象是什么? 是一种编程思想,指导你如何更好的编写代码 关注点在对象 具 ...

  10. URLOS开发基础教程——docker容器的使用方法

    URLOS本是基于docker容器运行,在入门URLOS开发之前,我们首先需要掌握docker的相关基础知识,本篇就以docker容器的基本使用方法为例,快速的让大家对docker有一个全面的印象. ...