LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-28-2003, 12:45 AM   #1
guru_stew
LQ Newbie
 
Registered: Jul 2003
Distribution: red hat, BSD, debian
Posts: 3

Rep: Reputation: 1
Unhappy Assigning the output of one command to a variable (shell)


Hey,
I want to assign the output of one comman into a variable to be used in another command.
for example
for each i in `cat /etc/passwd`
do
#the bit that don't work
name= echo $i | ut -f1 -d :
done

I could then use $name for my tasks

any ideas?
Thanks
 
Old 07-28-2003, 01:47 AM   #2
storm3x
LQ Newbie
 
Registered: Jul 2003
Location: Ontario, Canada
Distribution: SuSE 8.1
Posts: 8

Rep: Reputation: 1
I'll hazard a guess here. If I wanted to extract the full name field, that is field #5 from each line of /etc/passwd, I would pipe the output of your cat command to awk and ask it to print out only field #5 of each line like this:

cat /etc/passwd | gawk -F: '{print $5}'

Notes:
1. gawk is 'gnu awk'. It will process each line coming in from cat.
2. -F: tells gawk to treat the colon as the input field separator. (The default is a space, so we have to make that explicit).
3. '{print $5}' is the action we want to carry out. If you wanted to print out the first field then change $5 to $1.
4. Once you've extracted the field data you're interested in you can pipe it off to another process or assign it to a shell variable if you like.

Hope this helps.
 
1 members found this post helpful.
Old 07-28-2003, 04:01 AM   #3
DIYLinux
Member
 
Registered: Jul 2003
Location: NL
Distribution: My own
Posts: 92

Rep: Reputation: 17
How about $(...) or `...`.

Example:

# set varuiable UPPER to uppercase form of LOWER
UPPER=$(echo $LOWER | tr [a-z][A-Z])
UPPER=`echo $LOWER | tr [a-z][A-Z]`

The backquote (`) is more standard; it appeared in the original bourne shell, and is supported by almost every shell now available.

The $(...) construct is newer, POSIX compliant, and should be used unless you need extreme portability to ancient UN*X boxes. It makes nesting easier.
 
1 members found this post helpful.
Old 07-31-2003, 08:07 PM   #4
guru_stew
LQ Newbie
 
Registered: Jul 2003
Distribution: red hat, BSD, debian
Posts: 3

Original Poster
Rep: Reputation: 1
hmm,
I tried

for i in `cat /etc/passwd`
do
name = `echo $i | cut -f1 -d :`
echo $name
done

and I got lots of
name: not found

???
 
Old 08-01-2003, 03:44 AM   #5
DIYLinux
Member
 
Registered: Jul 2003
Location: NL
Distribution: My own
Posts: 92

Rep: Reputation: 17
When assigning to a variable, do not put a space between the variable and the assignment ('=').

The shell uses this space to distinguish program invocation (with the first arg a =) from assignment.
 
1 members found this post helpful.
Old 08-03-2003, 07:12 PM   #6
guru_stew
LQ Newbie
 
Registered: Jul 2003
Distribution: red hat, BSD, debian
Posts: 3

Original Poster
Rep: Reputation: 1
Ah ha, it works... who said a java programer couldn't learn shell!
lol
Thanks
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting - use the output of a command as an argument for another timgiffney Programming 6 04-17-2006 12:13 PM
shell scipting: append output of a command to a variable jonhewer Linux - Newbie 10 08-24-2005 06:42 AM
series-Write a single shell command to display the following output ricki Programming 2 07-31-2005 04:53 PM
in C, Assigning output of system() to a variable Miaire Programming 4 01-30-2005 01:40 PM
Assigning a string to a variable (not a pointer, not a array) JStew Programming 3 11-18-2002 09:13 AM


All times are GMT -5. The time now is 09:45 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration