简体   繁体   中英

how to make vagrant use bash and resolve `[[: not found`?

I am using vagrant and my script outputs [[: not found .

I understand it is because I am using shell for script that should use bash.

I made sure I have a shebang for bash #!/usr/bin/env bash but I am still getting that error.

I tried replacing config.vm.provision "shell" to config.vm.provision "bash" but got provisioner could not be found.

What am I supposed to do?

details about the script

I am using cloudbees' node installation snippet

curl -s -o ./use-node https://repository-cloudbees.forge.cloudbees.com/distributions/ci-addons/node/use-node
NODE_VERSION=0.10.24 . ./use-node

The use-node script has the line if [[ -z $NODE_VERSION ]]; then if [[ -z $NODE_VERSION ]]; then

I am aware I can install node myself - that would definitely be the work around unless I get vagrant to execute with bash like I want.

If the script only contains trivial bashisms, you can simply rewrite it to sh -compatible, and change the shebang to #!/bin/sh .

Change

if [[ -z $NODE_VERSION ]]; then

to

if [ -z "$NODE_VERSION" ]; then

(The double quotes should properly be used in the Bash version too.)

However, recognizing and fixing Bashisms in a large script is potentially a nontrivial endeavour.

If the script is specifically bash-only, you can create a wrapper for it, like

#!/bin/sh
exec env bash /path/to/real/script

which solves the problem at the cost of an ugly extra layer of wrapping. (The env is not strictly necessary here if you only used it to avoid having to put in the full path to bash , but there are probably situations where it has other useful effects, too.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM