1

I've created a simple bash script to create a select menu which look like this :

1) Run nmap 2) Wireshark 3) metasploit framework 4) Exit

now I want to run these programs as per menu selection. I am very new to bash scripting so looking for anyone's help here.

0

2 Answers 2

1

Read reads ina user input to a variable.

echo 'Select an option
  1) Run nmap 
  2) Wireshark 
  3) metasploit framework 
  4) Exit?'
read OPTION

From here on the $OPION variable is set, test with

if [ "$OPTION" == "1" ]
then
   ...
0

Simple example:

  1. Select script:

    #!/bin/bash
    
    array=()
    
    while IFS= read -r line || [[ -n "$line" ]]
    do
        if [ ! -z "$line" ]; then
            array+=("$line")
        fi
    done < "$1"
    
    select fname in "${array[@]}";
    do
        /bin/bash -c "$fname"
        break;
    done  
    

Script operation:

  1. while loop: Take commands a create an array called array, then
  2. Read the commands from the array and create a menu that you choose from.

Information:

  1. IFS='' (or IFS=) prevents leading/trailing whitespace from being trimmed.

  2. -r prevents backslash escapes from being interpreted.

  3. || [[ -n $line ]] prevents the last line from being ignored if it doesn't end with a \n (since read returns a non-zero exit code when it encounters EOF).

  4. [ ! -z "$line" ] ignore blank lines in the source file.

  5. ${array[@]} array values

Not the answer you're looking for? Browse other questions tagged or ask your own question.