while read line
do
echo $line
calendar_date=$(cut -d\ -f1 $line)
hr_of_day=$(cut -d\ -f2 $line)
echo "date: $calendar_date hr: $hr_of_day"
done < $FILE
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
date: hr:
2011-06-30 | 23
cut: 2011-06-30: No such file or directory
cut: |: No such file or directory
cut: 23: No such file or directory
Run Code Online (Sandbox Code Playgroud)
cut
将$line
参数理解为文件名。如果你的 shell 是 bash,你可以使用<<<
here-word:
cut -d' ' -f1 <<< "$line"
Run Code Online (Sandbox Code Playgroud)
但是,不需要调用外部命令,bash 可以通过参数替换来实现:
date=${line%|*} # Delete from | to the right.
hour=${line#*|} # Delete up to |.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6331 次 |
最近记录: |