[英]Sourcing .bashrc in Travis-CI not working
最近幾天,我一直在嘗試為bash腳本項目設置Travis-CI構建。 我在將別名粘貼到生活在Travis構建中而不是進行采購的.bashrc中時遇到問題。
下面是我在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' ']'
"
) 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.