针对一系列if语句的优化思路

时间:2017-01-03 00:26:09

标签: linux bash if-statement optimization

我想尝试压缩下面的if语句,基本上以任何可能的方式缩小代码。

我从文件中获取数据以获取数据并且在代码中,但是就目前而言,我可以缩小代码吗?

`

#! /bin/bash
shopt -s nocasematch extglob
callsign() {
   while true; do
      read -ep "Insert your call sign: " input
      if [[ $input = [aknw]@(|[a-z])[0-9][a-z]@(|[a-z]@(|[a-z])) ]]; then
         break
      else
         echo "please insert valid call sign."
      fi
   done
clear
CAC
}
function CAC() {
ping -c 1 "www.google.com" &> /dev/null && lynx --dump http://callsign.ualr.edu/cdetail.php?call=$input|grep -ve '^   ' || CallsignDetails
CCyn
}
function CCyn() {
while true; do
        read -p "Would you like to Lookup another callsign [y/n]" yn
        case $yn in
           [Yy]* ) Last;;
           [Nn]* ) exit;;
           * ) echo "please answer yes or no.";;
        esac
    done
}
function Save() {
cat << log >> CallSign.log
$(ping -c 1 "www.google.com" &> /dev/null && lynx --dump http://callsign.ualr.edu/cdetail.php?call=$input|grep -ve '^   ' || CallsignDetails)
____________________________________________
log
callsign
}
function Last() {
while true; do
        read -p "Would you like to Log The Last callsign [y/n]" yn
        case $yn in
           [Yy]* ) Save;;
           [Nn]* ) callsign;;
           * ) echo "please answer yes or no.";;
        esac
    done
}
function CallsignDetails() {
CsT=$(echo $input | awk '{print toupper($0)}')
clear
LC_ALL=C grep -w $CsT l_amat/HD.dat | tail -1 | awk -F '|' '{print $5}'
echo ""
LC_ALL=C grep -w $CsT l_amat/EN.dat | tail -1 | awk -F '|' '{print $8}'
echo ""
LC_ALL=C grep -w $CsT l_amat/EN.dat | tail -1 | awk -F '|' '{print $16}'
echo ""
echo "$(echo $(LC_ALL=C grep -w $CsT l_amat/EN.dat | tail -1 | awk -F '|' '{print $17}'), $(LC_ALL=C grep -w $CsT l_amat/EN.dat | awk -F '|' '{print $18, $19}'))"
echo ""
echo ""
PClass="$(LC_ALL=C grep -w $CsT -w l_amat/AM.dat | tail -1 | awk -F '|' '{print $17}')"
Class="$(grep -w $CsT l_amat/AM.dat | tail -1 | awk -F '|' '{print $6}')"
if [ "$Class" == "E" ];then
echo "Class: Extra"
else
if [ "$Class" == "G" ];then
echo "Class: General"
else
if [ "$Class" == "A" ];then
echo "Class: Advanced"
else
if [ "$Class" == "P" ];then
echo "Class: Technician Plus"
else
if [ "$Class" == "N" ];then
echo "Class: Novice"
else
if [ "$Class" == "T" ];then
echo "Class: Technician"
else
if [ "$Class" != "T" ];then
echo "Class: Unknown"
fi
fi
fi
fi
fi
fi
fi
if [ "$PClass" == "E" ];then
echo "Previous Class: Extra"
else
if [ "$PClass" == "G" ];then
echo "Previous Class: General"
else
if [ "$PClass" == "A" ];then
echo "Previous Class: Advanced"
else
if [ "$PClass" == "P" ];then
echo "Previous Class: Technician Plus"
else
if [ "$PClass" == "N" ];then
echo "Previous Class: Novice"
else
if [ "$PClass" == "T" ];then
echo "Previous Class: Technician"
else
if [ "$PClass" != "T" ];then
echo "Previous Class: N/a"
fi
fi
fi
fi
fi
fi
echo "Previous Callsigns: $(LC_ALL=C grep -w $CsT l_amat/AM.dat | tail -1 | awk -F '|' '{print $16}')"
LStatus="$(LC_ALL=C grep $CsT -w l_amat/HD.dat | tail -1 | awk -F '|' '{print $6}')"
if [ "$LStatus" == "A" ];then
echo "License status: Active"
else
if [ "$LStatus" == "C" ];then
echo "License status: Canceled"
else
if [ "$LStatus" == "E" ];then
echo "License status: Expired"
fi
fi
fi
echo "Grant date: $(LC_ALL=C grep -w $CsT l_amat/HD.dat | tail -1 | awk -F '|' '{print $8}')"
echo "Expiration date: $(LC_ALL=C grep -w $CsT l_amat/HD.dat | tail -1 | awk -F '|' '{print $9}')"
fi
CCyn
}
callsign`

1 个答案:

答案 0 :(得分:0)

在我展示我称之为&#34;对&#34;要做到这一点,让我指出一些可能改进当前代码的变化。首先,将结果存储在变量中并在比较中使用:

Class=$(LC_ALL=C grep -w $CsT l_amat/AM.dat | awk -F '|' '{print $6}')
if [ "$Class" == "E" ];then
    ...

其次,在变量引用(如$CsT)周围使用双引号来保护它们免受意外的单词拆分和通配符扩展:

Class=$(LC_ALL=C grep -w "$CsT" l_amat/AM.dat | awk -F '|' '{print $6}')

第三,使用else if制作一个多条件elif语句,而不是使用if深度嵌套if语句:

Class=$(LC_ALL=C grep -w "$CsT" l_amat/AM.dat | awk -F '|' '{print $6}')
if [ "$Class" == "E" ];then
    echo "Class: Extra"
elif [ "$Class" == "G" ];then
    echo "Class: General"
elif [ "$Class" == "G" ];then
    ...
fi    # Note: only a single `fi` needed to close off the whole thing!

另外,为了便于携带,我建议在=测试中使用==代替[ ]。 bash接受两者,但有些shell只接受=;如果你只是为bash而不是通用的POSIX shell写作,那么[[ ]]通常比[ ]更受欢迎。

但所有这些都是微小的调整;有一个shell构造,专门用于将单个字符串与一堆可能的值(或模式)进行比较:case语句:

Class=$(LC_ALL=C grep -w "$CsT" l_amat/AM.dat | awk -F '|' '{print $6}')
case "$Class" in
    E) echo "Class: Extra";;
    G) echo "Class: General";;
    A) echo "Class: Advanced";;
    ...
    *) echo "Class: Unknown";;
esac