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

......

#!/bin/bash
#
#Name:del_mr_temp_file
#Date:2018-09-11
#Author:Created by shiminhua
#Company:Datang Mobile Co., Ltd
#Discription:This script delete old mr temp files.
########################################################################## ###########################################################################
if [ -f ~/.bash_profile ];then
. ~/.bash_profile
fi
##########################################################################
MRDIR="/export/home/omcrftp/" MRFILE1="${MRDIR}mrfile"
MRFILE2="${MRDIR}mrfile/success"
########################debugprint################################
debugswitch=0
sleepswitch=0
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 #这里应改为[[]] return 0
fi curtime=`date +%Y-%m-%d\ %H:%M:%S`
debugprint "Enter func:sleep_when_quarter, time is: $curtime" while [ 1 -eq 1 ]
do
currentminute=`date +%M` if [ $currentminute -gt 22 ] && [ $currentminute -lt 27 ]; then
break # break表示跳出死循环,执行后面的代码,否则一直在死循环中
fi if [ $currentminute -gt 37 ] && [ $currentminute -lt 42 ]; then
break
fi if [ $currentminute -gt 52 ] && [ $currentminute -lt 57 ]; then
break
fi debugprint "sleep...."
sleep $timeseconds done debugprint "Leave func:sleep_when_quarter."
}
########################del_old_files################################ del_old_files()
{
debugprint "dir = $1" for file_a in ${1}/*; do sleep_when_quarter temp_file1=`basename $file_a` if [ -f $file_a ]; then reserver=`date +%Y%m%d%H` debugprint "filename is [$temp_file1]" if [[ $temp_file1 != *${reserver}* ]]; then
debugprint "delete filename is [$temp_file1]"
rm -f $file_a >/dev/null 2>&1 #丢弃 标准、错误输出 真正起作用的删除命令
fi
fi
done
} ########################main################################
WDNAME=del_mr_temp_file.sh
####################declare var end####################################### if [ "x$1" = "x" ];then
debugswitch=0
else
debugswitch=$1 fi if [ "x$2" = "x" ];then
sleepswitch=0
else
sleepswitch=$2
fi PID=$$
WD1=`ps -ef|grep "$WDNAME"|grep -v grep|wc -l`    #grep -v grep 就是查找不含有 grep 字段的行
WD2=`ps -ef|grep "$WDNAME"|grep -v grep|grep $PID|wc -l`
echo "WD1=$WD1"
echo "WD2=$WD2"
echo "PID=$PID" # 这个判断的意义是,如果脚本已经运行,则不再运行第二次。避免脚本运行多次,产生多个死循环,影响系统速度
if [ ! "$WD1" -eq "$WD2" ]; then
echo "The script is live. please type Enter to exit!"
echo "`ps -ef|grep "$WDNAME"|grep -v grep`"
exit 0
fi while [ 1 -eq 1 ]
do
del_old_files $MRFILE1 del_old_files $MRFILE2 sleep 300 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. C#三种常用的读取XML文件的方法

    下面我将介绍三种常用的读取XML文件的方法.分别是 1: 使用 XmlDocument 2: 使用 XmlTextReader 3: 使用 Linq to Xml 这里我先创建一个XML文件,名为Bo ...

  2. flutter 屏幕宽高 状态栏高度

    MediaQuery.of(context) 包含了一些屏幕的属性: size : 一个包含宽度和高度的对象,单位是dp print(MediaQuery.of(context).size); //输 ...

  3. mysql的逻辑架构

    架构图 做Java开发时,项目一般会分为数据访问层.业务逻辑层.控制层等,每层处理不同的任务.类似的,mysql也不是单一的模块,其内部也分为几层.自己不会画,从网上找来了经典的mysql架构图: 分 ...

  4. rocketMQ retry 消息的实现

    consumer 消费失败,会把消息重新发往 %RETRY% + consumerGroup,这个 retry 消息会在一定时间后,真实送到 retry topic. broker 处理发送到 ret ...

  5. 阶段3 2.Spring_10.Spring中事务控制_2 作业-基于注解的AOP实现事务控制及问题分析_上

    创建maven的新项目 先复制坐标的依赖. 再把代码复制进来 先改造ioc的部分 复制上面一行代码.到下面 改成context 这里也是复制的上面两行代码.到下面改成context关键字 配置扫描的包 ...

  6. 【ABAP系列】SAP BOM反查

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP BOM反查   前言部分 ...

  7. Metinfo3.0 /include/common.inc.php PHP代码注入

  8. python 并发编程 异步IO模型

    异步IO(Asynchronous I/O) Linux下的asynchronous IO其实用得不多,从内核2.6版本才开始引入.先看一下它的流程: 用户进程发起read操作之后,立刻就可以开始去做 ...

  9. MyBatis Mapper XML 详解

    MyBatis Mapper XML 详解 MyBatis 真正的力量是在映射语句中.这里是奇迹发生的地方.对于所有的力量,SQL 映射的 XML 文件是相当的简单.当然如果你将它们和对等功能的 JD ...

  10. Forsaken喜欢数论

    链接:https://ac.nowcoder.com/acm/contest/1221/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语言1048 ...