2012-01-15 64 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

相关问题