shell报错:-bash: [: ==: 期待一元表达式 解决方法 ([: ==: unary operator expected)

blogdaren 2015-02-26  抢沙发 14916人次

问题背景:

  1. if [ $flag == '1' ]; then
  2. mode='--dev'
  3. else
  4. mode='--test'
  5. fi

如上代码, 执行shell报错:line 1: [: ==: unary operator expected【翻译过来就是:-bash: [: ==: 期待一元表达式】

解决方案:

  1. if [ "$flag" == '1' ]; then
  2. mode='--dev'
  3. else
  4. mode='--test'
  5. fi

问题说明:

1. 当 if 语句中使用 [ ] 条件修饰符时, $flag 变量必须加上引号。

2. 当 if 语句中使用 [[]] 条件修饰符时,$flag 变量的引号可有可无。

我通过第二种方法解决:

debugprint()
{
if [[ $debugswitch -eq 1 ]]; then
echo "$1"
else
echo "debug off" > /dev/null
fi
}
########################sleep_when_quarter################################
sleep_when_quarter()
{
local currentminute
local timeseconds=10
local curtime

if [[ $sleepswitch -eq 1 ]]; then

......

  1. #!/bin/bash
  2. #
  3. #Name:del_mr_temp_file
  4. #Date:2018-09-11
  5. #Author:Created by shiminhua
  6. #Company:Datang Mobile Co., Ltd
  7. #Discription:This script delete old mr temp files.
  8. ##########################################################################
  9.  
  10. ###########################################################################
  11. if [ -f ~/.bash_profile ];then
  12. . ~/.bash_profile
  13. fi
  14. ##########################################################################
  15. MRDIR="/export/home/omcrftp/"
  16.  
  17. MRFILE1="${MRDIR}mrfile"
  18. MRFILE2="${MRDIR}mrfile/success"
  19. ########################debugprint################################
  20. debugswitch=0
  21. sleepswitch=0
  22. debugprint()
  23. {
  24. if [ $debugswitch -eq 1 ]; then #这里应改为[[]]
  25. echo "$1"
  26. else
  27. echo "debug off" > /dev/null
  28. fi
  29. }
  30. ########################sleep_when_quarter################################
  31. sleep_when_quarter()
  32. {
  33. local currentminute
  34. local timeseconds=10
  35. local curtime
  36.  
  37. if [ $sleepswitch -eq 1 ]; then #这里应改为[[]]
  38.  
  39. return 0
  40. fi
  41.  
  42. curtime=`date +%Y-%m-%d\ %H:%M:%S`
  43. debugprint "Enter func:sleep_when_quarter, time is: $curtime"
  44.  
  45. while [ 1 -eq 1 ]
  46. do
  47. currentminute=`date +%M`
  48.  
  49. if [ $currentminute -gt 22 ] && [ $currentminute -lt 27 ]; then
  50. break # break表示跳出死循环,执行后面的代码,否则一直在死循环中
  51. fi
  52.  
  53. if [ $currentminute -gt 37 ] && [ $currentminute -lt 42 ]; then
  54. break
  55. fi
  56.  
  57. if [ $currentminute -gt 52 ] && [ $currentminute -lt 57 ]; then
  58. break
  59. fi
  60.  
  61. debugprint "sleep...."
  62. sleep $timeseconds
  63.  
  64. done
  65.  
  66. debugprint "Leave func:sleep_when_quarter."
  67. }
  68. ########################del_old_files################################
  69.  
  70. del_old_files()
  71. {
  72. debugprint "dir = $1"
  73.  
  74. for file_a in ${1}/*; do
  75.  
  76. sleep_when_quarter
  77.  
  78. temp_file1=`basename $file_a`
  79.  
  80. if [ -f $file_a ]; then
  81.  
  82. reserver=`date +%Y%m%d%H`
  83.  
  84. debugprint "filename is [$temp_file1]"
  85.  
  86. if [[ $temp_file1 != *${reserver}* ]]; then
  87. debugprint "delete filename is [$temp_file1]"
  88. rm -f $file_a >/dev/null 2>&1 #丢弃 标准、错误输出 真正起作用的删除命令
  89. fi
  90. fi
  91. done
  92. }
  93.  
  94. ########################main################################
  95. WDNAME=del_mr_temp_file.sh
  96. ####################declare var end#######################################
  97.  
  98. if [ "x$1" = "x" ];then
  99. debugswitch=0
  100. else
  101. debugswitch=$1
  102.  
  103. fi
  104.  
  105. if [ "x$2" = "x" ];then
  106. sleepswitch=0
  107. else
  108. sleepswitch=$2
  109. fi
  110.  
  111. PID=$$
  112. WD1=`ps -ef|grep "$WDNAME"|grep -v grep|wc -l`    #grep -v grep 就是查找不含有 grep 字段的行
  113. WD2=`ps -ef|grep "$WDNAME"|grep -v grep|grep $PID|wc -l`
  114. echo "WD1=$WD1"
  115. echo "WD2=$WD2"
  116. echo "PID=$PID"
  117.  
  118. # 这个判断的意义是,如果脚本已经运行,则不再运行第二次。避免脚本运行多次,产生多个死循环,影响系统速度
  119. if [ ! "$WD1" -eq "$WD2" ]; then
  120. echo "The script is live. please type Enter to exit!"
  121. echo "`ps -ef|grep "$WDNAME"|grep -v grep`"
  122. exit 0
  123. fi
  124.  
  125. while [ 1 -eq 1 ]
  126. do
  127. del_old_files $MRFILE1
  128.  
  129. del_old_files $MRFILE2
  130.  
  131. sleep 300
  132.  
  133. done

  

参考:

shell中>/dev/null 2>&1

https://www.cnblogs.com/520playboy/p/6275022.html

linux应用之test命令详细解析

https://www.cnblogs.com/tankblog/p/6160808.html

shell报错:-bash: [: ==: 期待一元表达式 解决方法 ([: ==: unary operator expected)

http://www.blogdaren.com/post-2189.html

Bash脚本的空格和“期待一元表达式”错误

https://blog.csdn.net/qinxiandiqi/article/details/41626215

grep -v grep反向查找(查找不含有 grep 字段的行)

https://blog.csdn.net/weixin_36667844/article/details/78999489

shell报错:-bash: [: ==: 期待一元表达式 解决方法 ([: ==: unary operator expected)的更多相关文章

  1. vs2010一运行就报错deven.exe assert failure 解决方法,卸载系统中.netFramework最新版本的(简体中文)

    vs2010一运行就报错deven.exe assert failure 解决方法,卸载系统中.netFramework最新版本的(简体中文)

  2. AFNetwork2.0在报错1016,3840的解决方法及一些感悟

    最近在学习AFNetwork,非常好的网络框架,能节省很多时间.不过请求网络数据时报错1016,3840. 这两个错误网上解决方法很多,http://blog.csdn.net/huifeidexin ...

  3. 阿里云安装kubernetes-UI报错endpoints \"kubernetes-dashboard\" not found解决方法

    问题:阿里云ECS安装kube-ui v5后,访问 http://master_ip:8080/ui/跳转到http://master_ip:8080/api/v1/proxy/namespaces/ ...

  4. 转)VCSA 6.5重启无法访问,报错“503 Service Unavailable”的解决方法

    1. 问题 重启vcenter,登陆vsphere client,提示 “503 Service Unavailable (Failed to connect to endpoint: [N7Vmac ...

  5. ExtJs6编译之后上线报错无法查看到的解决方法

    最近Extjs编译后部署遇到了一个错误c is not a constructor,报错位置在app.js里,这根本没法找 解决方法:用命令sencha app build testing 编译之后, ...

  6. Arcgis Javascript中geometryEngine报错’hq‘of undefined的解决方法

    这个问题困扰了我一个星期,原因是使用geomagicbuffer时候,有的线可正常使用,有的就直接报错,一直没有解决,后来发现是api自己的bug导致的 干脆直接读代码,在geometryEngine ...

  7. 网页视频不能自动播放?HTML5 video报错Uncaught (in promise) DOMException解决方法

    话说发哥四年前写了一个网页,如上图效果,实际网址http://pano.z01.com ,话说做好时是正常的,突然某一天,客户说你这个网站动画不见了,这是什么原因? 结果检查脚本一切正常. 其实也不是 ...

  8. Github上LeakCanary编译报错CreateProcess error=2的解决方法

    现象说明: 从github上拉下LeakCanary编译时报错 CreateProcess error=2, ϵͳÕҲ»µ½ָ¶ 原因分析: 该现象是由于Windows中Gradle调用命令未加cmd ...

  9. Eclipse导入web项目报错找不到HttpServletRequest解决方法

    解决方法 右击项目-> Properties -> Java Build Path -> Add Library -> Server Runtime -> next -& ...

随机推荐

  1. HTML+CSS实现导航栏二级下拉菜单完整代码

    工具是vs code 代码如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  2. GO开发:链表

    链表 type Student struct { Name string Next* Student } 每个节点包含下一个节点的地址,这样把所有的节点串起来了,通常把链表中的第一个节点叫做链表头 p ...

  3. 阶段3 3.SpringMVC·_02.参数绑定及自定义类型转换_2 请求参数绑定实体类型

    参数封装到javaBean对象中 创建新的包domain.在下面新建Account 实现序列化 的接口,定义几个属性 生成get和set.还有toString的方法 表单 重新发布tomcat jav ...

  4. python3.5 append使用

    1.从元组中添加 friends=[] tup1=("Jon",35) friends.append(tup1[0]) print(friends[0]) ssh://root@1 ...

  5. golang init方法和main方法初始化顺序

    init()和main()方法是golang默认的两个方法,不需要我们调用,程序执行会自动寻找项目中的这俩方法.现在我们就讲一种通用的情况:main 包下 导入了 init2 包而在init2 包下又 ...

  6. Office_Word使用技巧大全(超全)

    目录 不收藏不行的 word 使用技巧大全 三招去掉页眉那条横线 批量转换全角字符为半角字符 快速打开最后编辑的文档 格式刷的使用 删除网上 下载 资料的换行符(象这种 "↓" ) ...

  7. HDU 1263 水果 (STL map)

    水果 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

  8. [转帖]虚拟内存探究 -- 第二篇:Python 字节

    虚拟内存探究 -- 第二篇:Python 字节 http://blog.coderhuo.tech/2017/10/15/Virtual_Memory_python_bytes/ 是真看不懂哦     ...

  9. Java实现龟兔赛跑

    闲极无聊,加上翻手机看到龟兔赛跑的词语,想到了可以通过java起两个线程来实现龟兔赛跑的实现. 代码实现其实很简单: 首先是乌龟类: 然后是兔子类: 最后是赛跑类: 接下里让我们看一下输出结果吧: 乌 ...

  10. 洛谷 P1273 有线电视网 题解

    题面 按照常见树形背包定义状态:设dp[u][j]表示在以u为根的子树中,选择j个客户所能获得的最大收益. 状态转移:dp[u][j]=max(dp[u][j-k],dp[v][k]-w(u,v)); ...