2015-05-12 43 views
1

下cat命令似乎外面做工精细for循环,但是当我把它放在它里面给出了一个语法错误:击:内使用猫循环

for i in 1 2 3 4 5 do 
    cat file_$i | grep "random text" | cut -d':' -f2 > temp_$i 
done 

有人能向我解释,正确的方法写这个?谢谢

+0

它给出了什么错误?而且,grep可以将文件作为参数,因此您不必捕获文件并将其传送给它。 –

+0

“在意外标记'cat'附近出现语法错误” – Alex

回答

5

for循环应该有一个分号:

for i in 1 2 3 4 5; do 
+2

或在下一行放置'do'。 – Barmar

+0

哇哈哈,那工作。这样一个愚蠢的错误...谢谢! – Alex

1

我总是喜欢把“做”中的下一行,这样有助于我不记得使用分号:

for i in 1 2 3 4 5 
do 
    cat file_$i | grep "random text" | cut -d':' -f2 > temp_$i 
done 
1

在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 

所以,要么你把fordostatements和新线done或用分号分隔它们。