1755678 Members
4519 Online
108837 Solutions
New Discussion юеВ

Re: A "for" question

 
SOLVED
Go to solution
Jose Mosquera
Honored Contributor

A "for" question

Hi pals,

How I can proccess on the fly (without write a temp file) line-by-line and not space-by-space something like this into a "for" cycle:

SESSIONS=`who -u|sort`
for SESION in $SESSIONS
.
.
.
done

Rgds.
8 REPLIES 8
Pete Randall
Outstanding Contributor

Re: A "for" question

What portion of the who -u output do you want to process? Or do you want the whole line? The syntax

for SESION in `who -u |sort`

will give you the whole line, one by one.


Pete

Pete
Simon Hargrave
Honored Contributor

Re: A "for" question

You can simply get rid of the separate $SESSIONS variable, and put the backquotes directly in the for clause. As for processing line-by-line, you can set the IFS variable to be carriage-return instead of whitespace, eg: -

OIFS=$IFS
IFS="
"
for SESSION in `who -u | sort`
do
echo $SESSION
done
IFS=$OIFS

this will print one entire line per itteration of the for loop.

You can split this out into separate variables eg with

OIFS=$IFS
IFS="
"
for SESSION in `who -u | sort`
do
IFS=$OIFS
set $SESSION
echo "user: $1, client: $8"
done

Note the restting of IFS to the saved OIFS _within_ the loop, so that "set" uses the whitespace rather than carriage return as field separator.
Slawomir Gora
Honored Contributor
Solution

Re: A "for" question


who -u|sort | while read
do
SESSION=${REPLY}
echo $SESSION
done
Bharat Katkar
Honored Contributor

Re: A "for" question

Hi,
This should also work:

# who -u | sort | xargs

Regards,
You need to know a lot to actually know how little you know
Jannik
Honored Contributor

Re: A "for" question

If it is the user U want! else just change the $1 to something else.

for i in $(who -u | sort | awk '{print $1}')
do
.
.
.
done
jaton
Jose Mosquera
Honored Contributor

Re: A "for" question

Hi Pete,

for SESSION in `who -u|sort`
do
echo $SESSION
done

The output is:
user1
pts/tc
Oct
8
13:46
0:15
18272
12.4.49.29
user2
pts/tDb
Oct
8
07:19
0:14
29454
12.4.49.24

And I need this instead:
user1 pts/tc Oct 8 13:46 0:15 18272 12.4.49.29
user2 pts/tDb Oct 8 07:19 0:14 29454 12.4.49.24

Rgds.
Patrick Wallek
Honored Contributor

Re: A "for" question

How about something other than a for loop?

who -u | sort > /tmp/file
while read session
do
echo ${session}
done < /tmp/file
Jose Mosquera
Honored Contributor

Re: A "for" question

Thank you pals.