简体   繁体   中英

escaping spaces and other special characters in cygwin shell script

I am pulling my hair out try to get a script to work on cygwin. Here's the latest version of the script I am trying to run:

$ cat start_vm_2.sh
#!/bin/sh

VMRUN='/cygdrive/c/\"Program Files (x86)\"/VMware/VMware\ VIX/vmrun"'
echo "VMRUN is [$VMRUN]"
ARGS='-T ws start \"C:\\Users\\red\\Documents\\Virtual Machines\\myvm-dev-006 \(2\)\\myvm-dev-006 \(2\).vmx\"'
echo "ARGS is [$ARGS]"

And this is the error message I get:

$ ./start_vm_2.sh
VMRUN is [/cygdrive/c/\"Program Files (x86)\"/VMware/VMware\ VIX/vmrun"]
ARGS is [-T ws start \"C:\\Users\\red\\Documents\\Virtual Machines\\myvm-dev-006 \(2\)\\myvm-dev-006 \(2\).vmx\"]
./start_vm_2.sh: line 8: /cygdrive/c/\"Program: No such file or directory

You should run it as bash instead and store your arguments as arrays. Also, do not add literal quotes to your spaces:

#!/bin/bash
VMRUN="/cygdrive/c/Program Files (x86)/VMware/VMware VIX/vmrun"
echo "VMRUN is [$VMRUN]"
ARGS=(-T ws start 'C:\Users\red\Documents\Virtual Machines\myvm-dev-006 (2)\myvm-dev-006 (2).vmx')
echo "ARGS is [${ARGS[*]}]"
"$VMRUN" "${ARGS[@]}"

Run bash script.sh .

this might be your problem... I was having the same issue until I figured out my script had Windows special characters (cat -e script.ksh)... so I did a dos2unix to the file and it started to flow as I wanted

Hope this is useful

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