shell报错:-bash: [: ==: 期待一元表达式 解决方法 ([: ==: unary operator expected)
shell报错:-bash: [: ==: 期待一元表达式 解决方法 ([: ==: unary operator expected)
问题背景:
- if [ $flag == '1' ]; then
- mode='--dev'
- else
- mode='--test'
- fi
如上代码, 执行shell报错:line 1: [: ==: unary operator expected【翻译过来就是:-bash: [: ==: 期待一元表达式】
解决方案:
- if [ "$flag" == '1' ]; then
- mode='--dev'
- else
- mode='--test'
- 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