wilas / vbkick

Tool for building and maintaining VirtualBox VMs described as a code in a single definition file.
Other
8 stars 5 forks source link

sed: 1: "postinstall/adm_envrc": extra characters at the end of p command #44

Closed gregelin closed 10 years ago

gregelin commented 10 years ago
mbair:vbkick greg$ cd examples/SL6_provisioner/
mbair:SL6_provisioner greg$ vbkick build myWorkingVM
[INFO] Loading "definition.cfg" definition...
sed: 1: "postinstall/adm_envrc": extra characters at the end of p command
[INFO] Signal handler - cleanup before exiting...
gregelin commented 10 years ago

This appears to work for OS X

sed -n -e "/^#/!s/\(VBOX_VERSION\)=\"\([0-9\.]\+\)\"/\1=\"${_vb_version}\"/g" "${__file}"
wilas commented 10 years ago

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
gregelin commented 10 years ago

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
gregelin commented 10 years ago
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$
gregelin commented 10 years ago
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$
gregelin commented 10 years ago
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$
gregelin commented 10 years ago
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 ~]$
gregelin commented 10 years ago
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$
wilas commented 10 years ago

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.

wilas commented 10 years ago

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:

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:

wilas commented 10 years ago

Please feel free to test vbkick with enabled files_to_autoupdate_vbox_version

gregelin commented 10 years ago

files_to_autoupdate_vbox_version not commented out worked fine in OS X 10.9.2 in pre version 0.7 release.

wilas commented 10 years ago

Cool :)