LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-11-2009, 10:01 PM   #1
vvpp
LQ Newbie
 
Registered: Sep 2007
Location: Shanghai, China
Distribution: RH&Fedora
Posts: 6

Rep: Reputation: 0
for shell script how to store the output of another task other than this command self


Hi guys,

In my shell script, I failed to store the result of an external command, which will exit instantly just after sending msg to another task that will print some info out, into a variable.
For example, let's assume the command name is "alarm_dbg", which just sends a "PRINT" msg to another task like "alarm" then returns, and the task "alarm" will print the info out, so my sentence in script would be like
Code:
val=`alarm_dbg` #external command
echo "val=$val"
but the return value is null, as
Code:
# here the executed result by alarm task is displayed
# but val variable is null
val=
Does anyone know how to store the info into this variable just using shell script other than modify the code to let "alarm_dbg" self to print out the info?

Thanks in advance.
 
Old 11-11-2009, 10:23 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you are using bash, try using $() syntax instead of backticks
 
Old 11-11-2009, 11:01 PM   #3
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726

Rep: Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706
If you run alarm_dbg on the command line, what happens?
It prints some text? Is this what you want to store in your variable, val?

Also you didn't tell us what shell you are using for the script. Are we correct assuming that it is bash?

Evo2.

Last edited by evo2; 11-11-2009 at 11:01 PM. Reason: typo
 
Old 11-11-2009, 11:23 PM   #4
vvpp
LQ Newbie
 
Registered: Sep 2007
Location: Shanghai, China
Distribution: RH&Fedora
Posts: 6

Original Poster
Rep: Reputation: 0
1> it just prints out the info as that of
2> yeah, it does print texts, and which are what I want.
3> sorry for no mention about shell name, it is bash, as you assumed.

Quote:
Originally Posted by evo2 View Post
If you run alarm_dbg on the command line, what happens?
It prints some text? Is this what you want to store in your variable, val?

Also you didn't tell us what shell you are using for the script. Are we correct assuming that it is bash?

Evo2.
 
Old 11-11-2009, 11:26 PM   #5
vvpp
LQ Newbie
 
Registered: Sep 2007
Location: Shanghai, China
Distribution: RH&Fedora
Posts: 6

Original Poster
Rep: Reputation: 0
yeah, it's bash, but it seems $() take same effect as ``
and the "val" I got is still null
Quote:
Originally Posted by ghostdog74 View Post
if you are using bash, try using $() syntax instead of backticks
 
Old 11-11-2009, 11:44 PM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
A while back, I recall an "external" command or program of some sort, that for whatever reason was sending it's output through 'stderr' instead of 'stdout' as one would expect. Maybe this external command you are running is also doing the same?

At least to eliminate this as a possibility, try redirecting stderr into stdout when running the command whose output you wish for $val to trap.

Sasha
 
Old 11-12-2009, 01:39 AM   #7
vvpp
LQ Newbie
 
Registered: Sep 2007
Location: Shanghai, China
Distribution: RH&Fedora
Posts: 6

Original Poster
Rep: Reputation: 0
Sasha,thanks for your idea, but this command is stdout too, in fact the output is done by another running task instead of this command, which just sends msg to the task to trigger the output.

Quote:
Originally Posted by GrapefruiTgirl View Post
A while back, I recall an "external" command or program of some sort, that for whatever reason was sending it's output through 'stderr' instead of 'stdout' as one would expect. Maybe this external command you are running is also doing the same?

At least to eliminate this as a possibility, try redirecting stderr into stdout when running the command whose output you wish for $val to trap.

Sasha
 
Old 11-12-2009, 02:29 AM   #8
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726

Rep: Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706
Um, the top posting has made this difficult to follow...

Quote:
Originally Posted by vvpp View Post
1> it just prints out the info as that of
Yes but what?

Quote:
2> yeah, it does print texts, and which are what I want.
I'm just wondering if there are some escape sequences in there that could be giving you problems.

Did you try
Code:
val="`alarm_dbg`"
instead of just
Code:
val=`alarm_dbg`
?

Quote:
3> sorry for no mention about shell name, it is bash, as you assumed.
Ok.

Can you reproduce the problem with something other than "alarm_dbg"? Or perhaps let us know exactly what "alarm_dbg" is?

Evo2.
 
Old 11-12-2009, 03:30 AM   #9
vvpp
LQ Newbie
 
Registered: Sep 2007
Location: Shanghai, China
Distribution: RH&Fedora
Posts: 6

Original Poster
Rep: Reputation: 0
Hi Evo2,

in fact the command "alarm_dbg" just sends msg to some running task to trigger the output. After the msg sent, "alarm_dbg" will return ok, so the val will be null for there is really no output for "alarm_dbg". I just want to collect the terminal output info printed by another running task, but failed.

Quote:
Originally Posted by evo2 View Post
Um, the top posting has made this difficult to follow...


Yes but what?


I'm just wondering if there are some escape sequences in there that could be giving you problems.

Did you try
Code:
val="`alarm_dbg`"
instead of just
Code:
val=`alarm_dbg`
?


Ok.

Can you reproduce the problem with something other than "alarm_dbg"? Or perhaps let us know exactly what "alarm_dbg" is?

Evo2.
 
Old 11-12-2009, 04:18 AM   #10
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726

Rep: Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706
Please stop top posting. If you don't know what "top posting" means please refer to google.

You quoted my entire post below yours, but didn't answer any of the questions I asked. Why?

Quote:
Originally Posted by vvpp View Post
in fact the command "alarm_dbg" just sends msg to some running task to trigger the output.
After the msg sent, "alarm_dbg" will return ok,
"return ok" Do you mean that it exits with a status of 0?

Quote:
so the val will be null for there is really no output for "alarm_dbg".
"null"? Does that mean an empty string or something else?
Quote:
I just want to collect the terminal output info printed by another running task, but failed.
This much I do understand, but I can't help you with the relevant information.


Evo2.
 
Old 11-12-2009, 08:46 PM   #11
vvpp
LQ Newbie
 
Registered: Sep 2007
Location: Shanghai, China
Distribution: RH&Fedora
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Please stop top posting. If you don't know what "top posting" means please refer to google.

You quoted my entire post below yours, but didn't answer any of the questions I asked. Why?
Sorry for my overlooking, I'm a newbie, and just cared about the question itself.
Sorry again, I just posted the detail info that leaded to the failure

Quote:
"return ok" Do you mean that it exits with a status of 0?
yeah, return 0, but no any "printf" related sentence.

Quote:
"null"? Does that mean an empty string or something else?
hum, may be blank, or \r or \n, but it is not the point

Quote:
This much I do understand, but I can't help you with the relevant information.
^_^, thanks for your attention, I guess what I mentioned above is really the key point to the issue. Maybe I could modify the command code to output the info towards a file or let it print the info too bu adding "printf" sentences.

I really appreciate your comments. Thanks again
 
Old 11-13-2009, 01:28 AM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
One possibility is that the mystery program run by alarm_dbg that produces the desired output writes to terminal rather than stdout or stderr -- but the output would be seen on screen when the the script is run.

@vvpp: regards "hum, may be blank, or \r or \n, but it is not the point" that makes sense when you simply want to make your script work but it does not make sense when you are working with a volunteer who is trying to help you and needs all the available information in case it helps solve the problem.

Even if you think people are asking dumb questions it is smart to answer them. Answering people's questions keeps them happy. And you can't make your script work so do you know enough to decide what is a dumb question and what is a useful question?

Sometimes when trying to solve a difficult problem, the most crazy question can get the information that leads to a solution.
 
  


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
csh Shell Script: Getting wc command output to variable to do math on it, how? vxc69 Programming 5 05-04-2009 05:31 PM
[shell script] execute command and parse output stoiss Programming 2 01-26-2009 02:49 AM
Shell Script format question - output command results in quotes Barefootpanda Linux - General 5 10-14-2008 12:40 AM
Help!! Shell script to get output of ls -l command into an array kasthana Programming 8 06-02-2008 12:37 AM
Odd problem with making a variable the output of a command in a shell script linux=future Programming 3 12-13-2005 10:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:54 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