File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to return a string number from shell script?

 
Gaurav Kr. Arora
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Am a beginner in shell scripting and stuck at a point.

Below is my shell script:

I call this script from ant and pass a file which contains a number >0. $1 in below script is file path.

file.sh
----------


#!/bin/bash
export MY_VAR=$(cat $1 | grep -E "^[0-9]+$")
echo newResult:${MY_VAR}
exit ${MY_VAR}


----------------------------


The above script should return MY_VAR to calling ant script. But it always return 255 and I don't get correct value. Also it shows [exec] : numeric argument required error.

Below is ant piece of code which calls it:

<exec executable="/bin/bash" resultproperty="count">
<arg value="/file.sh"/>
<arg value="/TestFile.txt"/>
</exec>

Please help.

Thanks!
 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't tried it myself, but did you try to execute the script directly, instead of as an argument to bash; i.e.


The first line in your script already ensures that bash will be used. Also, is your script really in the root directory?
 
Gaurav Kr. Arora
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Koen Aerts wrote:I haven't tried it myself, but did you try to execute the script directly, instead of as an argument to bash; i.e.


The first line in your script already ensures that bash will be used. Also, is your script really in the root directory?


Yes, that is fine, I just removed the actual file name and simply put /TestFile.txt which shows it in root. Am sorry for that.
But my major concern is how to return the exact number found by MY_VAR variable to the calling program.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17620
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shell scripts, just like binary commands, can ONLY return a single numeric integer value. For anything else, you need to use an alternative channel, such as stdout or stderr. An attempt to exit a script using a non-numeric exit value will fail and the script will return with a non-zero (error) numeric value.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic