Bash EOF 有意外行为

发布于 2024-10-04 06:15:33 字数 836 浏览 12 评论 0原文

我正在使用一个快速而肮脏的 BASH 脚本来自动化 CF 分区过程。
这很有效,直到我想添加它。

基本上,我有一个对紧凑型闪存进行分区的脚本:

CMD=.fdisk
DEV=/dev/sdc

echo "Calculating partition sizes..."
P1SIZE=+1100M
P2SIZE=+1200M

echo "Creating instruction file..."
PT1="n p 1\n $P1SIZE"
PT2="n p 2\n $P2SIZE"
END="a 1 w EOF\n"

[ -e $CMD ] && rm -rf $CMD

for i in $PT1 $PT2 $END; do echo -e $i >> $CMD; done

echo "Creating partitions..."
fdisk $DEV << EOF < $CMD
[ -e $CMD ] && rm -f $CMD

这个想法是,将来由“$CMD”表示的文件将由具有正确开始值和结束值的外部工具自动创建。现在我只是接受默认的开始并提供尺寸。

在我想将一些额外的步骤合并到我的脚本中之前,这种方法非常有效。但当我注意到奇怪的行为时,我遇到了问题。

首先,如果我在函数中添加上面的代码,我将不断收到脚本错误:

line XX: syntax error: unexpected end of file

此外,我意识到“fdisk”命令之后的任何附加代码都不会执行,并且脚本似乎只是终止(没有错误, $? = 0)。

有谁知道为什么该函数不起作用并且脚本终止?

I was using a quick and dirty BASH script to automate a CF partitioning process.
This worked well until I wanted to add to it.

Basically, I have a script which partitions a compact flash as such:

CMD=.fdisk
DEV=/dev/sdc

echo "Calculating partition sizes..."
P1SIZE=+1100M
P2SIZE=+1200M

echo "Creating instruction file..."
PT1="n p 1\n $P1SIZE"
PT2="n p 2\n $P2SIZE"
END="a 1 w EOF\n"

[ -e $CMD ] && rm -rf $CMD

for i in $PT1 $PT2 $END; do echo -e $i >> $CMD; done

echo "Creating partitions..."
fdisk $DEV << EOF < $CMD
[ -e $CMD ] && rm -f $CMD

The idea is that in the future the file represented by '$CMD' will be created automatically by an external tool with the proper start and end values. Right now I simply accept the default start and provide a size.

This worked quite well until I wanted to incorporate a few additional steps to my script. But I got into problems when I noticed weird behaviors.

The first is that if I add the code above inside a function, I will keep getting a script error:

line XX: syntax error: unexpected end of file

Also, I realized that any additional code after the 'fdisk' command will not execute and the script seems to simply terminate (without errors, $? = 0).

Does anyone know why the function doesn't work and the script terminates?

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

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

发布评论

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

评论(2

喜你已久 2024-10-11 06:15:33

经验法则是,您使用的“限制字符串”(在我的例子中使用“EOF”,但您可以使用您想要的任何内容)前面不能有空格(或制表符)。如果确实如此,则不会被视为结束。

示例:

myFunction() {
    cat << EOF
        Hello
        World
        EOF
}

上面的“EOF”被视为常规文本,因为它前面有空格!如果您改为编写以下内容:

myFunction() {
    cat << EOF
        Hello
        World
EOF
}

现在就可以了!

如果您坚持使用空格,则可以通过在“<<”右侧使用“-”字符来抑制制表符人物。

示例:

myFunction() {
    cat <<- EOF
        Hello
        World
        EOF
}

请注意,这仅适用于制表符,不适用于空格!这对于让你的代码更具可读性来说非常有用。

另请注意,如果引用“限制字符串”,最终会禁用参数替换。因此,如果您想编写不应被替换或执行的文本,这使之成为可能:

cat << "EOF"
    Type `ls -la` at the command prompt
EOF

A rule of thumb is that the 'limit string' you use (I was using 'EOF' in my case, but you can use anything you want) must not have spaces (or tabs) in front of it. If it does, it will not be considered the end.

Example:

myFunction() {
    cat << EOF
        Hello
        World
        EOF
}

The 'EOF' above is considered regular text because it has spaces in front of it! If you write the following instead:

myFunction() {
    cat << EOF
        Hello
        World
EOF
}

It will now work!

If you insist on using spaces, then you can suppress the tabs by using a '-' character to the immediate right of the '<<' characters.

Example:

myFunction() {
    cat <<- EOF
        Hello
        World
        EOF
}

Note that this only works for tabs and not spaces! This is great to keep your code a bit more readable.

Also note that if you quote the 'limit string', you end up disabling parameter substitution. So if you want to write text that should not be substituted or executed, this makes it possible:

cat << "EOF"
    Type `ls -la` at the command prompt
EOF
云淡月浅 2024-10-11 06:15:33

你已经开始了一个heredoc (<< EOF),但你永远不会在任何地方结束它。

You've started a heredoc (<< EOF), but you never end it anywhere.

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