help-bash
[Top][All Lists]
Advanced

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

Re: Checking validity of a command


From: Alan D. Salewski
Subject: Re: Checking validity of a command
Date: Sun, 11 Oct 2020 11:47:48 -0400
User-agent: Mutt/1.14.6 (2020-07-11)

On 2020-10-11 10:48:53, Budi spake thus:
> How to check validity of a command using Bash command inside a script ?
> e.g. for echo command checking, tried this
> 
> $ set -n 'echo HI' &&echo Y
> Y
> 
> $ set -n 'eco HI' &&echo Y
> Y
> 
> won't do the check, how to solve ?

Hi Budi,

It looks like your examples were tried interactively in the shell.
The bash(1) section for the 'set' builtin notes that '-n' is ignored
for interactive shells:
<quote>
      -n      Read commands but do not execute them.  This may be
              used to  check a shell script for syntax errors. This
              is ignored by interactive shells.
</quote>

Also note that the '-n' option is checking only for syntax
errors. The 'eco' token is not a syntax error, though bash will
complain when it later attempts to run it:

    bash: eco: command not found

To have bash syntax-check a script, you can place 'set -n' at the
top of the script and then run it. But a less intrusive way to do
the same thing would be to simply invoke the script like this:

    $ bash -n ./some-script

That has the same effect, but does not require modification of the
script itself. Note that this behavior is also documented in
bash(1):
<quote>
    OPTIONS
           All of the single-character shell options documented in
           the description of the set builtin command, including -o,
           can be used as options when the shell is invoked.
    ...
</quote>

    $ cat - <<'EOF' > /tmp/A
    > # syntactically correct, and runs
    > echo HI
    > EOF

    $ cat - <<'EOF' > /tmp/B
    > # syntactically correct, but errs when run
    > eco HI
    > EOF

    $ cat - <<'EOF' > /tmp/C
    > # syntactically incorrect
    > echo ( oops
    > EOF

    $ bash -n /tmp/A && echo Y || echo N
    Y

    $ bash -n /tmp/B && echo Y || echo N
    Y

    $ bash -n /tmp/C && echo Y || echo N
    /tmp/C: line 2: syntax error near unexpected token `oops'
    /tmp/C: line 2: `echo ( oops'
    N

Adding the 'set -n' statements at the top of the individual scripts
works, as well, but then you'll later need to remember to remove
them:

    $ sed -e '1s/^\(.*\)$/set -n\n\1/' /tmp/A > /tmp/a && chmod u+w /tmp/a
    $ sed -e '1s/^\(.*\)$/set -n\n\1/' /tmp/B > /tmp/b && chmod u+w /tmp/b
    $ sed -e '1s/^\(.*\)$/set -n\n\1/' /tmp/C > /tmp/c && chmod u+w /tmp/c

    $ /tmp/a && echo Y || echo N
    Y

    $ /tmp/b && echo Y || echo N
    Y

    $ /tmp/c && echo Y || echo N
    /tmp/c: line 3: syntax error near unexpected token `oops'
    /tmp/c: line 3: `echo ( oops'
    N

Take care,
-Al

-- 
-----------------------------------------------------------------
a l a n   d.   s a l e w s k i                   salewski@att.net
                                               ads@salewski.email
                                      https://github.com/salewski
-----------------------------------------------------------------


reply via email to

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