help-bash
[Top][All Lists]
Advanced

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

Re: #!shebang


From: Andreas Kähäri
Subject: Re: #!shebang
Date: Wed, 20 Nov 2024 10:38:17 +0100

On Wed, Nov 20, 2024 at 10:19:25AM +0100, lacsaP Patatetom wrote:
> hi,
> 
> I'm not sure this is the right place for this question, but maybe I'll find
> the answer here, as bash isn't far away...
> 
> I'd like to use an executable file as a configuration file for `qemu` using
> a shebang : let me explain.
> 
> I currently store my configuration like this in `configuration.qemu` :
> ```
> -argument
> 
> - argument -option
> 
> -etc...
> ```
> 
> and I call `qemu` like this and it works as expected :
> `$ qemu $(<configuration.qemu)`
> 
> which corresponds to the following command line :
> `$ qemu -argument -argument -option -etc...`
> 
> (I can also add additional/temporary arguments/options on the command line
> if I wish)
> 
> I'd like to add comments to this configuration file :
> ```
> # this is a configuration file for...
> 
> -argument
> 
> # this option...
> -argument -option
> 
> # add your own arguments/options here...
> -etc...
> ```
> 
> but passed directly in this way, these comments become arguments/options
> for `qemu` :
> ```
> $ qemu $(<configuration.qemu)
> qemu-system-x86_64: # this is a configuration file for...: Could not open
> '# this is a configuration file for...': No such file or directory
> ```
> 
> I can eliminate them using `grep` like this :
> `$ qemu $(grep -v '^#' configuration.qemu)`
> 
> but it seems to me complicated and time-consuming to enter.
> 
> so I wanted to use `grep` as shebang :
> ```
> #!/usr/bin/grep -v '^#'
> # this is a configuration file for...
> 
> -argument
> 
> # this option...
> -argument -option
> 
> # add your own arguments/options here...
> -etc...
> ```
> 
> but it doesn't work as I'd hoped :
> ```
> $ qemu $(./configuration.qemu)
> /usr/bin/grep: invalid option -- ' '
> Usage: grep [OPTION]... PATTERNS [FILE]...
> Try 'grep --help' for more information.
> ```
> 
> does anyone have an explanation or another way to get a commented
> configuration file ?
> 
> regards, lacsaP.


What's happening for you is that everything after "grep" on the #!-line
is taken as a single argument to the "grep" utility, which means that
"-v '^#'" is interpreted as the options "-v", "- ", "-'", "-^", etc.
This is why it's complaining about "invalid option -- ' '".

On certain systems, the "env" utility is able to be called with "-S" in
order to split its argument on spaces.  You can use this call
a utility with multiple arguments in #!-lines:

        #!/usr/bin/env -S grep -v '^#'



-- 
Andreas (Kusalananda) Kähäri
Uppsala, Sweden

.



reply via email to

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