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.
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'
$ 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....
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
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.
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.