Bash脚本创建另一个文件

时间:2012-01-15 01:18:36

标签: bash

我创建了一个简单的shell脚本:

#!/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: jsmith@demo.com
echo "gender: M"
echo 
echo
echo "File is done"

我想在同一目录中使用名称,电子邮件和性别详细信息创建一个文件。 我不想从命令行这样做:

#./script.sh > my.config

我宁愿在文件本身内进行。

4 个答案:

答案 0 :(得分:14)

定界符。

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

答案 1 :(得分: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: jsmith@demo.com"
echo "gender: M"
echo 
echo

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

echo "File is done"

答案 2 :(得分:3)

好吧,只需添加&gt;&gt;你的文件到你想写的回声线:

echo "name: John Smith" >> yourfile
echo "email: jsmith@demo.com" >> yourfile
echo "gender: M" >> yourfile

答案 3 :(得分:0)

对于您的所有echo "name:John Smith"行添加> $1(即传入脚本的第一个参数)。

然后运行类似./script.sh my.config的脚本。

或者您可以将$1替换为my.config,然后运行./script.sh