-1
#!/bin/bash

echo "What is the vendor? (DELL or CISCO)" #this is an example for reference
read VENDOR                                #this is an example for reference

if [ "$VENDOR" = "DELL" ]; then
  echo "Step 1: Configuring DELL switch..."
  # DELL-specific commands go here
elif [ "$VENDOR" = "CISCO" ]; then
  echo "Step 1: Configuring CISCO switch..."
  # CISCO-specific commands go here
else
  echo "Invalid vendor entered. Exiting script."
  exit 1
fi

if [ "$VENDOR" = "DELL" ]; then
  echo "Step 2: Configuring DELL router..."
  # DELL-specific commands go here
elif [ "$VENDOR" = "CISCO" ]; then
  echo "Step 2: Configuring CISCO router..."
  # CISCO-specific commands go here
fi

if [ "$VENDOR" = "DELL" ]; then
  echo "Step 3: Configuring DELL firewall..."
  # DELL-specific commands go here
elif [ "$VENDOR" = "CISCO" ]; then
  echo "Step 3: Configuring CISCO firewall..."
  # CISCO-specific commands go here
fi

if [ "$VENDOR" = "DELL" ]; then
  echo "Step 4: Configuring DELL VPN..."
  # DELL-specific commands go here
elif [ "$VENDOR" = "CISCO" ]; then
  echo "Step 4: Configuring CISCO VPN..."
  # CISCO-specific commands go here
fi

if [ "$VENDOR" = "DELL" ]; then
  echo "Step 5: Configuring DELL WAN..."
  # DELL-specific commands go here
elif [ "$VENDOR" = "CISCO" ]; then
  echo "Step 5: Configuring CISCO WAN..."
  # CISCO-specific commands go here
fi

echo "Script completed successfully."

please don't focus on the content of the script but rather on the type of script.

I want an efficient way (fewer lines of code) to run 5+ steps where each step executes different variations of the same actions based on the value of $Var1 (in this example $VENDOR)

Jens
  • 67,715
  • 15
  • 98
  • 113
  • Why are you checking the value of `$VENDOR` before every step? Just check it once, then do the sequence of steps you need for that vendor. (It sounds like there should be two separate vendor-specific scripts in the first place.) – chepner Jan 19 '23 at 12:30

1 Answers1

1

Simplified:

#!/bin/bash

read -p "What is the vendor? (DELL or CISCO) >>> " vendor

for step in {1..5}; do
    case "$vendor" in
        CISCO)
            echo "CISCO step $step"
        ;;
        DELL)
            echo "DELL step $step"
        ;;
        *)
            echo >&2 "Unknown brand"
        ;;
    esac
done

Don't use UPPER case variables

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223