Running commands or script lines with simple tests, like -e
, in vagrant
using the ssh
subcommand works fine (eg
vagrant ssh -c 'if ! [ -e file.deb ] ; then wget http://a.b/file.deb; fi'
as soon as string comparison and command execution bash
quoting gets involved, complexity immediately rises to the sky. I tried
bash
is just inacceptable) writing everything in a script file with cat <<EOF > file [code]EOF
idiom, eg
vagrant ssh -c 'cat <<EOF > fetch.sh \\ #!/bin/bash \\ if [ "$(md5sum file.deb | cut -d \\" \\" -f 1)" != "d757d51819f8f945ae7715b532ab8d2e" ] ; then wget http://a/b/file.deb; fi \\ EOF' vagrant ssh -c 'bash ./fetch.sh'
causes ./fetch.sh: line 1: syntax error near unexpected token `if'
cat EOF
idiom implies failure of any sort of script created in vagrant
( python
, groovy
, etc. would suffer from the same incapacity of bash
to provide usable string quoting/be usable at all) Is there any way to execute script code with complex statements in tests inside a vagrant
box? Do I seriously need to transfer the script via the localhost interface (using FTP or something similar)?
With knowledge of How to use SSH to run a shell script on a remote machine? it's possible to connect to vagrant using
ssh -p [port] -i [keyfile] vagrant@localhost 'bash -s' < script.sh
(with [port]
and [keyfile]
retrieved from vagrant ssh-config
) and the script code put in script.sh
.
The port can change if multiple vagrant
instances are running (each starting its own SSH server), so you might need to parse vagrant ssh-config
.
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.