Closed gregelin closed 10 years ago
This appears to work for OS X
sed -n -e "/^#/!s/\(VBOX_VERSION\)=\"\([0-9\.]\+\)\"/\1=\"${_vb_version}\"/g" "${__file}"
Could you please test these commands for me ?
List of sed
commands to try:
$ printf "4.5.16r5.5.7" | sed -n 's/\([0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/p'
$ printf "0.5.1r5.6.8" | sed 's/\([0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/p'
$ printf "0.5.1r5.9.5" | sed 's/\([0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/p' | tail -1
$ my_aa=$(printf "4.5.6r1.2.3" | sed -n 's/\([0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/p') && printf "${my_aa}"
$ printf " a1 a2 " | sed -e 's/[[:space:]]*$//g;s/^[[:space:]]*//g' && printf 'end'
$ printf " a1 a2 " | sed 's/[[:space:]]*$//g;s/^[[:space:]]*//g' && printf 'end'
$ printf " a1 a2 " | sed -e 's/[[:space:]]*$//g' -e 's/^[[:space:]]*//g' && printf 'end'
$ echo 'VBOX_VERSION="1.2.3" VBOX_VERSION="3.2.1"' > UGH.txt
$ echo '#VBOX_VERSION="0.2.3"' >> UGH.txt
$ mversion=4.3.9 && sed -i'' -e "/^#/!s/\(VBOX_VERSION\)=\"\([0-9\.]\+\)\"/\1=\"${mversion}\"/g" UGH.txt
$ cat UGH.txt
$ mversion=4.3.99 && sed -e "/^#/!s/\(VBOX_VERSION\)=\"\([0-9\.]\+\)\"/\1=\"${mversion}\"/g" UGH.txt
$ echo 'VBOX_VERSION="1.2.3" VBOX_VERSION="3.2.1"' > UGH2.txt
$ echo '#VBOX_VERSION="0.2.3"' >> UGH2.txt
$ mversion=7.9.9 && sed -i'' -e "/^#/"'!'"s/\(VBOX_VERSION\)=\"\([0-9\.]\+\)\"/\1=\"${mversion}\"/g" UGH2.txt
$ cat UGH2.txt
$ mversion=7.7.99 && sed -e "/^#/"'!'"s/\(VBOX_VERSION\)=\"\([0-9\.]\+\)\"/\1=\"${mversion}\"/g" UGH2.txt
$ echo 'VBOX_VERSION="1.2.3" VBOX_VERSION="3.2.1"' > UGH3.txt
$ echo '#VBOX_VERSION="0.2.3"' >> UGH3.txt
$ set +o histexpand && mversion=4.9.9 && sed -i'' -e "/^#/!s/\(VBOX_VERSION\)=\"\([0-9\.]\+\)\"/\1=\"${mversion}\"/g" UGH3.txt
$ cat UGH3.txt
$ set +o histexpand && mversion=4.9.99 && sed -e "/^#/!s/\(VBOX_VERSION\)=\"\([0-9\.]\+\)\"/\1=\"${mversion}\"/g" UGH3.txt
Some other test cases:
# try run twice:
$ vbkick on VM_NAME
$ vbkick on VM_NAME
# try run twice:
$ vbkick shutdown VM_NAME
$ vbkick shutdown VM_NAME
I was just exploring myself before your update. Here's my building of the regex:
mbair:vbkick greg$ _vb_version="4.3.12"
mbair:vbkick greg$ echo ${_vb_version}
4.3.12
mbair:vbkick greg$ echo 'VBOX_VERSION="4.3.4"' | sed 's/VBOX_VERSION="4.3.4"/VBOX_VERSION="4.3.12"/g'
VBOX_VERSION="4.3.12"
mbair:vbkick greg$ echo 'VBOX_VERSION="4.3.4"' | sed 's/\(VBOX_VERSION\)="4.3.4"/\1="4.3.12"/g'
VBOX_VERSION="4.3.12"
mbair:vbkick greg$ echo 'VBOX_VERSION="4.3.4"' | sed 's/\(VBOX_VERSION\)="\([0-9\.]*\)"/\1="4.3.12"/g'
VBOX_VERSION="4.3.12"
mbair:vbkick greg$ echo 'VBOX_VERSION="4.3.4"' | sed "s/\(VBOX_VERSION\)=\"\([0-9\.]*\)\"/\1=\"4.3.12\"/g"
VBOX_VERSION="4.3.12"
mbair:vbkick greg$ echo 'VBOX_VERSION="4.3.4"' | sed "s/\(VBOX_VERSION\)=\"\([0-9\.]*\)\"/\1=\"${_vb_version}\"/g"
VBOX_VERSION="4.3.12"
mbair:vbkick greg$ echo 'VBOX_VERSION="4.3.4"' | sed "s/^\(VBOX_VERSION\)=\"\([0-9\.]*\)\"/\1=\"${_vb_version}\"/g"
VBOX_VERSION="4.3.12"
mbair:vbkick greg$
mbair:vbkick greg$ echo '# Example:' | sed "s/^\(VBOX_VERSION\)=\"\([0-9\.]*\)\"/\1=\"${_vb_version}\"/g"
# Example:
mbair:vbkick greg$ echo 'export VBOX_VERSION' | sed "/^#/!s/\(VBOX_VERSION\)=\"\([0-9\.]\+\)\"/\1=\"${_vb_version}\"/g"
-bash: !s/\: event not found
mbair:vbkick greg$ echo 'export VBOX_VERSION' | sed "s/^\(VBOX_VERSION\)=\"\([0-9\.]\+\)\"/\1=\"${_vb_version}\"/g"
export VBOX_VERSION
mbair:vbkick greg$ printf "4.5.16r5.5.7" | sed -n 's/\([0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/p'
mbair:vbkick greg$ printf "0.5.1r5.6.8" | sed 's/\([0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/p'
0.5.1r5.6.8
mbair:vbkick greg$ printf "0.5.1r5.9.5" | sed 's/\([0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/p' | tail -1
0.5.1r5.9.5
mbair:vbkick greg$ my_aa=$(printf "4.5.6r1.2.3" | sed -n 's/\([0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/p') && printf "${my_aa}"
mbair:vbkick greg$ printf " a1 a2 " | sed -e 's/[[:space:]]*$//g;s/^[[:space:]]*//g' && printf 'end'
a1 a2
endmbair:vbkick greg$ printf " a1 a2 " | sed 's/[[:space:]]*$//g;s/^[[:space:]]*//g' && printf 'end'
a1 a2
endmbair:vbkick greg$ printf " a1 a2 " | sed -e 's/[[:space:]]*$//g' -e 's/^[[:space:]]*//g' && printf d'n
a1 a2
endmbair:vbkick greg$
mbair:vbkick greg$
mbair:vbkick greg$ echo 'VBOX_VERSION="1.2.3" VBOX_VERSION="3.2.1"' > UGH.txt
mbair:vbkick greg$ echo '#VBOX_VERSION="0.2.3"' >> UGH.txt
mbair:vbkick greg$ mversion=4.3.9 && sed -i'' -e "/^#/!s/\(VBOX_VERSION\)=\"\([0-9\.]\+\)\"/\1=\"${mversion}\"/g" UGH.txt
-bash: !s/\: event not found
mbair:vbkick greg$ cat UGH.txt
VBOX_VERSION="1.2.3" VBOX_VERSION="3.2.1"
#VBOX_VERSION="0.2.3"
mbair:vbkick greg$ mversion=4.3.99 && sed -e "/^#/!s/\(VBOX_VERSION\)=\"\([0-9\.]\+\)\"/\1=\"${mversion}\"/g" UGH.txt
-bash: !s/\: event not found
mbair:vbkick greg$
mbair:vbkick greg$ echo 'VBOX_VERSION="1.2.3" VBOX_VERSION="3.2.1"' > UGH2.txt
mbair:vbkick greg$ echo '#VBOX_VERSION="0.2.3"' >> UGH2.txt
mbair:vbkick greg$ mversion=7.9.9 && sed -i'' -e "/^#/"'!'"s/\(VBOX_VERSION\)=\"\([0-9\.]\+\)\"/\1=\"${mversion}\"/g" UGH2.txt
mbair:vbkick greg$ cat UGH2.txt
VBOX_VERSION="1.2.3" VBOX_VERSION="3.2.1"
#VBOX_VERSION="0.2.3"
mbair:vbkick greg$ mversion=7.7.99 && sed -e "/^#/"'!'"s/\(VBOX_VERSION\)=\"\([0-9\.]\+\)\"/\1=\"${mversion}\"/g" UGH2.txt
VBOX_VERSION="1.2.3" VBOX_VERSION="3.2.1"
#VBOX_VERSION="0.2.3"
mbair:vbkick greg$
mbair:vbkick greg$
mbair:vbkick greg$ echo 'VBOX_VERSION="1.2.3" VBOX_VERSION="3.2.1"' > UGH3.txt
mbair:vbkick greg$ echo '#VBOX_VERSION="0.2.3"' >> UGH3.txt
mbair:vbkick greg$ set +o histexpand && mversion=4.9.9 && sed -i'' -e "/^#/!s/\(VBOX_VERSION\)=\"\([0-9\.]\+\)\"/\1=\"${mversion}\"/g" UGH3.txt
-bash: !s/\: event not found
mbair:vbkick greg$ cat UGH3.txt
VBOX_VERSION="1.2.3" VBOX_VERSION="3.2.1"
#VBOX_VERSION="0.2.3"
mbair:vbkick greg$ set +o histexpand && mversion=4.9.99 && sed -e "/^#/!s/\(VBOX_VERSION\)=\"\([0-9\.]\+\)\"/\1=\"${mversion}\"/g" UGH3.txt
-bash: !s/\: event not found
mbair:vbkick greg$
mbair:CentOS6 greg$
mbair:CentOS6 greg$ vbkick on govready-centos6.5-min_x86-64
[INFO] Loading "definition.cfg" definition...
Waiting for VM "govready-centos6.5-min_x86-64" to power on...
VM "govready-centos6.5-min_x86-64" has been successfully started.
mbair:CentOS6 greg$ vbkick on govready-centos6.5-min_x86-64
[INFO] 'govready-centos6.5-min_x86-64' is already running...
mbair:CentOS6 greg$ vbkick ssh govready-centos6.5-min_x86-64
[INFO] Loading "definition.cfg" definition...
vagrant@127.0.0.1's password:
Last login: Fri May 30 02:35:36 2014 from 10.0.2.2
[vagrant@vagrant ~]$
mbair:CentOS6 greg$
mbair:CentOS6 greg$ vbkick shutdown govready-centos6.5-min_x86-64
[INFO] Loading "definition.cfg" definition...
[INFO] Shutting down 'govready-centos6.5-min_x86-64'.
[INFO] Exec: sudo /sbin/halt -h -p
vagrant@127.0.0.1's password:
Broadcast message from vagrant@vagrant
(/dev/pts/0) at 8:08 ...
The system is going down for power off NOW!
..........
[INFO] 'govready-centos6.5-min_x86-64' was shutdown cleanly.
mbair:CentOS6 greg$ vbkick shutdown govready-centos6.5-min_x86-64
[INFO] 'govready-centos6.5-min_x86-64' is already turn off.
mbair:CentOS6 greg$
I will try change all sed commands to be POSIX BRE compatybile or I will implement other solution like aliases base on sed version to support extended regular expressions.
I've cleared a bit this thread to make it more readable.
Maybe someone else benefit from the lesson which I've learned using sed
:
[0-9]
instead of POSIX character classes [:digit:] - POSIX not always mean portable (feel free to check character classes on solaris - echo "State: running " | grep "State:[[:space:]][[:space:]]*running"
).GNU sed
has a different option than BSD sed
to turn on Extended Regular Expressions.Summary: commands which works for SmartOS (Solaris derivate), FreeBSD, GNU/Linux:
$ echo " aa1 bb1 " | sed 's/[ ]*$//g;s/^[ ]*//g'
aa1 bb1
$ echo " aa1 bb1 " | sed 's/[ ]*$//g;s/^[ ]*//g' | od -c
0000000 a a 1 b b 1 \n
0000011
$ kvar=$(printf " aa1 bb1 " | sed 's/[ ]*$//g;s/^[ ]*//g')
$ printf "${kvar}" | od -c
0000000 a a 1 b b 1
0000010
$ echo "State: running " | sed 's/State:[[:space:]][[:space:]]*//g' | od -c
0000000 r u n n i n g \n
0000011
$ version=5.6.67r55678.888.999 && printf "${version}\n" | sed -n 's/\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*/\1/p'
5.6.67
$ echo 'VBOX_VERSION="1.2.3" VBOX_VERSION="3.2.1"' > UGH.txt
$ echo '#VBOX_VERSION="0.2.3"' >> UGH.txt
$ echo 'VBOX_VERSION="1.2.3" VBOX_VERSION="3.2.1"' >> UGH.txt
# Note here: `'!'`, without `'` `!` (in bash) is interpreted as a special character used to refer to the previous command if history expansion is enabled (by default is for interactive shell)
$ _vb_version=9.9.99 && sed -i'' -e "/^#/"'!'"s/\(VBOX_VERSION\)=\"\([0-9.][0-9.]*\)\"/\1=\"${_vb_version}\"/g" UGH.txt
$ cat UGH.txt
VBOX_VERSION="9.9.99" VBOX_VERSION="9.9.99"
#VBOX_VERSION="0.2.3"
VBOX_VERSION="9.9.99" VBOX_VERSION="9.9.99"
_vb_version=9.9.77 && sed -e "/^#/"'!'"s/\(VBOX_VERSION\)=\"\([0-9.][0-9.]*\)\"/\1=\"${_vb_version}\"/g" UGH.txt
VBOX_VERSION="9.9.77" VBOX_VERSION="9.9.77"
#VBOX_VERSION="0.2.3"
VBOX_VERSION="9.9.77" VBOX_VERSION="9.9.77"
useful link:
Please feel free to test vbkick with enabled files_to_autoupdate_vbox_version
files_to_autoupdate_vbox_version
not commented out worked fine in OS X 10.9.2 in pre version 0.7 release.
Cool :)