Operating System - Linux
1841319 Members
2409 Online
110179 Solutions
New Discussion

general scripting question

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

general scripting question

Hi all,

when I run a for statement and the line contains more than one field ...

can this line be displayed as 2 fields within a variable

ie

for aa in `ls -l`
do
echo $aa
done

root
system
705381
09
Aug
13:41
sortaudit4ge.aug09
-rw-r--r--
1
root
system
33280129
09
Aug
13:41

this is just and example btw.

Thanks

Chris.
hello
8 REPLIES 8
AwadheshPandey
Honored Contributor

Re: general scripting question

can u please paste the desired output.
It's kind of fun to do the impossible
Sandman!
Honored Contributor
Solution

Re: general scripting question

You would have to set the value of the IFS variable to some character that doesn't appear in the input stream i.e.


IFS="|"

for aa in `ls -l`
do
echo $aa
done
Oviwan
Honored Contributor

Re: general scripting question

lawrenzo_1
Super Advisor

Re: general scripting question

just what I was looking for.

Thanks for responses and solution.

Chris.
hello
James R. Ferguson
Acclaimed Contributor

Re: general scripting question

Hi Chris:

When you munge the shell IFS in a script, it's good practice to do something like this:

#!/usr/bin/sh
OLDIFS=${IFS}
IFS="|"
...
...
IFS=${OLDIFS}
...

Regards!

...JRF...

BTW: the "root system" ownership looks like AIX :-))


Dennis Handly
Acclaimed Contributor

Re: general scripting question

>when I run a for statement and the line contains more than one field ... can this line be displayed as 2 fields within a variable

You can of course use while read instead of "for i in $(); do":
ll | while read a b c; do

The delimited tokens are put in "a", "b" and the rest in "c".

You can of course pipe the output to awk and use its -F to separate tokens.
lawrenzo_1
Super Advisor

Re: general scripting question

Thanks again all.

Yes from the output it is AIX, I havent worked on a HP machine for 18 months now however moving jobs to a company that has 20 + HP servers ;=}
hello
lawrenzo_1
Super Advisor

Re: general scripting question

.
hello