使用 bash 脚本将一些行写入文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7823079/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:07:37  来源:igfitidea点击:

Write some lines to a file with a bash script

bashsh

提问by wim

How can I write some lines to a file in a bash script?

如何在 bash 脚本中将一些行写入文件?

I want to write the following into a file ~/.inputrc

我想将以下内容写入文件 ~/.inputrc

"\e[A": history-search-backward
"\e[B": history-search-forward
"\e[C": forward-char
"\e[D": backward-char

Currently I have this working but somewhat ugly method, and I'm sure there should be a better way.

目前我有这个工作但有点丑陋的方法,我相信应该有更好的方法。

#!/bin/sh
echo \"\e[A\": history-search-backward > ~/.inputrc 
echo \"\e[B\": history-search-forward >> ~/.inputrc
echo \"\e[C\": forward-char >> ~/.inputrc
echo \"\e[D\": backward-char >> ~/.inputrc

回答by Spencer Rathbun

A slightly simpler method would be:

一个稍微简单的方法是:

cat > ~/.inputrc << "EOF"
"\e[A": history-search-backward
"\e[B": history-search-forward
"\e[C": forward-char
"\e[D": backward-char
EOF

I'm curious why you need to do this though. If you want to setup a file with some specific text, then maybe you should create the skeleton file, and dump it into /etc/skel. Then, you can cp /etc/skel/.inputrc ~/.inputrcin your script.

我很好奇你为什么需要这样做。如果你想设置一个包含一些特定文本的文件,那么也许你应该创建骨架文件,并将其转储到/etc/skel. 然后,您可以cp /etc/skel/.inputrc ~/.inputrc在您的脚本中。

回答by Adam Rosenfield

Use a here document:

使用此处的文档

#!/bin/bash
cat >~/.inputrc <<EOF
"\e[A": history-search-backward
"\e[B": history-search-forward
"\e[C": forward-char
"\e[D": backward-char
EOF

This lets you put the data inline in your shell script. The string EOFcan be whatever you want, so just pick any string that doesn't appear in your input on a single line by itself.

这使您可以将数据内联到 shell 脚本中。该字符串EOF可以是您想要的任何字符串,因此只需选择任何未单独出现在您的输入中的字符串即可。

回答by Keith Thompson

Using the echocommand for things other than simple strings can be complex and non-portable. There's the GNU version of echo(part of coreutils), the BSD version that you'll probably find on MacOS, and most shells have echoas a built-in command. All these different versions can have subtly different ways of handling options command-line options (-nto inhibit the trailing newline, -c/-Cto enable or disable backslash escapes) and escapes (\emight or might not be an encoding for the escape character).

echo命令用于简单字符串以外的其他内容可能很复杂且不可移植。有echo(coreutils 的一部分)的 GNU 版本,您可能会在 MacOS 上找到的 BSD 版本,以及大多数 shellecho作为内置命令。所有这些不同的版本在处理选项命令行选项(-n禁止尾随换行,-c/-C以启用或禁用反斜杠转义)和转义(\e可能是也可能不是转义字符的编码)方面有微妙的不同方式。

printfto the rescue.

printf到救援。

printfis probably available as /usr/bin/printfand/or /bin/printf, and it's also a built-in in some shells (bash and zsh anyway), but its behavior is much more consistent.

printf可能作为/usr/bin/printfand/or可用/bin/printf,并且它也是某些 shell 中的内置函数(无论如何 bash 和 zsh),但它的行为更加一致。

Here's the GNU coretuils printf documentation.

这是 GNU coretuils printf 文档。

For your example, you could write:

对于您的示例,您可以编写:

(
   printf '"\e[A": history-search-backward\n'
   printf '"\e[B": history-search-forward\n'
   printf '"\e[C": forward-char\n'
   printf '"\e[D": backward-char\n'
) > ~/.inputrc

回答by mmrtnt

That's pretty much it.

差不多就是这样。

Here's one way to reduce the redundancy:

这是减少冗余的一种方法:

TEXT="\"\e[A\": history-search-backward 
\"\e[B\": history-search-forward 
\"\e[C\": forward-char 
\"\e[D\": backward-char"

echo "$TEXT" > ~/.inputrc