LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-27-2013, 11:16 PM   #1
Ahmadhr
LQ Newbie
 
Registered: Oct 2013
Posts: 1

Rep: Reputation: Disabled
redirecting a Command output to a Variable


Hi
I am trying to write something like this:
i need to find something in the output of a command and redirect that found line to a variable to i can call it somewhere else.

appreciate any help..


echo "please Enter the 1st 3rd Octet: "
read ipaddr

echo "Please Enter the number of host: "
read sub

for i in $(seq 1 $sub)
do
if ping -c 2 -t 2 $ipaddr.$i | grep "100.0% packet loss";
then echo "the host $ipaddr.$i is --- DOWN"
else echo "the host $ipaddr.$i is --- UP"
fi

nslkup=nslookup $ipaddr.$i | grep "name";
then echo "the $ipaddr.$i didnt resolve any name"
else echo "the $ipaddr.$i name is $nslkup "

fi
done
 
Old 10-27-2013, 11:51 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

welcome to LQ.

your sample code doesn't make sense to me but I guess you are looking for something like:
Code:
foo=$(date)
echo "The date is $foo"
The above uses the date command to produce some output that is stored in the variable $foo.

HTH,

Evo2.
 
Old 11-05-2013, 04:31 AM   #3
goarilla
LQ Newbie
 
Registered: Sep 2005
Location: Belgium
Distribution: slackware 10
Posts: 13

Rep: Reputation: 2
To capture a command's standard output you use command substituon like this:

output=$(command -arguments)
OR
output=`command -arguments`

The two forms are almost the same.
However you can also use the commands return value to check for success

ping -c 1 host

if [ $? -ne 0 ] ; then
# host wasn't pingeable
fi

or make grep silent and use its exit status only instead of its output, eg
use grep as a filter boolean tester.

ping -c 1 host | grep -q "100.0% packet loss"

if [ $? -eq 0 ]; then
# 100 % packet loss
fi


I hope this is clear.


Greetings,
 
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
Redirecting command output to input of another command mightymouse2045 Linux - Newbie 3 07-02-2011 12:28 PM
redirecting output from a command using bash nano2 Linux - General 2 05-10-2011 03:35 PM
Redirecting grep output to a variable dimako83 Linux - Software 6 11-17-2008 07:22 AM
Redirecting output of running command tommyr1216 Linux - General 3 03-04-2005 08:20 PM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 04:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:28 AM.

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