• Post Reply Bookmark Topic Watch Topic
  • New Topic

Echo value capture in .sh script  RSS feed

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Marshal Commander
Posts: 12156
256
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
Marshal Commander
Posts: 12156
256
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!