This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Echo value capture in .sh script

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Shell script support
Assume that am running an shell script (status.sh) which will print as
"Application is running"
or
"Application is not running"
Based on the output my next action has to take place in my script.
How to capture an echo value
Thanks
JOwsaki
 
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To simply capture all of what's echoed from a shell script just capture it with command substitution:
captured_echo=$(status.sh)
This may be enough for your purposes but you may run into a problem if the script also echo other stuff you can work your way around with grep:
captured_echo=$(status.sh | grep '^Application is.*running$')
This will ensure that $captured_echo only has one of those two lines.
But, I would use non of the above end simply modify the status.sh script to return a status which I can evaluate.
Rather then printing 'app run' or 'app not run' simply exit with with zero indicating success and any other integer (up to 255) indicating failure. Then just examine the $? variable:
 
Sham Jowsaki
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Leslie, Thanks
Yes, I got the output as "xyz is running" n store it as X.
Var Y="xyz is running"
Now am trying to compare it like
"$X = $Y"
am getting the following error
./driveremg.sh: [: too many arguments
If it is single word, it is passing thru perfectly.
Is there anyway to compare a statement like above.
Pls advice me.
Thanks
Jowsaki
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jowsaki
Put quotes around your variables:

See the quotation marks inside the if statement? That ensures that you are only comparing the value inside one set of quotes with the value in the other set of quotes.
Whithout the quotation marks, the shell would have seen two arguments on either side of the equal sign, and not known what to do with it.
Regards, Andrew
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sham Jowsaki:
Yes, I got the output as "xyz is running" n store it as X.
Var Y="xyz is running"
Now am trying to compare it like
"$X = $Y"
am getting the following error
./driveremg.sh: [: too many arguments


I think it would be best if you were to post your code, as conditional statements are a bit tricky.
Having said that, the probable reason is that you are not doing a string comparison. Rember that the expression $x means to substitute the value of x in the expression. So, if you have an expression like this:

that statement will turn out as:

So clearly this will cause the script to fail.
Another thing, if you use the expression:

then this is not comparison. Its like saying


What you need to do is have an expression like this:

if [ "$x" == "$y" ]; then ...

Hope this would help.
Al
 
Sham Jowsaki
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andrew n Alton,
Many thanks for your response. yes it is working fine. Appreciate !!!
Tks a lot
Cheers
Jowsaki
 
Sham Jowsaki
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andrew,
What is the use of ";" in If condition
If [ "$X" = "$Y" ];
Pls explain me.
thanks
Jowsaki
 
Andrew Monkhouse
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jowsaki
The semicolon in any unix command line allows multiple commands to be appended on one physical line.
So my example: is on one line. If I didnt use semicolons to break the commands, I would have had to use three lines:
Regards, Andrew
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic