开发者

Bash script exiting prematurely when calling another script inside it

I have a bash script which calls another bash script, like so:

#!/bin/bash
echo "Hi"
./script-two.sh
echo "Hello!"

The problem that I have is that it never makes it to printing "Hello!"

I think this is because ./sc开发者_如何学Cript-two.sh (Which I did not write) is somehow exiting or changing the shell. I have included this script at the end of this post.

Is there a way I can gurentee that my execution will continue after script-two.sh executes?

I have looked into using the trap command, but I don't fully understand its use properly.

Thanks,

Casey

Here is the contents of what would be script-two.sh

    #!/bin/sh
#  This file is part of the DITA Open Toolkit project hosted on 
#  Sourceforge.net. See the accompanying license.txt file for 
#  applicable licenses.
#  (c) Copyright IBM Corp. 2006 All Rights Reserved.

export DITA_HOME=cwd


if  [ "${DITA_HOME:+1}" != "1" ]; then 
   echo "DITA_HOME environment variable is empty or not set";
   exit 127;
fi

echo $DITA_HOME

cd "$DITA_HOME"

# Get the absolute path of DITAOT's home directory
DITA_DIR="`pwd`"

echo $DITA_DIR

if [ -f "$DITA_DIR"/tools/ant/bin/ant ] && [ ! -x "$DITA_DIR"/tools/ant/bin/ant ]; then
chmod +x "$DITA_DIR"/tools/ant/bin/ant
fi

export ANT_OPTS="-Xmx512m $ANT_OPTS"
export ANT_OPTS="$ANT_OPTS -Djavax.xml.transform.TransformerFactory=net.sf.saxon.TransformerFactoryImpl"
export ANT_HOME="$DITA_DIR"/tools/ant
export PATH="$DITA_DIR"/tools/ant/bin:"$PATH"

NEW_CLASSPATH="$DITA_DIR/lib:$DITA_DIR/lib/dost.jar:$DITA_DIR/lib/commons-codec-1.4.jar:$DITA_DIR/lib/resolver.jar:$DITA_DIR/lib/icu4j.jar"
NEW_CLASSPATH="$DITA_DIR/lib/saxon/saxon9.jar:$DITA_DIR/lib/saxon/saxon9-dom.jar:$NEW_CLASSPATH"
NEW_CLASSPATH="$DITA_DIR/lib/saxon/saxon9-dom4j.jar:$DITA_DIR/lib/saxon/saxon9-jdom.jar:$NEW_CLASSPATH"
NEW_CLASSPATH="$DITA_DIR/lib/saxon/saxon9-s9api.jar:$DITA_DIR/lib/saxon/saxon9-sql.jar:$NEW_CLASSPATH"
NEW_CLASSPATH="$DITA_DIR/lib/saxon/saxon9-xom.jar:$DITA_DIR/lib/saxon/saxon9-xpath.jar:$DITA_DIR/lib/saxon/saxon9-xqj.jar:$NEW_CLASSPATH"
if test -n "$CLASSPATH"
then
export CLASSPATH="$NEW_CLASSPATH":"$CLASSPATH"
else
export CLASSPATH="$NEW_CLASSPATH"
fi

"$SHELL"


It looks like script-two.sh is setting up an ant build environment.

I think the author intended that it sets up the build environment, then you type your build commands in manually, then type exit to leave the build environment.

I say this because the bottom line of script-two.sh is:

"$SHELL"

which starts a new shell.

Try running your script, then type exit. I think you will see it print Hello! after you type exit.


I'm guessing you're trying to do something like:

#!/bin/bash
echo "Hi"
./script-two.sh
ant <some args>

To do that, what you really want to do is source it, by changing:

./script-two.sh

to

. script-two.sh

e.g.

#!/bin/bash
echo "Hi"
. script-two.sh
ant <some args>

But, you will need to edit script-two.sh and change:

"$SHELL"

to:

case $0 in *script-two.sh)
    # executed, start a new shell with the new environment
    "$SHELL" 
    ;;
*)
    # sourced, don't start a new shell
    ;;
esac

so that it only starts a shell if the script is being run like ./script-two.sh, but not if it is being sourced like . script-two.sh.

Or if you absolutely can't change script-two.sh, then you could do:

#!/bin/bash
echo "Hi"
. script-two.sh </dev/null
ant <some args>

which will trick "$SHELL" into exiting because it has no input.


Also

export DITA_HOME=cwd

doesn't seem right to me.

It should probably be

export DITA_HOME=$(pwd)

or

export DITA_HOME=`pwd`

(both are equivalent)


I had a similar problem today, up on digging I finally found the answer.

The script I was calling (from within my script) actually had an exit 0 in the end. Removing that fixed my issues.

Just leaving this here as someone may find it useful.


Well for starters, you can execute your bash script with the -x switch to see where it is failing:

bash -x script-one.sh

Secondly, if you call the second script like this:

#!/bin/bash
echo "Hi"
var=$(bash script-two.sh)
echo "Hello!"

It will continue, as long as script-two.sh exits cleanly. Again, you can run the -x script against that script find any problems.

And as Mikel mentioned, always make sure to have exit at the bottom of your scripts.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜