如何使用带有变量的 Bash 编写多行字符串?

发布于 2024-12-12 01:14:14 字数 266 浏览 0 评论 0原文

如何使用 BASH 在名为 myconfig.conf 的文件中写入多行?

#!/bin/bash
kernel="2.6.39";
distro="xyz";

echo <<< EOL
line 1, ${kernel}
line 2,
line 3, ${distro}
line 4
line ...
EOL >> /etc/myconfig.conf;
cat /etc/myconfig.conf;

How can I write multi-lines in a file called myconfig.conf using BASH?

#!/bin/bash
kernel="2.6.39";
distro="xyz";

echo <<< EOL
line 1, ${kernel}
line 2,
line 3, ${distro}
line 4
line ...
EOL >> /etc/myconfig.conf;
cat /etc/myconfig.conf;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

心在旅行 2024-12-19 01:14:14

语法 (<<<) 和使用的命令 (echo) 错误。

正确的是:

#!/bin/bash

kernel="2.6.39"
distro="xyz"
cat >/etc/myconfig.conf <<EOL
line 1, ${kernel}
line 2, 
line 3, ${distro}
line 4 line
... 
EOL

cat /etc/myconfig.conf

此结构被称为 此处文档,可以在 Bash man 中找到man --pager='less -p "\s*Here Documents"' bash 下的页面。

The syntax (<<<) and the command used (echo) is wrong.

Correct would be:

#!/bin/bash

kernel="2.6.39"
distro="xyz"
cat >/etc/myconfig.conf <<EOL
line 1, ${kernel}
line 2, 
line 3, ${distro}
line 4 line
... 
EOL

cat /etc/myconfig.conf

This construction is referred to as a Here Document and can be found in the Bash man pages under man --pager='less -p "\s*Here Documents"' bash.

爱的故事 2024-12-19 01:14:14
#!/bin/bash
kernel="2.6.39";
distro="xyz";

cat > /etc/myconfig.conf << EOL
line 1, ${kernel}
line 2,
line 3, ${distro}
line 4
line ...
EOL

这就是你想要的。

#!/bin/bash
kernel="2.6.39";
distro="xyz";

cat > /etc/myconfig.conf << EOL
line 1, ${kernel}
line 2,
line 3, ${distro}
line 4
line ...
EOL

this does what you want.

不必你懂 2024-12-19 01:14:14

如果您不想替换变量,则需要用单引号将 EOL 引起来。

cat >/tmp/myconfig.conf <<'EOL'
line 1, ${kernel}
line 2, 
line 3, ${distro}
line 4 line
... 
EOL

前面的例子:

$ cat /tmp/myconfig.conf 
line 1, ${kernel}
line 2, 
line 3, ${distro}
line 4 line
... 

If you do not want variables to be replaced, you need to surround EOL with single quotes.

cat >/tmp/myconfig.conf <<'EOL'
line 1, ${kernel}
line 2, 
line 3, ${distro}
line 4 line
... 
EOL

Previous example:

$ cat /tmp/myconfig.conf 
line 1, ${kernel}
line 2, 
line 3, ${distro}
line 4 line
... 
薄荷港 2024-12-19 01:14:14

Heredoc 解决方案当然是最常见的方法。其他常见的解决方案有:

echo 'line 1, '"${kernel}"'
line 2,
line 3, '"${distro}"'
line 4' > /etc/myconfig.conf

exec 3>&1 # Save current stdout
exec > /etc/myconfig.conf
echo line 1, ${kernel}
echo line 2, 
echo line 3, ${distro}
...
exec 1>&3  # Restore stdout

printf "%s\n" "line1, ${kernel}" "line2," "line3, $distro" ...

The heredoc solutions are certainly the most common way to do this. Other common solutions are:

echo 'line 1, '"${kernel}"'
line 2,
line 3, '"${distro}"'
line 4' > /etc/myconfig.conf

and

exec 3>&1 # Save current stdout
exec > /etc/myconfig.conf
echo line 1, ${kernel}
echo line 2, 
echo line 3, ${distro}
...
exec 1>&3  # Restore stdout

and

printf "%s\n" "line1, ${kernel}" "line2," "line3, $distro" ...
人│生佛魔见 2024-12-19 01:14:14

我正在使用 Mac OS 并在 SH 脚本 中编写多行,以下代码对我有用

#! /bin/bash
FILE_NAME="SomeRandomFile"

touch $FILE_NAME

echo """I wrote all
the  
stuff
here.
And to access a variable we can use
$FILE_NAME  

""" >> $FILE_NAME

cat $FILE_NAME

请不要忘记根据需要将 chmod 分配给脚本文件。
我用过

chmod u+x myScriptFile.sh

I'm using Mac OS and to write multiple lines in a SH Script following code worked for me

#! /bin/bash
FILE_NAME="SomeRandomFile"

touch $FILE_NAME

echo """I wrote all
the  
stuff
here.
And to access a variable we can use
$FILE_NAME  

""" >> $FILE_NAME

cat $FILE_NAME

Please don't forget to assign chmod as required to the script file.
I have used

chmod u+x myScriptFile.sh
情未る 2024-12-19 01:14:14

下面的机制有助于将多行重定向到文件。将完整的字符串保留在 " 下,以便我们可以重定向变量的值。

#!/bin/bash
kernel="2.6.39"
echo "line 1, ${kernel}
line 2," > a.txt
echo 'line 2, ${kernel}
line 2,' > b.txt

a.txt 的内容是

line 1, 2.6.39
line 2,

b.txt 的内容是

line 2, ${kernel}
line 2,

Below mechanism helps in redirecting multiple lines to file. Keep complete string under " so that we can redirect values of the variable.

#!/bin/bash
kernel="2.6.39"
echo "line 1, ${kernel}
line 2," > a.txt
echo 'line 2, ${kernel}
line 2,' > b.txt

Content of a.txt is

line 1, 2.6.39
line 2,

Content of b.txt is

line 2, ${kernel}
line 2,
何其悲哀 2024-12-19 01:14:14

在我的用例中,我必须使用以下内容:

{
 ...
 printf "Suites: $SUITES"
 printf "Components: $COMPONENTS"
 printf "Signed-By: $SIGNATURE"
 [ ! -z "$ARCH_STRING" ] && printf "$ARCH_STRING"
 [ ! -z "$LANG_STRING" ] && printf "$LANG_STRING"
 [ ! -z "$TARGET_STRING" ] && printf "$TARGET_STRING"
 ...
} > /tmp/myconfig.conf 

这是因为如果未定义变量,我不想将空换行符写入文件。这种方法的好处是,如果需要,您可以在块内编写各种任意逻辑。

当然,如果您想附加,您也可以在此处使用 >> 而不是像其他答案中那样使用 >

In my usecase I had to use the following:

{
 ...
 printf "Suites: $SUITES"
 printf "Components: $COMPONENTS"
 printf "Signed-By: $SIGNATURE"
 [ ! -z "$ARCH_STRING" ] && printf "$ARCH_STRING"
 [ ! -z "$LANG_STRING" ] && printf "$LANG_STRING"
 [ ! -z "$TARGET_STRING" ] && printf "$TARGET_STRING"
 ...
} > /tmp/myconfig.conf 

This because I did not want to write empty newlines to the file if the variables were not defined. The nice thing about this method is that you can write all kinds of arbitrary logic within the block if necessary.

And ofcourse, if you would like to append, you can also use >> here instead of > like you would in the other answers.

最单纯的乌龟 2024-12-19 01:14:14

我通常将模板放入文件中并使用此模板引擎:

### <template-file> [ARG=VALUE..]
## Variables are replaced only within "{{" and "}}" notation.
## Example:
##         $0 path-to-tmpl REF=master pass=xx
##         # The template may look like so:
##         #    $pass = ["user", "{{ $pass }}"];
##         # Resulting in:
##         #    $pass = ["user", "xxx"];
##~
template() {
    tmpl=$1
    shift

    for i in $@; do
        declare $i;
    done

    eval "echo \"$(sed -e 's/"/\\"/g' -e 's/\$/\\$/g' -e 's/{{\s*\\\(\$\w*\)\s*}}/\1/g' $tmpl)\""
}

I usually put template in file and use this templating engine:

### <template-file> [ARG=VALUE..]
## Variables are replaced only within "{{" and "}}" notation.
## Example:
##         $0 path-to-tmpl REF=master pass=xx
##         # The template may look like so:
##         #    $pass = ["user", "{{ $pass }}"];
##         # Resulting in:
##         #    $pass = ["user", "xxx"];
##~
template() {
    tmpl=$1
    shift

    for i in $@; do
        declare $i;
    done

    eval "echo \"$(sed -e 's/"/\\"/g' -e 's/\$/\\$/g' -e 's/{{\s*\\\(\$\w*\)\s*}}/\1/g' $tmpl)\""
}
九局 2024-12-19 01:14:14

我认为另一种更简单的方法,但绝对适用于少量行

touch myfile.txt
echo "line1">>myfile.txt
echo "line2">>myfile.txt
echo "line3">>myfile.txt
echo "line4">>myfile.txt

another simpler way I think but definitely for small number of lines

touch myfile.txt
echo "line1">>myfile.txt
echo "line2">>myfile.txt
echo "line3">>myfile.txt
echo "line4">>myfile.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文