“set -o noglob” in bash shell script

后端 未结 2 1360
予麋鹿
予麋鹿 2021-02-11 02:19

I would usually write SQL statements inline in a Bash shell script to be executed in SQLPlus as-

#! /bin/sh

sqlplus user/pwd@dbname<         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-11 02:53

    Before I begin: @John Kugelman's answer (appropriate quoting) is the right way to solve this problem. Setting noglob only solves some variants of the problem, and creates other potential problems in the process.

    But since you asked what set -o noglob does, here are the relevant excerpts from the ksh man page (BTW, your tags say bash, but the error message says ksh. I presume you're actually using ksh).

    noglob  Same as -f.

    -f      Disables file name generation.

    File Name Generation.
       Following splitting, each field is scanned for the characters *, ?,  (,
       and  [  unless  the -f option has been set.  If one of these characters
       appears, then the word is regarded as a pattern.  Each file name compo-
       nent  that  contains  any  pattern character is replaced with a lexico-
       graphically sorted set of names that  matches  the  pattern  from  that
       directory.
    

    So what does that mean? Here's a quick example that should show the effect:

    $ echo *
    file1 file2 file3 file4
    $ ls *
    file1 file2 file3 file4
    $ *    # Note that this is equivalent to typing "file1 file2 file3 file4" as a command -- file1 is treated as the command (which doesn't exist), the rest as arguments to it
    ksh: file1: not found
    

    Now watch what changes with noglob set:

    $ set -o noglob
    $ echo *
    *
    $ ls *
    ls: *: No such file or directory
    $ *
    ksh: *: not found
    

提交回复
热议问题