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.
micktAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nick UpsonPrincipal Operations EngineerCommented:
can you provide examples of input and add an echo of $1 to your script so we can see whats wrong with it
ozoCommented:
How is $1 being used, and what would be the correct use?
micktAuthor 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.
Determine the Perfect Price for Your IT Services

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

Nick UpsonPrincipal Operations EngineerCommented:
please post the script that produced that output
micktAuthor 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
    }
ozoCommented:
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?
micktAuthor 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
ozoCommented:
try
close-medium $1 $2

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
micktAuthor Commented:
:-)       that worked fine

Is this necessary, should it not take it from outside function?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.