LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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-27-2003, 11:45 PM   #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, 12: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, 03:01 AM   #3
DIYLinux
Member
 
Registered: Jul 2003
Location: NL
Distribution: My own
Posts: 92

Rep: Reputation: 18
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, 07: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, 02:44 AM   #5
DIYLinux
Member
 
Registered: Jul 2003
Location: NL
Distribution: My own
Posts: 92

Rep: Reputation: 18
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, 06: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



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 11:13 AM
shell scipting: append output of a command to a variable jonhewer Linux - Newbie 10 08-24-2005 05:42 AM
series-Write a single shell command to display the following output ricki Programming 2 07-31-2005 03:53 PM
in C, Assigning output of system() to a variable Miaire Programming 4 01-30-2005 12:40 PM
Assigning a string to a variable (not a pointer, not a array) JStew Programming 3 11-18-2002 08:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:40 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration