脚本中的源.bashrc无法正常工作 [英] source .bashrc in a script not working

查看:264
本文介绍了脚本中的源.bashrc无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行一个脚本,该脚本正在安装ros,并在安装后用catkin_make编译工作区.

我找到了解决问题的解决方案,但我无法解释原因.我有一个名为install.bash的文件正在调用其他文件:

#!/bin/bash

source 01_install_ros.bash

重要的是在01_install_ros.bash中:

# variable not set because it is done in the script setup.bash of ros
echo "before source in 01_install_ros"
echo "ROS_ROOT: "$ROS_ROOT
whereis catkin_make
echo ""

echo "source /opt/ros/kinetic/setup.bash" >> $HOME/.bashrc
# doesn't set the variables
source "$HOME"/.bashrc
# the solutions
source /opt/ros/kinetic/setup.bash

# variables not set if I use the source of .bashrc
echo "after source in 01_install_ros"
echo "ROS_ROOT: "$ROS_ROOT
whereis catkin_make
echo ""

如评论中所述,采购.bashrc而不是直接安装setup.bash无效.我真的不明白为什么.你能解释一下我吗?

解决方案

某些平台附带的~/.bashrc顶部带有一个条件,如果发现外壳不是 non,则该条件明确停止处理-interactive .

例如,在Ubuntu 18.04上:

 # If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac
 

类似的测试,在同一平台上的/etc/bash.bashrc中看到:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

在这种情况下,脚本中获取~/.bashrc无效,因为脚本在非交互式外壳中运行默认情况下.

您的选项为:

  • 其中之一:停用~/.bashrc

  • 中的条件
  • 或者:在调用source ~/.bashrc之前尝试模拟交互式shell .
    所需的具体仿真取决于条件的具体情况,但是有两种可能的方法:如果您不提前知道会遇到什么条件,则可能必须同时雇用他们

    • set -i暂时使$-包含i,表示交互式外壳.
    • 如果您知道执行交互性测试的行的内容,请使用grep将其从~/.bashrc中过滤出来,然后使用eval得出结果(通常应避免使用后者,但应将其放入这种情况有效地提供了与采购相同的功能.
      请注意,确保环境变量PS1的值 还不够,因为Bash 在非交互式shell中主动重置-请参见

或者,如果您控制自己的脚本的调用方式,则可以使用
bash -i script调用它.

I am doing a script that is installing ros and after installing it, compiling a workspace with catkin_make.

I found the solution to solve my problem but I can't explain the reason. I have a file called install.bash that is calling others:

#!/bin/bash

source 01_install_ros.bash

What is important is in 01_install_ros.bash:

# variable not set because it is done in the script setup.bash of ros
echo "before source in 01_install_ros"
echo "ROS_ROOT: "$ROS_ROOT
whereis catkin_make
echo ""

echo "source /opt/ros/kinetic/setup.bash" >> $HOME/.bashrc
# doesn't set the variables
source "$HOME"/.bashrc
# the solutions
source /opt/ros/kinetic/setup.bash

# variables not set if I use the source of .bashrc
echo "after source in 01_install_ros"
echo "ROS_ROOT: "$ROS_ROOT
whereis catkin_make
echo ""

As written in comments, sourcing .bashrc instead of directly setup.bash doesn't work. I really don't get why. Can you explain me?

解决方案

Some platforms come with a ~/.bashrc that has a conditional at the top that explicitly stops processing if the shell is found to be non-interactive.

For example, on Ubuntu 18.04:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

A similar test, seen in /etc/bash.bashrc on the same platform:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

If this is the case, sourcing ~/.bashrc from a script will have no effect, because scripts run in non-interactive shells by default.

Your options are:

Alternatively, if you control how your own script is invoked, you can invoke it with
bash -i script.

这篇关于脚本中的源.bashrc无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆