help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Unexpected behaviour in job control inside subshell envi


From: Diego Augusto Molina
Subject: Re: [Help-bash] Unexpected behaviour in job control inside subshell environment
Date: Fri, 2 Dec 2016 09:07:26 -0300
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1

El 01/12/16 a las 13:29, Greg Wooledge escribió:
On Thu, Dec 01, 2016 at 12:55:04PM -0300, Diego Augusto Molina wrote:
So I came up with the following idea:

SLEEP_ARG=wrong-value;
(
  sleep "10${SLEEP_ARG}" &
  sleep 0.1; # Optional but recommended
  jobs -r 1;

Remember that job control is disabled in scripts (non-interactive shells)
by default.

Great point, but I'm using an interactive shell.


As far as wrapping the sleep(1) command to validate arguments, I would
go with something more like this:

ver=$(sleep --version 2>&1)
if [[ $ver = *GNU* ]]; then
  : fractions are allowed
else
  : fractions are probably going to blow up
fi

Actually *calling* sleep(1) with wacky arguments to see how long it
takes to die is pretty suboptimal, I should think.


The reason of the original mail was to understand why the code didn't work as expected. It wasn't intended to ask for a solution to the problem of validating the argument given to the "sleep" program. The real solution I came up to is:

SLEEP_TIME=something;
sleep_time_regex='^(\.[0-9]+)?$';
if
  [ -z "$SLEEP_TIME" ] || # SLEEP_TIME must be a value
  ( # If we don't have the GNU implem, only integers are accepted
    [[ "$(sleep --version 2>&1;)" != *GNU* ]] &&
    [ -n "${SLEEP_TIME##*([0-9])}" ]
  ) || # If we do have the GNU implem, then we may have decimal point
  [[ ! "${SLEEP_TIME##*([0-9])}" =~ ${sleep_time_regex} ]];
then
  echo "Invalid SLEEP_TIME value.";
fi;

Note that I use a separate variable for the regex since the code won't run on a (very) old Bash implementation I'm testing with.



reply via email to

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