Assigning entire to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assigning entire to a variable
# 1  
Old 01-10-2012
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.
Code:
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

Last edited by Franklin52; 01-11-2012 at 03:24 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 01-10-2012
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'
This User Gave Thanks to balajesuri For This Post:
# 3  
Old 01-10-2012
Code:
while read get_line
do
        echo "Finger Value: ${get_line}"
done < <(finger)

# 4  
Old 01-10-2012
Code:
 
while read line
do
    myvar=$(finger $line)
    echo "$myvar"
done < userlist.txt

# 5  
Old 01-10-2012
Hi balajesuri,

The finger output is as follows
Code:
$ 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)

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

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

Code:
$ 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
Code:
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

Last edited by Franklin52; 01-11-2012 at 03:25 AM.. Reason: Please use code tags for code and data samples, thank you
# 6  
Old 01-11-2012
Did you try the solution posted in post #3 by huaihaizi3?
# 7  
Old 01-11-2012
Hi Balajesuri,

I tried that one, the result is not coming properly...
Code:
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

Last edited by Franklin52; 01-11-2012 at 03:26 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning a variable

I have a date column as 06302015 but I need to have variable which extracts 063015. Am trying something like below but it is not assigning Please let me know if am missing something. Thanks in advance. ################################ #!/usr/bin/ksh DT=06302015 ... (7 Replies)
Discussion started by: weknowd
7 Replies

2. Shell Programming and Scripting

Assigning value to a variable

Unable to get the value to a variable. set -x cd $HOME echo "Enter the server name" read a echo $a i=4 j=1 k = ps -ef | awk '/server1/{ print $4 }' | tail -$i | head -$j` echo $k When I do the same in command line it works, however the same does not work when I provide that in the... (1 Reply)
Discussion started by: venkidhadha
1 Replies

3. Shell Programming and Scripting

Assigning a value to a variable

Hi I have a script that accepts an input date in YYYY-MM-DD format. After that, I used sed to delete the hyphen (-) which gives me an output YYYY MM DD. My question is, how can I assign those three numbers to a three different variable. Example: 2013-11-23 will become 2013 11 23... (4 Replies)
Discussion started by: erin00
4 Replies

4. Shell Programming and Scripting

problem in assigning value to variable have value fo other variable

my script is some thing like this i11="{1,2,3,4,5,6,7,8,9,10,11,}" echo "enter value" read value ..............suppose i11 x="$value" echo "$($value)" .............the echo should be {1,2,3,4,5,6,7,8,9,10,11,} but its showing "i11" only. plz help me out to get desired... (10 Replies)
Discussion started by: sagar_1986
10 Replies

5. Shell Programming and Scripting

Removing a character from a variable and assigning it to another variable?

Hi folks. I have this variable called FirstIN that contains something like this: 001,002,003,004... I am trying to assign the content of this variable into ModifiedIN but with the following format : 001 002 003 004...(changing the commas for spaces) I thought about using sed but i am not... (17 Replies)
Discussion started by: Stephan
17 Replies

6. Shell Programming and Scripting

Assigning value to a variable

Is there any difference between: set variable=39 and variable=39 (1 Reply)
Discussion started by: proactiveaditya
1 Replies

7. Shell Programming and Scripting

Assigning value to a variable

can we make a global variable and store character values and add other values to that variable ?? for example a="hello, John" and can we add value ". How are you? so a can have "hello, John. How are you?" can someone help me?? (2 Replies)
Discussion started by: bonosungho
2 Replies

8. Shell Programming and Scripting

assigning a variable

hi all, in ksh, how do i assign the output of a find command to a variable, e.g am trying something like this : totalNoFiles=$(print find ./ -name "SystemOut*.log"); but when i echo $totalNoFiles it displays find ./ -name "SystemOut*.log" instead of the total number of... (2 Replies)
Discussion started by: cesarNZ
2 Replies

9. Shell Programming and Scripting

Assigning Value of variable

Hi In my shell script, I'm trying to find the line count of a file and assign it to a variable. LINE_COUNT=$(wc -l $FILE_NAME) But when i display LINE_COUNT, i'm getting the linecount concatenated with the file name. I want only the number. How can i get the line count alone ? Someone... (2 Replies)
Discussion started by: janemary.a
2 Replies

10. Shell Programming and Scripting

Assigning a value to variable

Another newbie to Unix scripting Q.. How do you assign a value resulting from a command, such as awk, to a variable. I am currently trying:- $awk '{print $1}' file1 > variable1 with no change to $variable1. The line: $awk '{print $1}' file1 does print the first line of the... (3 Replies)
Discussion started by: sirtrancealot
3 Replies
Login or Register to Ask a Question