0

I am setting up firewall using firewall-cmd but it takes lots of time just to type firewall-cmd all the time.

It is possible to create temporary "virtual" shell or something which will append firewall-cmd to each following inputs ?

Example :

$ coolcommand firewall-cmd
> --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
[.....]
> --add-service https
> ^C
$

Thanks

1 Answers1

0

A loop with eval, like this:

coolcommand () { while IFS= read -rep "> $1 " line; do eval "$1 $line"; done; }

Then invoke coolcommand firewall-cmd like in your example.

Note firewall-cmd gets "glued" to the beginning of every line you type, but it's still the shell who evaluates the resulting string. So $, *, ;, #, quotes or such are still special. In particular after coolcommand firewall-cmd, typing --list-all; date will run firewall-cmd --list-all and date (not firewall-cmd date).


Improvements for better experience:

  • It's possible to pass a command with argument(s) as well. Example:

      coolcommand 'printf "foo %s\n"'
    
  • The same as a script. Added history support:

      #!/bin/bash
    

    unset HISTFILE HISTCONTROL=ignoreboth

    while IFS= read -rep "> $1 " line; do history -s "$line" eval "$1 $line" done

The history is not saved when the script is about to exit, still during a single invocation it should work (/ like normally in Bash).