2011-03-24 70 views
2

中的文件,我想知道怎樣才能實現類似如下:貓殼功能

test(){ 
    cat>file<<'EOF' 
    abc 
    EOF 
} 

非常感謝。

+1

我想你剛剛做了? – SiegeX 2011-03-24 00:51:47

+0

你的意思是在bash函數中捕獲一個文件嗎? – pajton 2011-03-24 00:53:08

+0

@SiegeX:我粘貼的代碼不適合我。它在你的終端上工作嗎?我的終端似乎沒有確定它可以在我複製/粘貼代碼後停止。 – 2011-03-24 00:56:35

回答

6

強:

在EOF前刪除空格(所以它是由單獨一行,而不是縮進)。

+0

這一個伎倆。刪除第二個EOF前面的空格/製表符(新行字符可以),你會沒事的。 – 2014-11-19 02:53:06

4

bash(1)

If the redirection operator is <<-, then all leading tab 
characters are stripped from input lines and the line 
containing delimiter. This allows here-documents within 
shell scripts to be indented in a natural fashion. 

它說tab,並在我的測試,tab作品,但空間並不:

#!/bin/bash 

cat>file <<-END 
    hello 
    world 
    hello 
    END 

echo done 

(所有這些縮進標籤;在有趣的是,關於代碼的四個前導空格標記意味着在渲染文本中只有四個空格。)

1

你的代碼應該工作得很好,有什麼具體的你在找什麼?

#!/bin/sh 

input() { 
    cat > file <<EOF 
input 
line 
another line 
EOF 
} 

input 

編輯:改變function inputinput()

+0

不要使用'function'關鍵字,它不是POSIX並已棄用。相反,使用'input()'做的事情' – SiegeX 2011-03-24 01:04:45

+0

謝謝,沒有意識到function關鍵字不符合POSIX標準,而且實際上已經被棄用了。非常感謝! – Suroot 2011-03-24 01:08:12