是否可以将此bash循环更改为case语句?

时间:2013-05-31 05:03:32

标签: bash loops switch-statement

我在服务台工作,我使用这个脚本来查找我工作中一直需要的信息。 这节省了我一些时间,这是一次很棒的学习经历。我对脚本非常陌生,任何有关此脚本的反馈都会很受欢迎。我真正想要帮助的是这个循环。

一般反馈也欢迎。

# If more than one member in Array Ask for user input.
if [[ ${tLen} -gt '1' ]]; then
  for (( i=0; i<${tLen}; i++ ));
  do
    # adding one so user selection starts at 1
    # gobble up everything up to last back slash.
    echo "$(($i + 1 ))) ${arr[$i]##*/}"
  done

是否可以将其转换为案例陈述,以防止错误的用户输入?或者另一种处理错误用户输入的方法也很好。提前感谢任何输入!

#!/bin/bash   

# Asking for serial number
echo -e "Enter serial number:"
read SerialNumber
SerialNumberCount=${#SerialNumber}
if [[ ${SerialNumberCount} > "11" ]]; then
  # If Serial is 12 or more characters, set the ModelSerial to four digits
  ModelSerial=$(echo $SerialNumber|awk '{ print substr( $0, length($0) - 3, length($0) ) }')
else
  # Set serial to three characters
  ModelSerial=$(echo $SerialNumber|awk '{ print substr( $0, length($0) - 2, length($0) ) }')
fi
# Set ModelURL based on length of Serial number   

# Creating temp dir/file trap for parsing html
TMPDIR=${TMPDIR:-/tmp}
temporary_dir=$(mktemp -d "$TMPDIR/XXXXXXXXXXXXXXXXXXXXXXXXXXXXX") || { echo "ERROR creating a temporary file" >&2; exit 1; }
trap 'rm -rf "$temporary_dir"' 0
trap 'exit 2' 1 2 3 15
temp="$temporary_dir/$RANDOM-$RANDOM-$RANDOM"   

# First downloaded html page
lookupUrl=$(echo "http://everymac.com/ultimate-mac-lookup/?search_keywords=${ModelSerial}")
curl -s -o $temp -L $lookupUrl    

# Parsing partial links 
webRslt=$(cat $temp |   grep -o '<a href=['"'"'"][^"'"'"']*['"'"'"]' |   sed -e 's/^<a href=["'"'"']//' -e 's/["'"'"']$//'|grep /systems* |grep ".html" )   

# Creating Array of partial links
arr=($webRslt)    

# Length of Array
tLen=${#arr[@]}   

# If more than one member in Array Ask for user input.
if [[ ${tLen} -gt '1' ]]; then
  for (( i=0; i<${tLen}; i++ ));
  do
    # adding one so user selection starts at 1
    # gobble up everything up to last back slash.
    echo "$(($i + 1 ))) ${arr[$i]##*/}"
  done

  # Ask user for input 
  printf "\n"
  echo "Please enter 1-${tLen} --> "
  read -r sel
  # Subtracting one from selection for correct Array member
  ArraySel=$((sel - 1)) 
  curl  "${arr["$ArraySel"]/#/http://everymac.com}" -s -o "$temp"
else
  # If array only has one member do this... 
  curl  "${arr[0]/#/http://everymac.com}" -s -o "$temp"
fi
clear   

# Parsing html for screen printing 
get_value() { perl -nE 'say $1 if m!>\Q'"$1"'\E</td>\s*<td[^>]+>([^<]+)<!' <"$temp"; }
VAR1=$(get_value "Apple Model No:")
VAR2=$(get_value "Apple Order No:")
VAR3=$(get_value "Model ID:")
VAR4=$(get_value "Processor Speed:")
VAR5=$(get_value "Standard RAM:")
VAR6=$(get_value "Maximum RAM:")
VAR7=$(get_value "RAM Type:")
VAR8=$(get_value "Pre-Installed MacOS:")
VAR9=$(get_value "Maximum MacOS:")
VAR10=$(get_value "Minimum Windows:")
VAR11=$(get_value "Maximum Windows:")
VAR12=$(get_value "Original Price (US):")
tput setaf 2
printf "\033[1mRam & Apple Model Information Information (experimental beta)\033[m\n\n"
printf "Apple Model No:\t\\t\033[1m$VAR1\033[m\n"
printf "Apple Order No::\t\033[1m$VAR2\033[m\n"
printf "Model ID:\t\\t\033[1m$VAR3\033[m\n"
printf "Processor Speed:\t\033[1m$VAR4\033[m\n"
printf "Standard RAM:\t\\t\033[1m$VAR5\033[m\n"
printf "Maximum RAM:\t\\t\033[1m$VAR6\033[m\n"
printf "RAM Type:\t\\t\033[1m$VAR7\033[m\n"
printf "Pre-Installed MacOS:\t\033[1m$VAR8\033[m\n"
printf "Maximum MacOS::\t\t\033[1m$VAR9\033[m\n"
printf "Minimum Windows:\t\033[1m$VAR10\033[m\n"
printf "Maximum Windows:\t\033[1m$VAR11\033[m\n"
printf "Original Price (US):\t\033[1m$VAR12\033[m\n"
printf "\n\n"
printf "See URL below for more details."
printf "\n\n"   

# Print page that is being parsed on  screen. 
if [[ ${tLen} -gt '1'  ]]; then
  tput setaf 2
  echo "${arr[$ArraySel]/#/http://everymac.com}"
else
  tput setaf 2
  echo "${arr[0]/#/http://everymac.com}"
fi
printf "\n\n"

1 个答案:

答案 0 :(得分:1)

说实话,我并不完全理解你的问题;但听起来你需要的是select命令(在§3.2.4.2 "Conditional Constructs" of the Bash Reference Manual中描述),它提供了一个编号项目菜单供用户选择。在您的情况下,您可以写:

if (( ${#arr[@]} == 1 )) ; then
    url="${arr[0]}"
else
    # present the user with all elements of the array, numbered,
    # and store the selected element in $url:
    select url in "${arr[@]}" ; do break ; done
fi
curl "${url/#/http://everymac.com}" -s -o "$temp"