1. 程式人生 > 其它 >shell踩坑之未找到命令

shell踩坑之未找到命令

技術標籤:shell

在寫shell指令碼的時候一直報錯未找到命令,指令碼如下:

#!/bin/bash
in_date=$1
if [ ! $in_date ]; then
yesterday=`date -d "yesterday" '+%Y%m%d'`
yesterday1=`date -d "yesterday" '+%Y-%m-%d'`
else
yesterday=$in_date
yesterday1=`date -d "$in_date" '+%Y-%m-%d'`
fi

echo $yesterday
echo $yesterday1
time1="$yesterday1 00:00:00" echo $time1 res=999 if [ $res -le 1000 ] then echo $res warn1="警告:" $time1 "抽取資料量小於1000,請核實!" echo warn1 fi echo "警告:" $time1 "抽取" $var"條資料"

只看邏輯不看細節還真的找不到錯誤,在shell中=號兩邊沒有空格是表示賦值,有空格表示比較。而且中括號也要有空格。if條件中括號裡面兩邊要有空格,由於if條件語句中"warn1=“警告:” t i m e 2 " 抽 取 數 據 量 小 於 1000 , 請 核 實 ! " " 是 賦 值 操 作 , 所 以 time2"抽取資料量小於1000,請核實!""是賦值操作,所以

time2"1000,!"",time2左邊有空格是錯誤的,而"echo “警告:” $time2 “抽取” $var"條資料""不是賦值操作,只是輸出字串,所以不會報錯。
正確的shell指令碼應該是:

#!/bin/bash
in_date=$1
if [ ! $in_date ]; then
yesterday=`date -d "yesterday" '+%Y%m%d'`
yesterday1=`date -d "yesterday" '+%Y-%m-%d'
`
else yesterday=$in_date yesterday1=`date -d "$in_date" '+%Y-%m-%d'` fi echo $yesterday echo $yesterday1 time1="$yesterday1 00:00:00" echo $time1 res=999 if [ $res -le 1000 ] then echo $res warn1="警告:"$time1"抽取資料量小於1000,請核實!" echo warn1 fi echo "警告:" $time1 "抽取" $var"條資料"