下cat命令似乎外面做工精细for循环,但是当我把它放在它里面给出了一个语法错误:击:内使用猫循环
for i in 1 2 3 4 5 do
cat file_$i | grep "random text" | cut -d':' -f2 > temp_$i
done
有人能向我解释,正确的方法写这个?谢谢
下cat命令似乎外面做工精细for循环,但是当我把它放在它里面给出了一个语法错误:击:内使用猫循环
for i in 1 2 3 4 5 do
cat file_$i | grep "random text" | cut -d':' -f2 > temp_$i
done
有人能向我解释,正确的方法写这个?谢谢
你并不需要通过把1 2 3 4 5
循环。可以使用bash brace expansion。 {1..5}
for i in {1..5}; do
##
done
我总是喜欢把“做”中的下一行,这样有助于我不记得使用分号:
for i in 1 2 3 4 5
do
cat file_$i | grep "random text" | cut -d':' -f2 > temp_$i
done
在bash中“行尾”被bash编译器隐式视为命令/语句的结束。
例子:
echo "Hello"
exit
#No need of semi-colons here as it is implicit that the end of the line is the completion of the statement
但是,当你想添加两条语句在同一行/命令,你需要通过分号隔开它们;明确()。
例子:
echo "hello"; exit
#here semi-colon implies that the echo statement ends at the semi-colon and from there on to the end of the line is a new statement.
对于 “for语句”,语法都按:
for variable in (value-set)
do
----statements----
done
所以,要么你把for
,do
,statements
和新线done
或用分号分隔它们。
它给出了什么错误?而且,grep可以将文件作为参数,因此您不必捕获文件并将其传送给它。 –
“在意外标记'cat'附近出现语法错误” – Alex