Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

pipeline output as variable

Original Message
Name: numassa
Date: November 21, 2006 at 15:16:17 Pacific
Subject: pipeline output as variable
OS: red hat
CPU/Ram: 2/1
Model/Manufacturer: sony
Comment:
Hi,
Could someone please tell me how to set the result of a pipeline as a new variable

e.g cat file | cmd to remove every character but the first letter | ?

what do I need to put to assign the outcome (a single letter in this case) to a variable?

Any help would be greatly appreciated.


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: November 21, 2006 at 16:31:05 Pacific
Subject: pipeline output as variable
Reply: (edit)
Use command substitution:

For bourne shell

char=`cmd|cmd|cmd` # back-tics, not single quotes

For korn and bash:

char=$(cmd|cmd|cmd)



Report Offensive Follow Up For Removal

Response Number 2
Name: numassa
Date: November 21, 2006 at 16:46:43 Pacific
Subject: pipeline output as variable
Reply: (edit)
thats great -thanks very much for your help

Report Offensive Follow Up For Removal

Response Number 3
Name: numassa
Date: November 21, 2006 at 17:01:41 Pacific
Subject: pipeline output as variable
Reply: (edit)
Unfortunately I have another question...

if I have a file that contains variable information

e.g file

erx2=yes
gex9=yes

how can I use these later in the script that has created the file? at the moment and am using a very dodgy 'if' statement as follows

var=`grep erx2 file`
if [ $var="erx2=yes" ]
then var=yes
else
var=no
fi

this always sets var=yes
even of there is no line in file that matches the grep on the first line.

Any help would be greatly appreciated.


Report Offensive Follow Up For Removal

Response Number 4
Name: James Boothe
Date: November 21, 2006 at 18:04:27 Pacific
Subject: pipeline output as variable
Reply: (edit)
Relational operators need to be surrounded by white space, same as the brackets. So your command

if [ $var="erx2=yes" ]

just needs to be

if [ $var = "erx2=yes" ]

As it was, you had a single operand - a mixture of a variable and literals - and it will test true if the string is not null, which of course it would always be not null due to the literals.

And you might find the following helpful ...

eval `grep erx2 file`
echo $erx2
yes

The grep finds (or maybe not) the erx2=yes line, and that feeds into the eval command which evaluates it and sets the variable. You can then test for the variable's content in the normal fashion, but since it may not exist, make sure you surround with double_quotes to avoid a syntax error:

if [ "$erx2" = yes ]
then echo yep
else echo nope
fi


Report Offensive Follow Up For Removal

Response Number 5
Name: Devaraj (by Fidy)
Date: November 21, 2006 at 22:31:03 Pacific
Subject: pipeline output as variable
Reply: (edit)
you can do a count on the match

#!/bin/sh
if [ `grep -c "erx2=yes" file3` -gt 0 ]
then
var=yes
else
var=no
fi
echo $var


Regards,
Devaraj Takhellambam


Report Offensive Follow Up For Removal


Response Number 6
Name: numassa
Date: November 22, 2006 at 03:10:59 Pacific
Subject: pipeline output as variable
Reply: (edit)
I went with the eval method which worked prefectly.
Thanks Nails, James Booth and Devaraj you have been extremely helpful.

Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: pipeline output as variable

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




My PC has been hijacked!

Lexmark 2600 Printer Issues

btk1w1 infected start here post

Unwanted message remians on screen

Slow boot time


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC