我写了一个像这样运行的脚本:
./check.sh site_location
使用案例循环为我公司的5个地理位置网站中的每一个编写脚本。
我想确保用户的输入是我在案例选项中添加的可能性之一,否则脚本将以错误代码退出。
function usage() {
echo "Usage: $0 SiteName"
echo "SiteName can be: ny / ny4a / lax / ams / sg / all "
}
if [ $# -eq 0 ]; then
usage
exit 1
fi
if [ $1 != "ny" ] || [ $1 != "ny4a" ] || [ $1 != "lax" ] || [ $1 != "ams" ] || [ $1 != "sg" ] || [ $1 != "all" ]; then
usage
exit 1
fi
我100%肯定我在写第二个IF语句的方式上有错误(“如果声明或声明或声明......”)。我该如何解决这个问题?
编辑: 完整的脚本看起来像这样:
#!/bin/bash
# This scripts checks the the cache sync between the proxies in each one of the data centers.
# Script by Itai Ganot
#
ny4="ny4.company.com"
lax="lax.company.com"
ams="ams2.company.com"
sg="sg.company.com"
dcdny="nyproxy"
dcdny4a="ny4a"
dcdams="ams"
dcdlax="la"
dcdsg="sg"
checkerPath="/workspace/test/java2db/scripts/"
function usage() {
echo "Usage: $0 SiteName"
echo "SiteName can be: ny / ny4a / lax / ams / sg / all "
}
if [ $# -eq 0 ]; then
usage
exit 1
fi
#if [ $1 != "ny" ] || [ $1 != "ny4a" ] || [ $1 != "lax" ] || [ $1 != "ams" ] || [ $1 != "sg" ] || [ $1 != "all" ]; then
# usage
# exit 1
#fi
case $1 in
ny)
echo -e "\e[36m #===# NY: #===#\e[0m"
cd $checkerPath && ant proxy-size -Ddcd=$dcdny | grep $ny4 | awk -F',' '{print $2}' | sort -n | uniq -c
read -r -p "Would you like to view specific list? [y/n] " reply
if [ "$reply" = "y" ];
then
cd $checkerPath && ant proxy-size -Ddcd=$dcdny | grep $ny4
fi
;;
ny4a)
echo -e "\e[36m #===# NY4A: #===#\e[0m"
cd $checkerPath && ant proxy-size -Ddcd=$dcdny4a | grep $ny4 | awk -F',' '{print $2}' | sort -n | uniq -c
read -r -p "Would you like to view specific list? [y/n] " reply
if [ "$reply" = "y" ];
then
cd $checkerPath && ant proxy-size -Ddcd=$dcdny4a | grep $ny4
fi
;;
lax)
echo -e "\e[36m #===# LAX: #===#\e[0m"
cd $checkerPath && ant proxy-size -Ddcd=$dcdlax | grep $lax | awk -F',' '{print $2}' | sort -n | uniq -c
read -r -p "Would you like to view specific list? [y/n] " reply
if [ "$reply" = "y" ];
then
cd $checkerPath && ant proxy-size -Ddcd=$dclax | grep $lax
fi
;;
sg)
echo -e "\e[36m #===# SG: #===#\e[0m"
cd $checkerPath && ant proxy-size -Ddcd=$dcdsg | grep $sg | awk -F',' '{print $2}' | sort -n | uniq -c
read -r -p "Would you like to view specific list? [y/n] " reply
if [ "$reply" = "y" ];
then
cd $checkerPath && ant proxy-size -Ddcd=$dcdsg | grep $sg
fi
;;
ams)
echo -e "\e[36m #===# AMS: #===#\e[0m"
cd $checkerPath && ant proxy-size -Ddcd=$dcdams | grep $ams | awk -F',' '{print $2}' | sort -n | uniq -c
read -r -p "Would you like to view specific list? [y/n] " reply
if [ "$reply" = "y" ];
then
cd $checkerPath && ant proxy-size -Ddcd=$dcdams | grep $ams
fi
;;
all)
echo -e "\e[36m #===# NY: #===#\e[0m"
cd $checkerPath && ant proxy-size -Ddcd=$dcdny | grep $ny4 | awk -F',' '{print $2}' | sort -n | uniq -c
echo -e "\e[36m #===# NY4A: #===#\e[0m"
cd $checkerPath && ant proxy-size -Ddcd=$dcdny4a | grep $ny4 | awk -F',' '{print $2}' | sort -n | uniq -c
echo -e "\e[36m #===# LAX: #===#\e[0m"
cd $checkerPath && ant proxy-size -Ddcd=$dcdlax | grep $lax | awk -F',' '{print $2}' | sort -n | uniq -c
echo -e "\e[36m #===# SG: #===#\e[0m"
cd $checkerPath && ant proxy-size -Ddcd=$dcdsg | grep $sg | awk -F',' '{print $2}' | sort -n | uniq -c
echo -e "\e[36m #===# AMS: #===#\e[0m"
cd $checkerPath && ant proxy-size -Ddcd=$dcdams | grep $ams | awk -F',' '{print $2}' | sort -n | uniq -c
read -r -p "Would you like to view specific list? [y/n] " reply
if [ "$reply" = "y" ];
then
echo -e "\e[36m #===# NY: #===#\e[0m"
cd $checkerPath && ant proxy-size -Ddcd=$dcdny | grep $ny4
echo -e "\e[36m #===# NY4A: #===#\e[0m"
cd $checkerPath && ant proxy-size -Ddcd=$dcdny4a | grep $ny4
echo -e "\e[36m #===# LAX: #===#\e[0m"
cd $checkerPath && ant proxy-size -Ddcd=$dcdlax | grep $lax
echo -e "\e[36m #===# SG: #===#\e[0m"
cd $checkerPath && ant proxy-size -Ddcd=$dcdsg | grep $sg
echo -e "\e[36m #===# AMS: #===#\e[0m"
cd $checkerPath && ant proxy-size -Ddcd=$dcdams | grep $ams
fi
;;
esac
答案 0 :(得分:4)
您可以使用case
来修改您的条件:
#!/usr/bin/env bash
function usage() {
echo "Usage: $0 SiteName"
echo "SiteName can be: ny / ny4a / lax / ams / sg / all "
}
case "$1" in
ny|ny4a|lax|ams|sg|all) ;;
*) usage ; exit 1; ;;
esac