Avatar of mickt
mickt
Flag 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.
Shell ScriptingLinux

Avatar of undefined
Last Comment
mickt

8/22/2022 - Mon
Nick Upson

can you provide examples of input and add an echo of $1 to your script so we can see whats wrong with it
ozo

How is $1 being used, and what would be the correct use?
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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Nick Upson

please post the script that produced that output
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
    }
ozo

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?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
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
ozo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
mickt

ASKER
:-)       that worked fine

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