variable not being used correctly by grep in shell script

mickt
mickt used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Nick UpsonPrincipal Operations Engineer

Commented:
can you provide examples of input and add an echo of $1 to your script so we can see whats wrong with it
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
How is $1 being used, and what would be the correct use?

Author

Commented:
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.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Nick UpsonPrincipal Operations Engineer

Commented:
please post the script that produced that output

Author

Commented:
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
    }
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
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?

Author

Commented:
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
Most Valuable Expert 2014
Top Expert 2015
Commented:
try
close-medium $1 $2

Author

Commented:
:-)       that worked fine

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial