Combining a declared variable with a temporary variable

Hi folks!

Kind of a noob question... from an OLD AIX/HPUX Admin.

I am writing a script to ease use of a command; an extended aliasing if you will. What I want to do is set several variables (OPT1, OPT2, etc) with command arguments, such as --help, --list-all, etc. Later in the script, I have a menu that queries for a number option and the selects the appropriate declared variable.

What i need to do is pass $COMMAND $OPT"$NUMBER". How is this quoted to make it happen? See my code below.

ANY help is GREATLY appreciated.

#!/bin/bash
#ident  "@(#)fwcmd.bash  v2.1  Last Mod: 120918" 
#
########################################################################
# fwcmd.bash
#
# Christopher B. Lee
# for LeeVault
#
########################################################################
#
# Last Modification: 120918
# Purpose:     Script to run selected firewall-cmd commands from a menu-like
#            interface
# Scope:    Runs firewall-cmd with either stated options on the command line,
#        or in the absence of command line options, presents a menu of 
#        frequently used options.
#
# Modification History
#
########################################################################
#
# DECLARED VARIABLES
FWCMD="firewall-cmd --"
OPT1="state"
OPT2="panic-on"
OPT3="panic-off"
OPT4="query-panic"
OPT5=""
OPT6=""
OPT7="help"

########################################################################

if [ -n "$1" ]
then 
    $FWCMD$1
    exit 0
else
    echo "FIREWALL COMMANDER"
    echo ""
    echo "Select From the floowing options:"
    echo "     (1)Show STATE of the Firewall"
    echo "     (2)Set Panic Mode ON"
    echo "     (3)Set Panic Mode OFF"
    echo "     (4)Show the Panic Mode Status"
    echo "     (5)NULL"
    echo "     (6)NULL"
    echo "     (7)Print out the HELP information"
    echo "Selection (1-7)"
fi

read PICK
if [ -n "$PICK" ]
then
    $FWCMD$OPT"($PICK)"
else
    echo "Selection not made."
fi

exit 0
EOF

Quoting won't make it possible. What you are trying to do is not allowed by the shell command language. It could be done using the eval command, but there can be undesired side-effects which lead most people to avoiding its use unless you really know what you're doing and knowing every possible setting for all variables that are being processed by eval .

Since you're using bash as your shell, you might be better off creating an array of options and using the selection value read into the PICK variable as a subscript into that array to choose the option to be applied.

1 Like

Does your bash version offer the select command, and/or variable indirection?

1 Like

Thank Don!

I was starting to suspect that was the case. I did some quick research after posting and I didn't see where what I wanted to do was even referenced, and surely I can't be the only person that ever wanted to do something like that.

Damn, but I'm rusty. It's been nearly 20 years since I wrote a shell script.... and those were all in KSH. :o

For that reason, I will rule out `eval`.

I did come up with a functional, if inelegant solution; it works as I want even if it IS ugly as hell.:slight_smile:

Thanks for the reply.

--- Post updated at 11:50 AM ---

RudiC,

It does, and I'll read up on using it. This won't be the last time I write this type of script. The CLI commands keep getting longer and longer. Running Centos 7 as a home media/file server.

I did find an inelegant solution, as follows:

Thanks for your reply!

#!/bin/bash
# ident    "@(#)fwcmd.bash  v1.0  Last Mod: 120918" 
#
########################################################################
# fwcmd.bash
#
# Christopher B. Lee
# for LeeVault
#
########################################################################
#
# Last Modification: 120918
# Purpose: @(#)    Script to run selected firewall-cmd commands from a  
#    @(#) menu-like interface
# Scope:    Runs firewall-cmd with either stsed options on the command line,
#        or in the absence of command line options, presents a menu of 
#        frequently used options.
#
# Modification History
#
########################################################################
#
# DECLARED VARIABLES
FWCMD="firewall-cmd --"
OPT1="state"
OPT2="reload"
OPT3="panic-on"
OPT4="panic-off"
OPT5="query-panic"
OPT6="list-all"
OPT7="add-service="
OPT8="add-port="
OPT9="help"

########################################################################

if [ -n "$1" ]  # Test whether command-line argument is present (non-empty).
then 
    $FWCMD$1
    exit 0
else
    echo ""
    echo "FIREWALL COMMANDER"
    echo ""
    echo "Select From the floowing options:"
    echo "     (1) Show STATE of the Firewall."
    echo "     (2) Reload the Firewall configuration."
    echo "     (3) Set Panic Mode ON."
    echo "     (4) Set Panic Mode OFF."
    echo "     (5) Show the Panic Mode Status."
    echo "     (6) Show the list of allowed services."
    echo "     (7) Add a Service."
    echo "     (8) Add a Port."
    echo "     (9) Print out the HELP information."
    echo "Selection (1-7)"
fi

read -s PICK
#echo "$PICK"     #for debugging
if [[ -n "$PICK" ]]
    if [[ $PICK = "1" ]]
        then
        OPTION="$OPT1"
    elif [[ $PICK = "2" ]]
        then
        OPTION="$OPT2"
    elif [[ $PICK = "3" ]]
        then
        OPTION="$OPT3"
    elif [[ $PICK = "4" ]]
        then
        OPTION="$OPT4"
    elif [[ $PICK = "5" ]]
        then
        OPTION="$OPT5"
    elif [[ $PICK = "6" ]]
        then
        OPTION="$OPT6"
    elif [[ $PICK = "7" ]]
        then
        echo "Please type in the <ServiceName> to add."
        read SERVICE
        OPTION="$OPT8$SERVICE --permanent"
        OPTION="$OPT7"
    elif [[ $PICK = "8" ]]
        then
        echo "Please type in the <PortName/Type> TCP or UDP, to add."
        read PORT
        OPTION="$OPT8$PORT --permanent"
    elif [[ $PICK = "9" ]]
        then
        OPTION="$OPT9"    
    elif [[ $PICK != {1,2,3,4,5,6,7,8,9} ]]
        then
        echo "No valid selection was made."
        exit 0
    fi
then
    $FWCMD${OPTION}
    echo "Script Complete."
    exit 0
fi

EOF

See if this quite incomplete sketch can be used as a starting point for your future solution:

FWCMD="echo firewall-cmd --"
OPT1="state"
OPT2="panic-on"
OPT3="panic-off"
OPT4="query-panic"
OPT5=""
OPT6=""
OPT7="help"

if [ -n "$1" ]
  then  $FWCMD$1
        echo exit 0
  else  echo "FIREWALL COMMANDER"
        echo ""
        PS3="Select from above (q or <ctrl>D to quit): "
        select PICK in "Show STATE of the Firewall" "Set Panic Mode ON" "Set Panic Mode OFF" "Show the Panic Mode Status" "NULL" "NULL" "Print out the HELP information"
          do    [ "${REPLY,,}" = 'q' ] && break
                TMP=OPT$REPLY
                $FWCMD${!TMP}
          done
fi

It uses the select statement incl. the PS3 variable for building the menu, and "variable indirection" to build the command, here used with echo for simplicity. Supplying arguments as command line arguments is a better choice than making them a part of the command, btw.

A case statement leans itself towards being used in the loop, as is the use of array as alluded to by Don Cragun.

1 Like

Freaking outstanding, RudiC!

This construct WILL bed used. :slight_smile: