How to use multiple arguments for awk with a shebang (i.e. #!)?

前端 未结 10 762
小蘑菇
小蘑菇 2020-11-22 17:42

I\'d like to execute an gawk script with --re-interval using a shebang. The \"naive\" approach of

#!/usr/bin/gawk --re-interval -f
... awk scri         


        
10条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 17:46

    This seems to work for me with (g)awk.

    #!/bin/sh
    arbitrary_long_name==0 "exec" "/usr/bin/gawk" "--re-interval" "-f" "$0" "$@"
    
    
    # The real awk program starts here
    { print $0 }
    

    Note the #! runs /bin/sh, so this script is first interpreted as a shell script.

    At first, I simply tried "exec" "/usr/bin/gawk" "--re-interval" "-f" "$0" "$@", but awk treated that as a command and printed out every line of input unconditionally. That is why I put in the arbitrary_long_name==0 - it's supposed to fail all the time. You could replace it with some gibberish string. Basically, I was looking for a false-condition in awk that would not adversely affect the shell script.

    In the shell script, the arbitrary_long_name==0 defines a variable called arbitrary_long_name and sets it equal to =0.

提交回复
热议问题