Operating System - HP-UX
1837896 Members
3459 Online
110122 Solutions
New Discussion

Passing two-word input to shell script

 
Scott Downs
New Member

Passing two-word input to shell script

I have a personalized script that takes inputs using -p, -user, and -role. I am trying to pass a role called "Process Manager" (as in, script.sh -role "Process Manager"), but I only get the first word when I store $1 into a variable (after shifting to it). I have tried with quotes, and without, and each time I just get the first word.

Thank you for any advice!
9 REPLIES 9
Umapathy S
Honored Contributor

Re: Passing two-word input to shell script

hi Scott,
You can use getopt in shell scripts. Check the man page for details.

cheers
Umapathy
Arise Awake and Stop NOT till the goal is Reached!
Paula J Frazer-Campbell
Honored Contributor

Re: Passing two-word input to shell script

Hi

get it as two variables :-

$1 and $2

then $1+$2 = $3

then pass the variable $3.

Paula
If you can spell SysAdmin then you is one - anon
Darren Prior
Honored Contributor

Re: Passing two-word input to shell script

Hi,

$* will give you all the arguments.

regards,

Darren.
Calm down. It's only ones and zeros...
Bill Hassell
Honored Contributor

Re: Passing two-word input to shell script

text with whitespace is processed by the shell as separate words unless it is enclosed in quotes. This is true both for input as well as output. So if your script reads input from the command line, you must protect the whitespace to make it appear as a single string as in:

two strings "one string" one\ more\ string

In the above example, there are 4 strings. "one string" and one\ more\ string will each be assigned to a single variable.


Bill Hassell, sysadmin
Ralph Grothe
Honored Contributor

Re: Passing two-word input to shell script

Just a hint, in case you haven't yet stumbled accross it.

Have a look at the getopt program (see its manpage).
It makes parsing of commandline switches so much easier.

Basically, quoting strings that contain whitespace (e.g. "Process Manager"), or quoting each whitespace (e.g. Process\ Mangare) should deliver the argument as one token.
Madness, thy name is system administration
Ramkumar Devanathan
Honored Contributor

Re: Passing two-word input to shell script

hi,

if the script is called like this -

$ script.sh -p xxx -user yyy -role "hello world!"

then add this line to your script -

IFS='"' # IFS=internal field separator

works for me.

HTH.

- ramd.
HPE Software Rocks!
Ramkumar Devanathan
Honored Contributor

Re: Passing two-word input to shell script

that's a double quote (") within 2 single quotes (') btw.

- ramd.
HPE Software Rocks!
John Meissner
Esteemed Contributor

Re: Passing two-word input to shell script

script optionone optiontwo option three

optionvar1=$1
optionvar2=$2
optionvar3="$3 $4"

This should work as well
it sets a variable = to the command line options
All paths lead to destiny
john korterman
Honored Contributor

Re: Passing two-word input to shell script

Hi,
when you make the variable assignment of $1 in your script, is $1 qouted? It should be.

regards,
John K.
it would be nice if you always got a second chance