bash脚本有问题

发布于 2024-10-03 21:52:17 字数 619 浏览 7 评论 0原文

我正在使用这个 bash 脚本:

for a in `sort -u $HADOOP_HOME/conf/slaves`; do
        rsync -e ssh -a "${HADOOP_HOME}/conf" ${a}:"${HADOOP_HOME}"
done
for a in `sort -u $HBASE_HOME/conf/regionservers`; do
        rsync -e ssh -a "${HBASE_HOME}/conf" ${a}:"${HBASE_HOME}"
done

当我直接从 shell 调用这个脚本时,没有任何问题并且工作正常。但是,当我从另一个脚本调用此脚本时,尽管该脚本完成了其工作,但我在最后收到此消息:

sort: open failed: /conf/slaves: No such file or directory
sort: open failed: /conf/regionservers: No such file or directory

我已在 /etc/profile 中设置了 $HADOOP_HOME 和 $HBASE_HOME 并且该脚本正确完成了工作。但我不明白为什么它最后给出了这个信息。

I'm using this bash script:

for a in `sort -u $HADOOP_HOME/conf/slaves`; do
        rsync -e ssh -a "${HADOOP_HOME}/conf" ${a}:"${HADOOP_HOME}"
done
for a in `sort -u $HBASE_HOME/conf/regionservers`; do
        rsync -e ssh -a "${HBASE_HOME}/conf" ${a}:"${HBASE_HOME}"
done

When I call this script directly from shell, there are no problems and it works fine. But when I call this script from another script, although the script does its job, I get this message at the end:

sort: open failed: /conf/slaves: No such file or directory
sort: open failed: /conf/regionservers: No such file or directory

I have set $HADOOP_HOME and $HBASE_HOME in /etc/profile and the script does the job right. But I don't understand why it gives this message in the end.

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

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

发布评论

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

评论(2

绝情姑娘 2024-10-10 21:52:17

确定这样做是正确的吗?当您从 shell 调用此脚本时,它充当交互式 shell,它读取并获取 /etc/profile~/.bash_profile 如果它存在。当您从另一个脚本调用它时,它以非交互式方式运行,并且不会获取这些文件。如果您希望非交互式 shell 获取文件,可以通过设置 BASH_ENV 环境变量来实现。

#!/bin/bash

export BASH_ENV=/etc/profile
./call/to/your/HADOOP/script.sh

Are you sure it's doing it right? When you call this script from the shell it is acting as an interactive shell which reads and sources /etc/profile and ~/.bash_profile if it exists. When you call it from another script it is running as non-interactive and wont source those files. If you want a non-interactive shell to source a file you can do this by setting the BASH_ENV environment variable.

#!/bin/bash

export BASH_ENV=/etc/profile
./call/to/your/HADOOP/script.sh
记忆消瘦 2024-10-10 21:52:17

所有内容都指向脚本运行时定义的变量。

您应该确保它们是为您的脚本设置的。在第一个循环之前,放置以下行:

echo "[${HADOOP_HOME}] [${HBASE_HOME}]"

并确保不会输出 "[] []" (甚至不会输出一个 "[]")。

此外,在脚本顶部放置一个 set +x 行 - 这将在执行之前输出行,您可以看到正在执行的操作。

请记住,某些 shell 不会将环境变量传递给子 shell,除非您显式导出它们(设置它们是不够的)。

Everything points to those variables not being defined when your script runs.

You should ensure that they are set for your script. Before the first loop, place the line:

echo "[${HADOOP_HOME}] [${HBASE_HOME}]"

and make sure that doesn't output "[] []" (or even one "[]").

Additionally, put a set +x line at the top of the script - this will output lines before executing them and you can see what's being done.

Keep in mind that some shells don't pass on environment variables to subshells unless you explicitly export them (setting them is not enough).

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