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
