1835601 Members
3162 Online
110079 Solutions
New Discussion

scripting question

 
Smucker
Regular Advisor

scripting question

I need to be able to take on the command line of a command and later spit it back out. The problem is there are quotes whitespaces that need to be handled....

example

xxxxx.sh -a -o param -h -n "title name" filename

any ideas
4 REPLIES 4
Steven E. Protter
Exalted Contributor

Re: scripting question

Shalom,

scripting 101

spaced arguments are automatically put in variables, in the order on the command line left to right.

scriptname parm1 parm2 parm3

echo $1
# shows parm1

etc

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Bill Hassell
Honored Contributor

Re: scripting question

Scripting 102...the entire command line parameter list can be seen with $@ so you can write your script like this:

MYPARMS="$@"

and later:

echo "parms= $ABC"

Note the prolific use of " marks. The quote marks will preserve embedded spaces. Now asa Steven mentioned, the parameters will be separated with white space so if you put extra spaces (or tabs) between values on the line, they will not be preserverd, but the spaces inside a parameter enclosed with quote marks will be preserved.


Bill Hassell, sysadmin
James R. Ferguson
Acclaimed Contributor

Re: scripting question

Hi:

# cat ./seeargs
#!/usr/bin/sh
echo "----------"
for X in "$@"
do
echo $X
done
echo "----------"

# ./seeargs -a -o param -h -n "title name" filename
----------
-a
-o
param
-h
-n
title name
filename
----------

Regards!

...JRF...
Tingli
Esteemed Contributor

Re: scripting question

Again, you can use either quote or escape to do the job.

xxxxx.sh -a -o param -h -n \"title name\" filename

xxxxx.sh -a -o param -h -n '"title name"' filename