Kul*_*mar 2 command-line scripts
我尝试像12DEC2013bhav.csv.zip
下面的脚本一样创建输出。
但它给了我类似的东西12DEC.csv.zip
:
for (( i = 2013; i <= 2014; i++ ))
do
for m in JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
do
for (( d = 1; d <= 31; d++))
do
echo "$d$m$ibhav.csv.zip"
done
done
done
Run Code Online (Sandbox Code Playgroud)
我该如何纠正?
问题是您试图引用一个名为$ibhav
(not $i
)的变量。
变量可以是多个字符,在您的示例中,shell 无法判断您的意思是$i
or $ibhav
(或$ibha
or$ibh
或$ib
)。
解决方法是将您的变量名括起来:
echo "${d}${m}${i}bhav.csv.zip"
Run Code Online (Sandbox Code Playgroud)
这样您就可以明确引用哪个变量。
来自man bash
:
Parameter Expansion
The `$' character introduces parameter expansion, command substitution, or
arithmetic expansion. The parameter name or symbol to be expanded may be
enclosed in braces, which are optional but serve to protect the variable
to be expanded from characters immediately following it which could be
interpreted as part of the name.
When braces are used, the matching ending brace is the first `}' not
escaped by a backslash or within a quoted string, and not within an embed?
ded arithmetic expansion, command substitution, or parameter expansion.
${parameter}
The value of parameter is substituted. The braces are required
when parameter is a positional parameter with more than one digit,
or when parameter is followed by a character which is not to be
interpreted as part of its name. The parameter is a shell parame?
ter as described above PARAMETERS) or an array reference (Arrays).
Run Code Online (Sandbox Code Playgroud)