Bash EOF 有意外行为
我正在使用一个快速而肮脏的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经验法则是,您使用的“限制字符串”(在我的例子中使用“EOF”,但您可以使用您想要的任何内容)前面不能有空格(或制表符)。如果确实如此,则不会被视为结束。
示例:
上面的“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:
The 'EOF' above is considered regular text because it has spaces in front of it! If you write the following instead:
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:
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:
你已经开始了一个heredoc (
<< EOF
),但你永远不会在任何地方结束它。You've started a heredoc (
<< EOF
), but you never end it anywhere.