BASH 意外的 EOF

发布于 2024-10-24 04:07:54 字数 501 浏览 1 评论 0原文


我的 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 技术交流群。

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

发布评论

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

评论(2

仙女 2024-10-31 04:07:54

我在 Mac 上从问题中复制了您的代码(复制粘贴)并运行该文件:

bash -n -v x.sh

事实上,我这样做了两次;第一次,我确保文件末尾有换行符,第二次我确保没有换行符。 bash 两次都非常高兴。

这向我表明问题不在于可见字符;而在于可见字符。文件中有一些看不见的字符导致悲伤。您可能需要使用 od -c 等工具仔细检查文件,以找到导致问题的字符。

另外,FWIW,我的 Mac 上的 readlink 命令给出:

$ readlink -f $0
readlink: illegal option -- f
usage: readlink [-n] [file ...]
$

Linux 版本的 readlink 采用 -f。它不是 POSIX 命令,因此没有法律上的标准可供参考。

I coped your code from the question on a Mac (copy'n'paste) and ran the file with:

bash -n -v x.sh

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:

$ readlink -f $0
readlink: illegal option -- f
usage: readlink [-n] [file ...]
$

The Linux version of readlink takes -f. It isn't a POSIX command, so there is no de jure standard to refer to.

梦冥 2024-10-31 04:07:54

用 od -c 分析文件发现行结尾是 \r\n,我确实在 Windows 上修改了文件,愚蠢的我。不管怎样,我在 BASH 脚本方面还遇到了另一个问题。此行:

while sleep 1; do kill -0 $PIDD || break; done

应该等待 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:

while sleep 1; do kill -0 $PIDD || break; done

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.

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