gpt4 book ai didi

shell 脚本 : Define a variable within an if-statement

转载 作者:行者123 更新时间:2023-12-04 16:17:52 28 4
gpt4 key购买 nike

在 shell 程序中,我想在 if 语句中定义一个月份变量,如下所示。但我似乎无法在 if 语句中定义变量——我不断收到一条错误消息,提示“未找到命令‘dmonth’”。任何帮助都感激不尽!

    #Enter date:

echo "Enter close-out date of MONTHLY data (in the form mmdd): "
read usedate
echo " "

#Extract first two digits of "usedate" to get the month number:

dmonthn=${usedate:0:2}
echo "month number = ${dmonthn}"
echo " "

#Translate the numeric month identifier into first three letters of month:

if [ "$dmonthn" == "01" ]; then
dmonth = 'Jan'
elif [ "$dmonthn" == "02" ]; then
dmonth = "Feb"
elif [ "$dmonthn" == "03" ]; then
dmonth = "Mar"
elif [ "$dmonthn" == "04" ]; then
dmonth = "Apr"
elif [ "$dmonthn" == "05" ]; then
dmonth = "May"
elif [ "$dmonthn" == "06" ]; then
dmonth = "Jun"
elif [ "$dmonthn" == "07" ]; then
dmonth = "Jul"
elif [ "$dmonthn" == "08" ]; then
dmonth = "Aug"
elif [ "$dmonthn" == "09" ]; then
dmonth = "Sep"
elif [ "$dmonthn" == "10" ]; then
dmonth = "Oct"
elif [ "$dmonthn" == "11" ]; then
dmonth = "Nov"
else
dmonth = "Dec"
fi

echo dmonth

最佳答案

我认为您在使用空格时遇到了麻烦……它在 Bourne shell 中很重要,而且它是指令。 dmonth="Dec" 是一个赋值,其中 dmonth = "Dec" 是一个以 '=' 和 'Dec' 作为参数的命令。

关于 shell 脚本 : Define a variable within an if-statement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21051372/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com