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


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

简体:

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

不要使用大写变量

相关内容

  • 没有找到相关文章

最新更新