我可以使用帶有如下cat命令的 bash 腳本創建一個包含內容的檔案:
#!/bin/sh
cat >myFyle.txt <<EOL
Some text inside the file
EOL
這按預期作業。但是當我嘗試在這樣的 if 陳述句中使用它時:
#!/bin/sh
if true; then
cat >myFyle.txt <<EOL
Some text inside the file
EOL
fi
我收到錯誤訊息:
Syntax error: end of file unexpected (expecting "fi")
為什么這不起作用以及如何cat在 if 陳述句中正確使用?
注意:if 陳述句的條件是示例性的。這只是為了確保示例執行代碼。
uj5u.com熱心網友回復:
結束分隔線必須是 EOL. 前面沒有空格。
if true; then
cat >myFyle.txt <<EOL
Some text inside the file
EOL
#^^^ Spaces above will be preserved!
fi
您可以<<-EOL在前面使用然后使用制表符(不是空格!),這將被忽略。
if true; then
cat >myFyle.txt <<-EOL
Some text inside the file
EOL
#^^^ - This is a tab. Tabs in front will be ignored.
fi
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/455977.html
下一篇:這是一個隱式管道嗎?
