shell 编写以下类型的bash脚本的更有效的方法是什么

kokeuurv  于 2023-01-21  发布在  Shell
关注(0)|答案(1)|浏览(151)
#!/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."

请不要关注剧本的内容,而要关注剧本的类型。
我需要一种高效的方法(更少的代码行)来运行5个以上的步骤,其中每个步骤基于$Var1的值(在本例中为$VENDOR)执行相同操作的不同变体

llew8vvj

llew8vvj1#

简化版:

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

相关问题