This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Linux / UNIX and the fly likes How to return a string number from shell script? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "How to return a string number from shell script?" Watch "How to return a string number from shell script?" New topic
Author

How to return a string number from shell script?

Gaurav Kr. Arora
Ranch Hand

Joined: Feb 20, 2011
Posts: 37
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

Joined: Feb 07, 2012
Posts: 344

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

Joined: Feb 20, 2011
Posts: 37
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

Joined: Jun 25, 2001
Posts: 15950
    
  19

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to return a string number from shell script?
 
Similar Threads
How to use ct command with ANT in Unix Environment
execute script in background with ANT
Checking for .jar in classpath with ANT
Problem with catalina.sh
Trouble launching ant exec task