【发布时间】:2021-07-07 10:26:59
【问题描述】:
我在文件 finalinput.txt 中有如下一行
美国东部时间 2021 年 4 月 9 日晚上 10:52:35 c1d99c1af08ce4ae:-4277b446:178b8700262:-8000-0000000000019a8b 1618023155272 我在下面写了一个小代码subtest.sh
keyinput="c1d99c1af08ce4ae:-4277b446:178b8700262:-8000-0000000000019a8b"
for line in $(cat finalinput.txt)
do
if $line | grep "$keyinput" > /dev/null; then
echo -e "$date $time\n" >> timeoutput.txt
fi
done
output :
line 20: Apr: command not found
./subtest.sh: line 20: 9,: command not found
./subtest.sh: line 20: 2021: command not found
./subtest.sh: line 20: 10:52:35: command not found
./subtest.sh: line 20: PM: command not found
./subtest.sh: line 20: EDT: command not found
./subtest.sh: line 20: c1d99c1af08ce4ae:-4277b446:178b8700262:-8000-0000000000019a8b: command not found
./subtest.sh: line 20: 1618023155272: command not found
./subtest.sh: line 20: ###Update: command not found
./subtest.sh: line 20: Assignment: command not found
./subtest.sh: line 20: Milestone: command not found
./subtest.sh: line 20: Apr: command not found
./subtest.sh: line 20: 9,: command not found
./subtest.sh: line 20: 2021: command not found
./subtest.sh: line 20: 10:52:37: command not found
./subtest.sh: line 20: PM: command not found
./subtest.sh: line 20: EDT: command not found
请在我错的地方帮忙
【问题讨论】:
-
用shellcheck.net检查你的脚本
-
试过了。它在这个 shellcheck 中显示不错,但在我的本地服务器上却是同样的错误
-
你正在运行 $line 作为命令
-
知道了.. 非常感谢如果 echo $line | grep "$keyinput"
标签: linux shell if-statement scripting grep