Combining a declared variable with a temporary variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combining a declared variable with a temporary variable
# 1  
Old 12-09-2018
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.


Code:
#!/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

# 2  
Old 12-10-2018
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.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 12-10-2018
Does your bash version offer the select command, and/or variable indirection?
This User Gave Thanks to RudiC For This Post:
# 4  
Old 12-10-2018
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. Smilie


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.Smilie


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!



Code:
#!/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

# 5  
Old 12-10-2018
See if this quite incomplete sketch can be used as a starting point for your future solution:


Code:
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.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 12-10-2018
Freaking outstanding, RudiC!


This construct WILL bed used. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

2. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

3. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

4. Shell Programming and Scripting

write in file using printf with variable declared with a phrase

Hi guys, kinda new to unix/linux, could you please help me figure this out. i need to write in a file using printf. printf "%-20s %-40s %-20s\n" $a $b $c >> out.txt but a, b and c are declared in a header file: a='I am a dog' b='I am a cat' c='I am a fish' i want the file to look like... (1 Reply)
Discussion started by: kokoro
1 Replies

5. Shell Programming and Scripting

Combining multiple variables into new variable

Hello, I am a new joiner to the forum, and have what i hope is a simple question, however I can't seem to find the answer so maybe it is not available within bash scripting. I intend to use the below script to archive files from multiple directories at once by using a loop, and a variable (n)... (10 Replies)
Discussion started by: dring
10 Replies

6. Shell Programming and Scripting

Variable not found error for a variable which is returned from stored procedure

can anyone please help me with this: i have written a shell script and a stored procedure which has one OUT parameter. now i want to use that out parameter as an input to the unix script but i am getting an error as variable not found. below are the unix scripts and stored procedure... ... (4 Replies)
Discussion started by: swap21783
4 Replies

7. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

8. Shell Programming and Scripting

shell script: Bind variable not declared

Hi Friends, I am trying to run a sql query from shell script as below but I get "Bind variable "1" not declared" error. 1.sh shell script has following: sDb="abc/xyz@aaa" a="1.sql" sqlplus -s $sDb @$a $1 1.sql file has following: spool Result.tmp append select cust_name from orders... (1 Reply)
Discussion started by: ppat7046
1 Replies

9. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

10. Shell Programming and Scripting

Combining Two fixed width columns to a variable length file

Hi, I have two files. File1: File1 contains two fixed width columns ID of 15 characters length and Name is of 100 characters length. ID Name 1-43<<11 spaces>>Swapna<<94 spaces>> 1-234<<10 spaces>>Mani<<96 spaces>> 1-3456<<9 spaces>>Kapil<<95 spaces>> File2: ... (4 Replies)
Discussion started by: manneni prakash
4 Replies
Login or Register to Ask a Question