使用 if 语句调用变量给出 -bash: 'Command not find'

使用 if 语句调用变量给出 -bash: 'Command not find'

我正在修改我的.bash_profile文件,以便当我运行 ipy 时,它会每七天或更多天清除一次日志文件。问题是,每当我在 if 语句中引用变量时,我都会返回-bash: command not found

  1 #!/bin/sh
  2 # .bash_profile
  ...
 15 # These lines clear the ipy.log every seven or more days
 16 PREV=$(awk '/./{line=$0} END{print line}' days.log)
 17 echo $PREV
 18 WEEKDAY=$(date '+%Y%m%d')
 19 echo $WEEKDAY
 20 DIF=$((WEEKDAY-PREV))
 21 MARKER=false
 22 if [ ${DIF} > 6 ]; then
 23     YEAR=$(date '+%Y')
 24     MON=$(date '+%m')
 25     DAY=$(date '+%d')
 26     DIGIT=${DAY:-1}
 27     if [ ${DIGIT} -eq 1 ]; then
 28         TAG='st'
 29     elif [ ${DIGIT} -eq 2 ]; then
 30         TAG='nd'
 31     elif [ ${DIGIT} -eq 3 ]; then
 32         TAG='rd'
 33     else
 34         TAG='th'
 35     fi
 36     TIMESTAMP=$(date '+%H:%M:%S')
 37     MARKER=true
 38     PRINT=$("ipy.log was cleared at ${TIMESTAMP} on ${WEEKDAY} the ${DAY}${TAG}  of  ${MON},  ${YEAR}")
 39     echo ${PRINT}
 40     ${PRINT} > ~/ipy.log
 41     ${WEEKDAY} > ~/days.log
 42 else
 43     echo "It has been ${DIF} days since your last write, no need to clear logs"
 44 fi

输出:


20200216
-bash: ipy.log was cleared at 14:35:45 on 20200216 the 16th  of  02,  2020: command not found

-bash: 20200216: command not found

您会注意到,当我调用文件的最后一行days.log并将其分配给变量时$PREV,它只会分配给 null ,但这是我将在另一篇文章中正式提出的问题。我想弄清楚如何以及为何command not found提出这些言论。

答案1

在第 15 行中,您:

WEEKDAY=$(date '+%Y%m%d')

所以 WEEKDAY == "20200216" 或类似的。在第 41 行中,您:

${WEEKDAY} > ~/days.log

它将替换${WEEKDAY}为“20200216”。在 bash 中,您给出的第一个字符串始终是一个命令(除了:关键字、赋值和其他内容,但您可以暂时忘记这些)。所以bash会尝试执行:

20200216

这是不存在的。你可能想写:

echo "${WEEKDAY}" > ~/days.log

反而。

答案2

由于您在第 20 行“DIF=$((WEEKDAY-PREV))”中使用算术运算,我认为最好的方法是第 22 行读取:

22 if [ ${DIF} -gt 6 ]; then

另外,正如 Tomáš Pospíšek 之前所说,第 40 行和第 41 行需要在变量前面有一个命令,因此应该读取

40 echo "${PRINT}" > ~/ipy.log
41 echo "${WEEKDAY}" > ~/days.log

答案3

下面这行没有任何意义:

PRINT=$("ipy.log was cleared at ${TIMESTAMP} on ${WEEKDAY} the ${DAY}${TAG}  of  ${MON},  ${YEAR}")

语法$(...)是为了命令替换,因此bash尝试执行:

ipy.log was cleared at ${TIMESTAMP} on ${WEEKDAY} the ${DAY}${TAG}  of  ${MON},  ${YEAR}

作为命令。

相反,您可以:

  • 将变量设置PRINT为字符串:
PRINT="ipy.log was cleared at ${TIMESTAMP} on ${WEEKDAY} the ${DAY}${TAG}  of  ${MON},  ${YEAR}"
  • echo字符串到终端:
echo "$PRINT"
  • echo字符串到文件:
echo "$PRINT" > ~/ipy.log

由于以下错误,分配给的文件PREV未按预期工作,因为~/days.log文件已存在但为空:

${WEEKDAY} > ~/days.log

这应该是:

echo "$WEEKDAY" > ~/days.log

这是一个稍微不同的方法:

# This function clears ipy.log every seven or more days
function clear_ipylog() {
    typeset -i prev yearmday dif
    typeset tag timedate message
    typeset -g marker
#     read -r prev <"$HOME/days.log" # Should work instead of awk as days.log should only contain '%Y%m%d'
    prev=$(awk '/./{line=$0} END{print line}' "$HOME/days.log")
    echo $prev >&2
    yearmday=$(date '+%Y%m%d')
    echo $yearmday >&2
    dif=$((yearmday-prev))
    if [ $dif -gt 6 ]; then
        case ${yearmday:6:2} in
            (01) tag='st' ;;
            (02) tag='nd' ;;
            (03) tag='rd' ;;
            (*) tag='th' ;;
        esac
        timedate=$(date '+%H:%M:%S %F%b')
        message=$(printf '%s %d%s\n' "ipy.log was cleared at ${timedate:0:19}, the" \
            "${yearmday:6:2}" "$tag of ${timedate:19} ${yearmday:0:4}")
        echo "$message" > "$HOME/ipy.log"
        echo "$message" >&2
        echo $yearmday > "$HOME/days.log"
        marker='true'
    else
        echo "It has been $dif days since your last write, no need to clear logs"
        marker='false'
    fi
}
clear_ipylog
  • 这是在一个函数中隔离一些变量 - 我假设你不希望将$prev$dif等设置为全局变量
  • 不确定它的$marker用途,所以假设您希望将其设置为全局变量
  • ~/.bash_profile~/.bashrc直接读取,bash因此不应以该行开头#!/bin/sh或设置可执行模式
  • 您可能需要考虑~/days.log通过更改第一行的格式来使其过时,~/ipy.log以便prev可以从第一行/字段中获取ipy.log

相关内容