linux - 在 Travis-CI 中采购 .bashrc 不工作

标签 linux bash shell travis-ci

最近几天我一直在尝试为我的 bash 脚本项目设置一个 Travis-CI 构建。我在将别名粘贴到 .bashrc 中时遇到问题,该别名存在于 Travis 构建中而不是采购中。

下面是我在 Linux 上的 .bashrc 文件中创建 bash 别名的简单示例,但尝试失败。

Travis-CI (.travis.yaml):

language: bash

git:
  quiet: true
  submodules: false

matrix:
  include:
    - os: linux
      dist: xenial

script:
  - sh test_bash.sh || travis_terminate 1;
  - bash test_sourcing.sh || travis_terminate 1;

test_bash.sh:

current_shell=$(echo $SHELL)
if [ "$current_shell" != "/bin/bash" ]; then
    echo "The current build is not working with the Bash Shell."
    exit 1
fi 

test_sourcing.sh

alias name='echo "John Doe"' >> $HOME/.bashrc
source $HOME/.bashrc
output=$(name)
if [ "$output" != "John Doe" ]; then
    echo "Sourcing is not working for some reason."
    exit 1
fi 

我从构建的输出中得到以下内容:

$ bash -c 'echo $BASH_VERSION'
3.2.57(1)-release
0.02s$ sh test_bash.sh || travis_terminate 1;
The command "sh test_bash.sh || travis_terminate 1;" exited with 0.
$ bash test_sourcing.sh || travis_terminate 1;
test_sourcing.sh: line 3: name: command not found
Sourcing is not working for some reason.

我希望通过所有测试,但我很难理解如此简单的功能。我唯一能想到的是 BASH 的版本是不支持别名的版本。感谢您的帮助!

最佳答案

您可以使用简单的变量或通过 eval 执行它们,这与您想要的很相似。

查找 name2 以及带和不带 eval 的两个选项。 name1 是您的代码。

$ cat test_sourcing.sh
set -x
alias name1='echo "John Doe"' >> $HOME/.bashrc
name2='echo "John Doe"' >> $HOME/.bashrc
source $HOME/.bashrc
output=$($name1)
output=$($name2)
output=$(eval $name2)
if [ "$output" != "John Doe" ]; then
    echo "Sourcing is not working for some reason."
    exit 1
fi

运行脚本时可以看到:

$ ./test_sourcing.sh
++ alias 'name1=echo "John Doe"'
++ name2='echo "John Doe"'
++ source /home/schroen/.bashrc
+++ case $- in
+++ return
++ output=
+++ echo '"John' 'Doe"'
++ output='"John Doe"'
+++ eval echo '"John' 'Doe"'
++++ echo 'John Doe'
++ output='John Doe'
++ '[' 'John Doe' '!=' 'John Doe' ']'
  • 第一个输出是空的,因为别名没有展开(就像 chepner 说的)
  • 第二个输出设置为“John Doe”,但直接运行 echo。 (注意变量值里面的")
  • 第三个输出也设置为“John Doe”,但是通过运行回显的 eval。有时这在使用其他变量构建变量名时很重要。

这个eval 东西...

$ cat variable-loop.sh
#!/usr/bin/env bash
#set -x

dev1=foo
dev2=bar
dev3=foobar

echo 'without eval not what we want...'
for i in $(seq 1 3); do
        echo dev$i
        echo $dev$i
done

echo 'with eval it is working...'
for i in $(seq 1 3); do
        eval echo \$dev$i
done

$ ./variable-loop.sh 
without eval not what we want...
dev1
1
dev2
2
dev3
3
with eval it is working...
foo
bar
foobar

关于linux - 在 Travis-CI 中采购 .bashrc 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56974682/

相关文章:

sql - 如何为 ssh 使用 sql 数据库?

node.js - Raspberry pi 3 上 Node.js 的最佳环境

bash - Dockerfile - 如何将答案传递给 apt-get install 后的提示?

java - 打印 ASM 代码以生成给定的类用法 : ASMifier [-debug] <fully qualified class name or class file name>

linux - 以 root 身份运行 bash 安装脚本 - 如何处理普通用户的文件?

Bash:将标准输入复制到文件

linux - 在 linux 上删除包含子文件夹和文件的文件夹

linux - 递归检查shell中文件之间的差异

linux - shell 脚本 : Using own IP adresss as variable

linux - linux中ctrl-z和 "&"的区别