Bourn Shell -n Flag Questions

Kevin Kinsey kdk at daleco.biz
Sat Apr 29 00:41:01 UTC 2006


> On 4/28/06, Martin McCormick <martin at dc.cis.okstate.edu> wrote:
> 
>>         I read about the noexecute flag or -n flag which is supposed
>> to check the syntax of a Bourn Shell script to see what it would do if
>> run, but not actually do anything.  This sounds like a wonderful
>> thing, especially when one is going to run a dangerous script and you
>> only get one chance to get it right.
>>
>>         I tried sh -n scriptname and it always silently succeeds even
>> if I type sh -x -n somescript.  I even deliberately created a script
>> with a syntax error in it and tried sh -x -n again.  It still did
>> nothing but exit.  Does this just not work or am I misunderstanding
>> the purpose of the flag?
>>
>>         Thanks for your help.
>>
N. Ersen SISECI wrote:
 > Hello,
 >
 > Bourne shell is bash. So you should try
 > bash -n scriptname

Um, "Bourne shell" is /bin/sh.  "Bourne Again SHell" is bash, which is 
not installed in FreeBSD by default.

As for "sh -n", consider:

[343] Fri 28.Apr.2006 19:35:03
[kadmin at archangel][~/scripts]# cat foobaz
#!/bin/sh

PRE="Foo
ECHO=/bin/echo
TARGET=/tmp/foobar

$ECHO $PRE > $TARGET

[344] Fri 28.Apr.2006 19:35:20
[kadmin at archangel][~/scripts]# sh -n foobaz
foobaz: 3: Syntax error: Unterminated quoted string

So, it would appear that "-n" is merely a "lint" type option or "syntax 
checker".  Whether or not it's smart enough to catch your error I can't 
tell; it caught mine with and without "-x" ....

HTH,

Kevin Kinsey

-- 
The giraffe you thought you offended last week is willing to be nuzzled
today.


More information about the freebsd-questions mailing list