2012-01-15 85 views
1

我創建了一個簡單的shell腳本:bash腳本來創建另一個文件

#!/bin/bash 
clear 
echo "Starting Script now....." 
echo "Write the info below to a new file in same directory...." 

echo "name: John Smith" 
echo "email: [email protected] 
echo "gender: M" 
echo 
echo 
echo "File is done" 

我想創建一個與名稱,電子郵件,和性別信息相同目錄中的文件。 我不希望從這樣的命令行做到這一點:

#./script.sh > my.config 

我寧願從文件本身內做到這一點。

回答

3

,只需添加>> yourfile你想寫回聲線:

echo "name: John Smith" >> yourfile 
echo "email: [email protected]" >> yourfile 
echo "gender: M" >> yourfile 
0

對於您所有的echo "name:John Smith"行添加> $1(即傳遞給腳本的第一個參數)。然後運行./script.sh my.config這樣的腳本。

或者您可以將$1替換爲my.config,然後運行./script.sh

14

Heredoc。

cat > somefile << EOF 
name: ... 
... 
EOF 
4

你可以這樣做:

#!/bin/bash 
clear 
echo "Starting Script now....." 
echo "Write the info below to a new file in same directory...." 

# save stdout to fd 3; redirect fd 1 to my.config 
exec 3>&1 >my.config 

echo "name: John Smith" 
echo "email: [email protected]" 
echo "gender: M" 
echo 
echo 

# restore original stdout to fd 1 
exec >&3- 

echo "File is done" 
+1

很酷! +1 – 2012-01-15 01:31:08

相關問題