在 bash 脚本中写入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14637284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
writing a file in bash script
提问by frazman
I a new to bash but I am trying to write a bash script which does the following:
我是 bash 的新手,但我正在尝试编写一个执行以下操作的 bash 脚本:
write_to_file()
{
#check if file exists
# if not create the file
# else open the file to edit
# go in a while loop
# ask input from user
# write to the end of the file
# until user types ":q"
}
If anyone can point out the literature, I would be very thankful Thanks
如果有人可以指出文献,我将非常感激谢谢
回答by hek2mgl
Update: As it's a bash question, you should try this first. ;)
更新:因为这是一个 bash 问题,你应该先试试这个。;)
cat <<':q' >> test.file
To understand what is going on, read about bash's IO redirection, heredoc syntaxand the catcommand
要了解发生了什么,请阅读 bash 的IO 重定向、heredoc 语法和cat命令
As you see above, there are many ways to do it. To explain some more bash commands I've prepared the function also in the way you've requested it:
正如你在上面看到的,有很多方法可以做到。为了解释更多 bash 命令,我也按照您要求的方式准备了该函数:
#!/bin/bash
write_to_file()
{
# initialize a local var
local file="test.file"
# check if file exists. this is not required as echo >> would
# would create it any way. but for this example I've added it for you
# -f checks if a file exists. The ! operator negates the result
if [ ! -f "$file" ] ; then
# if not create the file
touch "$file"
fi
# "open the file to edit" ... not required. echo will do
# go in a while loop
while true ; do
# ask input from user. read will store the
# line buffered user input in the var $user_input
# line buffered means that read returns if the user
# presses return
read user_input
# until user types ":q" ... using the == operator
if [ "$user_input" = ":q" ] ; then
return # return from function
fi
# write to the end of the file. if the file
# not already exists it will be created
echo "$user_input" >> "$file"
done
}
# execute it
write_to_file
回答by miku
Example with basic argument checks:
带有基本参数检查的示例:
write_to_file()
{
while [ "$line" != ":q" ]; do
read line
if [ "$line" != ":q" ]; then
printf "%s\n" "$line" >> ""
fi
done
}
if [ "$#" -eq 1 ]; then
write_to_file ""
else
echo "Usage: # append to file () user supplied lines until `:q` is entered
write_to_file()
{
until read line && [ "$line" = ":q" ]; do
printf "%s\n" "$line" >> ""
done
}
FILENAME"
exit 2
fi
Or using the probably lesser known untilconstruct, the function can be written a bit more terse:
或者使用可能鲜为人知的until构造函数,可以更简洁地编写函数:
write_to_file() { sed '/^:q$/q' | sed '$d' >>""; }
回答by William Pursell
There are several solutions here that are working waytoo hard. Just do:
这里有几种解决方案正在工作的方式太辛苦。做就是了:
write_to_file test.file
where the first argument is the name of the file. That is, invoke it as:
其中第一个参数是文件名。也就是说,调用它:
while true
do
read INPUT
if [[ "${INPUT}" == :q ]]
then
return
fi
echo "${INPUT}" >> file
done
回答by Carl Norum
This quick example should get you started:
这个快速示例应该可以帮助您入门:
##代码##