Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

kshell - separating parameters to a program 2

Status
Not open for further replies.

philipose

Programmer
Dec 24, 2003
137
US
I have a k shell scripting issue. I have arguments to a program as given below.

first second "third argument"

I need to separate them into first, second and "third argument". Can anyone suggest to do the same??

When I use the for construct as below

for file in "$*"; do
....
....
done

I get first, second, third and argument.
Any help or pointers will be appreciated.


Philipose
 
for file
do
echo file is \"$file\"
done

should do it, list of positional parameters is the default list for "for" construct.


HTH,

p5wizard
 
The [tt]read[/tt] command will stuff any extra values into the last variable listed, so you can do this:

Code:
#!/bin/ksh

echo "$*" | read first second third

echo "First arg: $first"
echo "Second arg: $second"
echo "Third arg: $third"

- Rod

IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

Wish you could view posts with a fixed font? Got Firefox & Greasemonkey? Give yourself the option.
 
Try this on your /etc/hosts file.

awk '{print "IP", $1," ","ADDR",$2," ","ALIAS",$3}' /etc/hosts

HTH
 
You need to understand the differences between "$*" and "$@" (which can only be used as "$@" or it reverts to a $*). The "$@" returns the arguments in a quoted form.
Code:
for argtext in "$@"; do
   let index=${index:-0}+1
   echo "Argument ${index}: is '${argtext}'"
done
for example:
Code:
$ my_cmd one two "three has four words" four
Argument 1: is 'one'
Argument 2: is 'two'
Argument 3: is 'three has four words'
Argument 4: is 'four'
$
 
oiihob,
Thanks very much. That was good to know
philipose
 
Use an array:
set -A args $(ls /path)
echo ${args[@]} or ${args[*]}

The difference between ${args[@]} and ${args[*]} is given in this example using (1 2 3):
${args[@]} = "1" "2" "3"
${args[*]} = "1 2 3"

Then reference them individually if you want:
echo ${args[0]} ${args[1]} ${args[2]} ...

You can also return the number of elelments in the array:
${#args[@]}

To use in a script, something like this:
i=0
while (( i < numArgs )); do
echo ${args}
((i++))
done ## where numArgs = ${#args[*]}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top