Sponsored Content
Top Forums Shell Programming and Scripting Combining a declared variable with a temporary variable Post 303027142 by clee on Monday 10th of December 2018 06:50:38 AM
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

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 01:58 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy