Assigning entire to a variable

Hi,

Need help on the below topic.

I am looping finger command and trying to get each line content into a variable. Output of finger command produces more than one line and each line was multiple words.

for get_line in `finger`
do
   echo "Finger Value: ${get_line}'"
done

The issue is entire line is not coming into $get_line, i am getting each word from a single line into a different line.

Please help me how to get entire line content into variable.

Thanks in advance.

Venkat

Please provide more input as to what exactly you want to do with the output of "finger" command. May be it'll help us to think of an alternate solution.

You can do a line by line parsing using awk or perl.

For e.g. finger | awk 'NR==1 {print}' would print just the first line from the output of finger equivalent to finger | head -1

You could do x=`finger` to get the entire output of finger in variable 'x'

1 Like
while read get_line
do
        echo "Finger Value: ${get_line}"
done < <(finger)
 
while read line
do
    myvar=$(finger $line)
    echo "$myvar"
done < userlist.txt

Hi balajesuri,

The finger output is as follows

$ finger
Login Name TTY Idle When Site Info
projdev US DEV projdev p2 Tue 19:13 , Created@03262008
projuser US DEV projuser p3 Tue 19:14 , Created@03262008
projdev US DEV projdev p4 Tue 19:15 , Created@03262008

I will pass username "projdev" to the script, the script pickup anyone line which has got this username and return name value (US DEV projdev)

for get_line in `finger`
do
  echo "Finger Value: ${get_line}'"
done

When i execute the above script, i am getting output like this....

$ sh b.sh
 
username: projdev
Finger Value1: Login'
Finger Value1: Name'
Finger Value1: TTY'
Finger Value1: Idle'
Finger Value1: When'
Finger Value1: Site'
Finger Value1: Info'
Finger Value1: projdev'
Finger Value1: US'
Finger Value1: DEV'
Finger Value1: projdev'
Finger Value1: p2'
Finger Value1: Tue'

I am looking for output like shown below

Finger Value1: Login Name TTY Idle When Site Info
Finger Value1: projdev US DEV projdev p2 Tue 19:13 , Created@03262008
Finger Value1: projuser US DEV projuser p3 Tue 19:14 , Created@03262008
Finger Value1: projdev US DEV projdev p4 Tue 19:15 , Created@03262008

Thanks in advance.

Venkat

Did you try the solution posted in post #3 by huaihaizi3?

Hi Balajesuri,

I tried that one, the result is not coming properly...

i=0
while read get_line
do
   echo "$i: Finger Value: ${get_line}"
   i=`expr $i + 1`
done < `finger`
 
 
b.sh[6]: Login                               Name               TTY Idle    When    Site Info^projdev                         US DEV projdev   p2         Tue 19:13   , Created@03262008: 0403-016 Cannot find or open the file.

Thanks,
Venkat

 
finger projdev | awk '{printf("Finger Value%s:%s",NR,$0)}'

@vfrg: Did you notice another redirection operator in huaihaizi3's solution?

done < <(finger)

Hi Balajesuri/Itkamaraj

Thank you very much to both of you.

I tried below code and it worked...

finger > t.txt
comp_str=projdev
i=0
while read line
do
    xx=`echo $line | grep -i $comp_str | wc -l`
    if [ $xx -gt 0 ]
    then
       #echo $i: Found match "line=>$line"
       match_str=$line
       break
    fi
    i=`expr $i + 1`
done < t.txt
echo "Final result=>"$match_str

Also, i tried solution provided by Itkamaraj, even that also worked for me.

Again, thanks so much for your timely help.

Thanks,
Venkat