我写了这个函数很多次,但不能像一个小的语法错误一样获得right..looks,但也不能修复它。
function hadleDate{
date=`echo $1|cut -d"." -f1-3`
for element in $date; do
size=${#element}
if [[ $element == 0? ]]; then
echo -n $element|cut -c2-
elif [[ $size -eq 4 ]]; then
echo $element
else
echo -n $element
fi
done
}
mos="14.03.2013"
echo handleDate $mos发布于 2016-05-23 22:00:22
声明中的函数名不能直接跟在{后面。应该有空格或圆括号。
function hadleDate () {
# ~~发布于 2016-05-23 22:03:15
您可能希望访问shellcheck.net以查找语法错误。
你错过了,if的fi和" for“的if。
function hadleDate {
date=`echo $1|cut -d"." -f1-3`
for element in $date; do
size=${#element}
if [[ $element == 0? ]]; then
echo -n $element|cut -c2-
elif [[ $size -eq 4 ]]; then
echo $element
fi
done
}
mos="14.03.2013"
echo handleDate $mos发布于 2016-05-23 23:48:16
您的handleDate函数尝试将字段中的日期剪切并将其放入数组中。您的变量date不是数组,而只是一个字符串。
您可以不使用这样的数组:
function handleDate {
# Cut fields with dots and use $1 as input for the read.
IFS=. read mm dd yyyy <<< "${1}"
# Using printf can cut of the zeroes by converting the string to a number
printf "%d-%d-%s\n" "${mm}" "${dd}" "${yyyy}"
}
mos="14.03.2013"
handleDate "${mos}"https://stackoverflow.com/questions/37392936
复制相似问题