Bash string manipulation

In my script I'm retrieving a parameter through an API call. I need to trim some things out of the result so I can use it as a parameter to pass to another process. I've got it working but it's pretty kludgy and I'm hoping someone can help me with a better way.

This is the code that retrieves the parameter:

  vmx=`/usr/lib/vmware-vcli/apps/vm/vminfo.pl --url https://"$1":443/sdk/webService --username "$esxiuser" --password "$esxipass" --vmname "$2" --fields vmPathName`
  vmx1=[${vmx##*[}
  vmx2=${vmx1% *}

And this is the result of vmx, vmx1, & vmx2:

vmx='vmPathName:                 [NFS1] lab-xp03/lab-xp03.vmx '
vmx1='[NFS1] lab-xp03/lab-xp03.vmx '
vmx2='[NFS1] lab-xp03/lab-xp03.vmx'

vmx2 is the result I need to pass on.

Thanks for taking a look!

h

Your current sh solution is as efficient as it gets. I would just stick with it.

However, you could accomplish the same task with a much more expensive pipeline that funnels the output of vminfo.pl into sed 's/[^[]*//; s/ $//' .

Regards,
Alister

1 Like

Thanks alister, glad to know it's a good way to achieve my goal. I'm pretty new (i.e. a couple of days) to bash scripting so I'm on a huge learning curve right now :slight_smile:

h