LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-18-2012, 01:29 PM   #1
withanh
LQ Newbie
 
Registered: Sep 2008
Location: In my own private hell
Distribution: Debian
Posts: 9

Rep: Reputation: 0
Passing parameters to bash script function (or subroutine)


I'm kind of stuck on a subroutine that I'm writing in a bash script. I have the script commented with the error lines and the lines that work. Hopefully someone will be able to tell me why the lines with the parameters are not working.

Thanks!

Code:
#!/bin/bash

function waitforvmtools {
# this line works
  /usr/bin/vmware-cmd -H lab-esxi1 -U root -P 'pass!word' '[NFS2] lab-vcenter/lab-vcenter.vmx' gettoolslastactive
# this line should be the same but does not work does not work
  /usr/bin/vmware-cmd -H $1 -U root -P 'pass!word' $2 gettoolslastactive
# but this shows the proper parameters being passed
echo /usr/bin/vmware-cmd -H $1 -U root -P 'pass!word' $2 gettoolslastactive

# this is what I'm ultimately trying to do with it
#  toolstate=`/usr/bin/vmware-cmd -H $1 -U root -P 'pass!word' $2 gettoolslastactive`
#  until [  "$toolstate" = "gettoolslastactive() = 1" ]; do
#    sleep 5
#    toolstate=`/usr/bin/vmware-cmd -H $1 -U root -P 'pass!word' $2 gettoolslastactive`
#    echo $toolstate
#  done
}

waitforvmtools lab-esxi1 "'[NFS2] lab-vcenter/lab-vcenter.vmx'"
 
Old 02-18-2012, 02:09 PM   #2
PDock
Member
 
Registered: Aug 2004
Distribution: Slack10 & curr. tried numerous
Posts: 189

Rep: Reputation: 37
Have you echoed $2 to see that the space between [NFS2] and lab.... is not messing things up?
 
1 members found this post helpful.
Old 02-18-2012, 04:22 PM   #3
withanh
LQ Newbie
 
Registered: Sep 2008
Location: In my own private hell
Distribution: Debian
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by PDock View Post
Have you echoed $2 to see that the space between [NFS2] and lab.... is not messing things up?
Yes, if I echo the full command line:
Code:
echo /usr/bin/vmware-cmd -H $1 -U root -P 'pass!word' $2 gettoolslastactive
it resolves to:
Code:
/usr/bin/vmware-cmd -H lab-esxi1 -U root -P 'pass!word' '[NFS2] lab-vcenter/lab-vcenter.vmx' gettoolslastactive
The space in there is required, it's how VMware reads the datastore name and file path. If I copy the output from the echo command and paste it to the command prompt, it properly returns the status.

h
 
Old 02-19-2012, 04:31 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
When you call the function with this parameter:

Code:
"'[NFS2] lab-vcenter/lab-vcenter.vmx'"
The single quote marks, being escaped by the double-quotes, become literal, and are stored in the variable along with the rest of the string. These are then NOT re-parsed later (generally) as shell syntax. So when you launch the command like this...

Code:
/usr/bin/vmware-cmd -H $1 -U root -P 'pass!word' $2 gettoolslastactive
...the $2 string is passed to the vmware-command as including those literal quote marks.

The echoed command may look the same by eyeball, but to the shell they are completely different, due to the line parsing order. Quotes are processed before variable expansion, so the shell never has a chance to parse the ones stored in the variable.

In addition, since you didn't quote the $2 parameter, the space is processed and the contents are word-split, because word splitting happens after parameter expansion.

So you're actually passing the following two literal strings to vmware-cmd:

Code:
'[NFS2]
lab-vcenter/lab-vcenter.vmx'
It will likely start working if you remove the extra set of quotes from the passed parameter, and double-quote the "$2".

BTW, it's a good idea to train yourself to always quote your variables, parameters, and other expansions, unless and until you actually need word-splitting to occur.

See here for more on how the shell parses arguments:

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

This link goes into more detail about how it's generally not advisable to try storing shell commands/syntax inside variables:

http://mywiki.wooledge.org/BashFAQ/050

Last edited by David the H.; 02-19-2012 at 04:54 AM. Reason: Rewording for better description
 
1 members found this post helpful.
Old 02-19-2012, 08:53 AM   #5
withanh
LQ Newbie
 
Registered: Sep 2008
Location: In my own private hell
Distribution: Debian
Posts: 9

Original Poster
Rep: Reputation: 0
Thank you for the explanation, that was exactly the issue. I am sure you can tell I'm pretty new to scripting in Linux and the explanation & links you provided will help me greatly.

Thanks again!

Final script version:
Code:
#!/bin/bash

function waitforvmtools {
  toolstate=`/usr/bin/vmware-cmd -H "$1" -U root -P 'pass!word' "$2" gettoolslastactive`
  until [  "$toolstate" = "gettoolslastactive() = 1" ]; do
    sleep 5
    toolstate=`/usr/bin/vmware-cmd -H "$1" -U root -P 'pass!word' "$2" gettoolslastactive`
  done
}

waitforvmtools lab-esxi1 "[NFS2] lab-vcenter/lab-vcenter.vmx"
 
Old 02-19-2012, 08:54 AM   #6
withanh
LQ Newbie
 
Registered: Sep 2008
Location: In my own private hell
Distribution: Debian
Posts: 9

Original Poster
Rep: Reputation: 0
Thumbs up for PDock

Quote:
Originally Posted by PDock View Post
Have you echoed $2 to see that the space between [NFS2] and lab.... is not messing things up?
Thanks for your reply as well, it appears you were on the right track, I just didn't fully understand how things worked.

h
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Passing parameters to script -help flea89 Linux - Newbie 4 03-13-2010 12:47 PM
passing array and variable to function in bash script ajaypitroda Programming 2 07-07-2009 11:10 PM
Passing parameters to bash script Kamikazee Programming 4 10-01-2005 06:41 AM
Bash Script Passing variable to Function nutthick Programming 2 02-02-2005 05:15 AM
Passing Parameters to Bash Script mooreted Linux - Software 3 04-05-2004 09:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration