help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Odd behaviour using $(...)


From: Pierre Gaston
Subject: Re: [Help-bash] Odd behaviour using $(...)
Date: Wed, 22 Apr 2015 11:09:42 +0300

On Wed, Apr 22, 2015 at 9:11 AM, Peter West <address@hidden> wrote:

> The following script, called as 'tiktst', executes as expected when
> invoked directly from the shell.
> --------------------------------------------------------------
> #! /bin/sh
> TIKA_APP=${TIKA_APP:="$JARS/tika-app.jar"}
>
> typeset -i port=45000
>
> msg () { echo Using port $port; }
>
> pid () { echo Server PID $!; }
>
> case $(basename $0) in
>     tiktst)
>         msg; java -jar "$TIKA_APP" --detect --server $port & pid ;;
>     *)
>         echo "Server not executed. Program name not known." >&2 ;;
> esac
>
> echo Exited from case statement
> exit 0
> -------------------------------------------------------------
>
> Here's the output:
> $ tiktst
> Using port 45000
> Server PID 11899
> Exited from case statement
>
>
> However, if I try to capture the output like so:
> $ res=$(tiktst)
>
> the shell hangs. At this point, the shell in which I have executed this
> statement has no children, and the java process is a child of init.  I can
> type commands, which echo to the terminal, but do not execute, until I kill
> the java process, whereupon they will execute.
>
> If I 'set -x' I will see all of the commands in debug output, including
> the final 'exit 0' and the java command.  If I put 'sleep 2' after the java
> invocation, I see the java command in the dubug output before the 'exit 0';
> otherwise the java command is echoed to debug after the exit 0.
>
> What's going on?
>
> OS X 10.10.3 bash 3.2.57(1)
>
> Peter West
> "Why do you seek the living among the dead? He is not here, but has risen."
>
>
>
$( ) waits for stdout to be closed, since your server continues to run in
the background stdout stays open
add >/dev/null after you java command an you should be ok....even better
close all the fds:
java >/dev/null 2>&1 </dev/null


(as an aside typeset -i is not needed and implies that your shell is
bash/ksh while you are using /bin/sh in your shebang, so you should
probably remove it)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]