BASH 意外的 EOF
我的 Mac 不断告诉我这个 bash 脚本的文件意外结束,在最后一行。我对编程并不陌生,但对 BASH 很陌生,有人认为这有什么问题吗?
#!/bin/bash
#bootstrapper.sh
PIDD="$5"
while sleep 1; do kill -0 $PIDD || break; done
# Absolute path to this script. /home/user/bin/foo.sh
SCRIPT=$(readlink -f $0)
# Absolute path this script is in. /home/user/bin
SCRIPTPATH=`dirname $SCRIPT`
POSPAR1="$1" #-l
POSPAR2="$2" #location
POSPAR3="$3" #-d
POSPAR4="$4" #directory
cp -r -f $SCRIPTPATH/$4/* $2
rm -r -f $SCRIPTPATH/$4
先感谢您!
My Mac keeps telling my unexpected end of file for this bash script, on the last line. I am not new to programming but very new to BASH, does anyone see anything wrong with this?
#!/bin/bash
#bootstrapper.sh
PIDD="$5"
while sleep 1; do kill -0 $PIDD || break; done
# Absolute path to this script. /home/user/bin/foo.sh
SCRIPT=$(readlink -f $0)
# Absolute path this script is in. /home/user/bin
SCRIPTPATH=`dirname $SCRIPT`
POSPAR1="$1" #-l
POSPAR2="$2" #location
POSPAR3="$3" #-d
POSPAR4="$4" #directory
cp -r -f $SCRIPTPATH/$4/* $2
rm -r -f $SCRIPTPATH/$4
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 Mac 上从问题中复制了您的代码(复制粘贴)并运行该文件:
事实上,我这样做了两次;第一次,我确保文件末尾有换行符,第二次我确保没有换行符。 bash 两次都非常高兴。
这向我表明问题不在于可见字符;而在于可见字符。文件中有一些看不见的字符导致悲伤。您可能需要使用 od -c 等工具仔细检查文件,以找到导致问题的字符。
另外,FWIW,我的 Mac 上的
readlink
命令给出:Linux 版本的
readlink
采用-f
。它不是 POSIX 命令,因此没有法律上的标准可供参考。I coped your code from the question on a Mac (copy'n'paste) and ran the file with:
In fact, I did that twice; the first time, I ensured there was a newline at the end of the file, and the second time I ensured that there wasn't a newline. And bash was quite happy both times.
This indicates to me that the problem is not in the visible characters; there are some invisible characters in the file causing grief. You will probably need to scrutinize the file with a tool such as
od -c
to find the character that is causing the trouble.Also, FWIW, the
readlink
command on my Mac gives:The Linux version of
readlink
takes-f
. It isn't a POSIX command, so there is no de jure standard to refer to.用 od -c 分析文件发现行结尾是 \r\n,我确实在 Windows 上修改了文件,愚蠢的我。不管怎样,我在 BASH 脚本方面还遇到了另一个问题。此行:
应该等待 PID(存储在变量 $PIDD 中)关闭。它一直等到它不存在(PID),但当它最终不存在时,它输出:kill: 4: No such process。脚本的其余部分按预期工作,但脚本不会终止。我可以使脚本正确终止并且不输出 No such process 吗?
对于所有新手问题,我深表歉意,我对 BASH 和 Linux 很糟糕。
再次感谢您的所有帮助。
Analyzing the file with od -c revealed the line ending were \r\n, I did modify the file one Windows, silly me. Anyway, I am having another issues with the BASH script. This line:
Is supposed to wait until the PID (stored in variable $PIDD) closes. It waits until it doesn't exist (the PID), but when it finally doesn't exist, it outputs: kill: 4: No such process. The rest of the script works as intended, but then the script doesn't terminate. Can I make the script terminate properly and not have that No such process be outputted?
Sorry for all the newbie questions, I'm awful at BASH and Linux.
Thanks again for all your help.