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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
11-11-2009, 10:01 PM
|
#1
|
LQ Newbie
Registered: Sep 2007
Location: Shanghai, China
Distribution: RH&Fedora
Posts: 6
Rep:
|
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.
|
|
|
11-11-2009, 10:23 PM
|
#2
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
if you are using bash, try using $() syntax instead of backticks
|
|
|
11-11-2009, 11:01 PM
|
#3
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
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
|
|
|
11-11-2009, 11:23 PM
|
#4
|
LQ Newbie
Registered: Sep 2007
Location: Shanghai, China
Distribution: RH&Fedora
Posts: 6
Original Poster
Rep:
|
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
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.
|
|
|
|
11-11-2009, 11:26 PM
|
#5
|
LQ Newbie
Registered: Sep 2007
Location: Shanghai, China
Distribution: RH&Fedora
Posts: 6
Original Poster
Rep:
|
yeah, it's bash, but it seems $() take same effect as ``
and the "val" I got is still null
Quote:
Originally Posted by ghostdog74
if you are using bash, try using $() syntax instead of backticks
|
|
|
|
11-11-2009, 11:44 PM
|
#6
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
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
|
|
|
11-12-2009, 01:39 AM
|
#7
|
LQ Newbie
Registered: Sep 2007
Location: Shanghai, China
Distribution: RH&Fedora
Posts: 6
Original Poster
Rep:
|
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
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
|
|
|
|
11-12-2009, 02:29 AM
|
#8
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
Um, the top posting has made this difficult to follow...
Quote:
Originally Posted by vvpp
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
instead of just
?
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.
|
|
|
11-12-2009, 03:30 AM
|
#9
|
LQ Newbie
Registered: Sep 2007
Location: Shanghai, China
Distribution: RH&Fedora
Posts: 6
Original Poster
Rep:
|
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
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
instead of just
?
Ok.
Can you reproduce the problem with something other than "alarm_dbg"? Or perhaps let us know exactly what "alarm_dbg" is?
Evo2.
|
|
|
|
11-12-2009, 04:18 AM
|
#10
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
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
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.
|
|
|
11-12-2009, 08:46 PM
|
#11
|
LQ Newbie
Registered: Sep 2007
Location: Shanghai, China
Distribution: RH&Fedora
Posts: 6
Original Poster
Rep:
|
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
|
|
|
11-13-2009, 01:28 AM
|
#12
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
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.
|
|
|
All times are GMT -5. The time now is 02:54 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|