2012-01-15 2 views
1

別のファイルを作成するには:バッシュスクリプトは、私は簡単なシェルスクリプトを作成し

#!/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のようなスクリプトを実行します。

または$1my.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 –

関連する問題