bash脚本有问题
我正在使用这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定这样做是正确的吗?当您从 shell 调用此脚本时,它充当交互式 shell,它读取并获取
/etc/profile
和~/.bash_profile
如果它存在。当您从另一个脚本调用它时,它以非交互式方式运行,并且不会获取这些文件。如果您希望非交互式 shell 获取文件,可以通过设置 BASH_ENV 环境变量来实现。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 theBASH_ENV
environment variable.所有内容都指向脚本运行时未定义的变量。
您应该确保它们是为您的脚本设置的。在第一个循环之前,放置以下行:
并确保不会输出
"[] []"
(甚至不会输出一个"[]"
)。此外,在脚本顶部放置一个
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:
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).