Link to home
Start Free TrialLog in
Avatar of mickt
micktFlag for Ireland

asked on

variable not being used correctly by grep in shell script

I'm writing a script to resize vagrant boxes.  I'm struggling with a section, which is supposed to close media.  I provide the path to the vmdk file and the new size to the script.

An example path is /root/.vagrant.d/boxes/test-80/1.0.0/virtualbox/packer-centos-7.0-x86_64-disk1.vmdk

The section is as follows:

for disk in `$execsudo vboxmanage list hdds | grep -B4 $1 | grep - | grep UUID | awk \ '{print $2}'`
  do
      $execsudo vboxmanage closemedium disk $disk
  done

It is not correctly using $1.  I've tried various quoting etc.  I'd appreciate some assistance.
Avatar of Nick Upson
Nick Upson
Flag of United Kingdom of Great Britain and Northern Ireland image

can you provide examples of input and add an echo of $1 to your script so we can see whats wrong with it
How is $1 being used, and what would be the correct use?
Avatar of mickt

ASKER

usage:
./resizeVmdk.sh /root/.vagrant.d/boxes/test-80/1.0.0/virtualbox/packer-centos-7.0-x86_64-disk1.vmdk 80

An important point that I forgot to mention is that this part is in a function.  It is not being picked up in the function. It is outside the function.

Here is output from the script:

+ echo input=/root/.vagrant.d/boxes/test-80/1.0.0/virtualbox/packer-centos-7.0-x86_64-disk1.vmdk
input=/root/.vagrant.d/boxes/test-80/1.0.0/virtualbox/packer-centos-7.0-x86_64-disk1.vmdk
+ echo path=/root/.vagrant.d/boxes/test-80/1.0.0/virtualbox
path=/root/.vagrant.d/boxes/test-80/1.0.0/virtualbox
+ /bin/sudo rm -f /tmp/cloned.vdi
+ echo 'Backing up vmdk file ....'
Backing up vmdk file ....
+ /bin/sudo cp /root/.vagrant.d/boxes/test-80/1.0.0/virtualbox/packer-centos-7.0-x86_64-disk1.vmdk /root/.vagrant.d/boxes/test-80/1.0.0/virtualbox/packer-centos-7.0-x86_64-disk1.vmdk.orig
+ echo 'Cloning vmdk file ....'
Cloning vmdk file ....
+ /bin/sudo vboxmanage clonehd /root/.vagrant.d/boxes/test-80/1.0.0/virtualbox/packer-centos-7.0-x86_64-disk1.vmdk /tmp/cloned.vdi --format vdi
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone hard disk created in format 'vdi'. UUID: e498adc1-490a-4580-801b-58dec6590bcb
+ echo 'Resizing cloned file ....'
Resizing cloned file ....
+ /bin/sudo vboxmanage modifyhd /tmp/cloned.vdi --resize 81920
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
+ close-medium
+ echo '++++++++++ Added pre loop echo here +++++++++++++'
++++++++++ Added pre loop echo here +++++++++++++
+ echo

NOTHING !!

+ echo '++++++++++ Added pre loop echo here +++++++++++++'
++++++++++ Added pre loop echo here +++++++++++++
++ /bin/sudo vboxmanage list hdds
++ grep -B4
++ grep -
++ grep UUID
++ awk ' {print $2}'
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
please post the script that produced that output
Avatar of mickt

ASKER

if [[ ! `$execsudo ls $1` ]]
  then
    echo "vmdk file does not exist.  Exiting."
    exit 1

  else
    path=`echo $1 | sed -e "s/\/[^\/]*$//"`
    file=`echo $1 | awk -F "/" '{print $NF}'`

# - this is for test
echo input=$1
echo path=$path
# -

    close-medium () {
     for disk in `$execsudo vboxmanage list hdds | grep -B4 $1 | grep - | grep UUID | awk \ '{print $2}'`
        do
          $execsudo vboxmanage closemedium disk $disk
        done

      for clone in `$execsudo vboxmanage list hdds | grep -B4 cloned.vdi | grep - | grep UUID | awk \ '{print $2}'`
        do
          $execsudo vboxmanage closemedium disk $clone

        done
    }
What part of that script produced the output
+ /bin/sudo vboxmanage modifyhd /tmp/cloned.vdi --resize 81920
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
+ close-medium
+ echo '++++++++++ Added pre loop echo here +++++++++++++'
++++++++++ Added pre loop echo here +++++++++++++
+ echo
?
Where was close-medium called?
Avatar of mickt

ASKER

Sorry.  Following from function


    # Remove any exisitng clone
    $execsudo rm -f /tmp/cloned.vdi > /dev/null 2>&1

    echo "Backing up files (will not overwrite original backups!) ...."
    $execsudo cp $1 $1.orig > /dev/null 2>&1
    $execsudo cp $path/box.ovf $path/box.ovf.orig > /dev/null 2>&1

    echo "Cloning vmdk file ...."
    $execsudo vboxmanage clonehd $1 /tmp/cloned.vdi --format vdi

    echo "Resizing cloned file ...."
    $execsudo vboxmanage modifyhd /tmp/cloned.vdi --resize $(($2*1024))

    close-medium

    # Remove vmdk file
    $execsudo rm -f $1
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mickt

ASKER

:-)       that worked fine

Is this necessary, should it not take it from outside function?