60天shell脚本计划-4/12-渐入佳境

--作者:飞翔的小胖猪

--创建时间:2021年2月11日

--修改时间:2021年2月15日

说明

 每日上传更新一个shell脚本,周期为60天。如有需求的读者可根据自己实际情况选用合适的脚本,也可在评论区留言提出脚本需求,作者会尽快根据需求编写相关脚本对功能进行实现。

 每篇文章包含5个脚本。

总进度:4/12

上一篇脚本链接:https://www.cnblogs.com/Pigs-Will-Fly/p/14380780.html

下一篇脚本链接:https://www.cnblogs.com/Pigs-Will-Fly/p/14401712.html

主要内容

21年2月11日-猜大小脚本

************************************************************************************************************************************************************************************************************************************

脚本说明

脚本调用系统变量RANDOM生成一个0‐32767的随机数,通过取余的方式得到0-100的数。每次读取用户如输入的数字和生成的数对比,如果相等则提示成功,同时判断是猜了几次猜对的。
加入判断用户输入的确实为数字,且小于100。否则提示输入错误的类型。暂时未实现。

文件说明

Bulls_and_Cows.sh:脚本主体文件

脚本主体

[root@135 16_Bulls_and_Cows]# cat Bulls_and_Cows.sh 
#!/bin/bash
# RANDOM 为系统自带的系统变量,值为 0‐32767的随机数,通过取余将随机数变为 0‐100的随机数。
rownum=$[RANDOM%100]
let input_n=1
echo "自动生成了一个0‐100的随机数。"
while  :
do
  read -p "第 ${input_n} 次尝试。请输入你认为对的数字: " input_v
  if [ $input_v -eq $rownum ];then
    if [ $input_n -eq 1 ];then
      echo "这也太厉害了吧,一次就猜出来了!!!"
    elif [ $input_n -eq 2  ];then
      echo "你真厉害,第二次就猜中了!!!"
    elif [ $input_n -le 4 ];then
      echo "运气不错只用了${input_n}次就猜对了!!!"
    else
      echo "呃 呃 呃 呃 呃 呃 呃 经过${input_n}次尝试,总算猜出来了!!!"
    fi
    echo "生成的数为 --${rownum}--"
    exit
  elif [ $input_v -gt $rownum ];then
    echo "Sorry,猜大了"
    let input_n=${input_n}+1
  else
    echo "Sorry,猜小了"
    let input_n=${input_n}+1
  fi
done

结果

**************************************************************************************************************2021年2月11日脚本结束*****************************************************************************************************************

 

21年2月12日-判断输入值类型脚本

 ************************************************************************************************************************************************************************************************************************************

脚本说明

脚本获取用户输入的值,依次读取输入字符串每一个位置的元素类型最后组合判断输入的值是什么类型。

文件说明

Judgment_input_value_type.sh:脚本主体文件

脚本主体

[root@135 17_Judgment_value_type]# cat Judgment_input_value_type.sh 
#!/bin/bash
# 判断用户输入的数据类型(字母、数字或其他)
#定义控制变量,如果出现某一匹配的类型则对应变量置1
str_n=0
int_n=0
decimal_n=0
other_n=0

#输入一段非汉字的字符串或单个字符。
read -p "请输入一个字符或字符串: " input_v
input_fv=${input_v:-null}

#如果啥都没有输入直接退出。
if [ "${input_fv}" == "null" ];then
  echo "啥都没有输入,包括空格。"
  exit 1
fi

str_len=${#input_fv}
#echo "输入字符串长度为${#input_fv} "
for i in `seq 0 $[str_len-1] `
do
  #echo  "当前位置的字符为${input_v:$i:1}"
  str_check=${input_v:$i:1}
  case "${str_check}" in
    [a-z]|[A-Z])
      #echo "字母"
      str_n=1
      ;;
    [0-9])
      #echo "数字"
      int_n=1
      ;;
    ".")
      #echo "小数点"
      decimal_n=1
      ;;
    *)
      #echo "其他及特殊字符”
      other_n=1
      ;;
  esac
done

if [ ${str_n} -eq 1 ] ;then
  #判断只要字符串中出现字母则表示为字符串。
  echo "入的字符串: ${input_fv} , 类型为普通字符串。" 
elif [ ${int_n} -eq 1 ] && [ ${str_n} -eq 0 ] && [ ${other_n} -eq 0 ];then
  #判断只要输入中包含数字同时不包含字母和特殊字符则为数字型
  if echo "${input_fv}"|grep '^\..*' &>/dev/null;then
     echo "输入的字符串: ${input_fv} , 类型为浮点型数字。"
  else
     echo "输入的字符串: ${input_fv} , 类型为整型数字。"
  fi
else
  echo "输入的字符串: ${input_fv} , 类型为特殊字符串。"
  
fi

结果

**************************************************************************************************************2021年2月12日脚本结束*****************************************************************************************************************

 

21年2月13日-操作系统识别脚本

 ************************************************************************************************************************************************************************************************************************************

脚本说明

脚本通过读取系统中的文件判断本地操作系统厂商及版本号,并输出结果到屏幕上。

文件说明

system_identifier.sh:脚本主体文件

脚本主体

[root@135 18_system_identifier]# cat system_identifier.sh 
#!/bin/bash
system_identifier(){
  #Manufacturer=`cat /proc/version|grep centos &>/dev/null`
  if cat /proc/version|grep -i centos &>/dev/null;then
    echo "This system Manufacturer : Centos"
    echo -e "Version: `cat /etc/redhat-release | grep  -o -E '[6-8].*[0-9]'` \n"
    echo -e "Version: `rpm -q centos-release  | cut -d- -f3` \n"
  elif cat /proc/version|grep -i redhat &>/dev/null;then
    echo "This system Manufacturer : Redhat"
    echo -e "Version: `cat /etc/redhat-release | grep  -o -E '[6-8].*[0-9]'` \n"
  elif cat /proc/version|grep -i debian &>/dev/null;then
    echo "This system Manufacturer : Debian"
  elif cat /proc/version|grep -i Ubuntu &>/dev/null;then
    echo "This system Manufacturer : Ubuntu"
  elif cat /proc/version|grep -i suse &>/dev/null;then
    echo "This system Manufacturer : Suse"
  else
    echo "This system Manufacturer : unknown"
  fi
}

main(){
  system_identifier 
}
main

结果

 

**************************************************************************************************************2021年2月13日脚本结束*****************************************************************************************************************

 

 

21年2月14日-打印日历脚本

************************************************************************************************************************************************************************************************************************************

脚本说明

脚本读取用户输入的日期打印指定日期的日历信息,如果没有输入任何值则打印当前时间的日历。

文件说明

print_calendar.sh:脚本主体文件

脚本主体

#!/bin/bash
#定义打印日历函数
Print_calendar(){
  #设置字体颜色
  tiffcolor="\033[0;35m"
  menucolor="\033[0;33m"
  todaycolor="\033[0;35;44m"
  start="\033[0m"

  #获取当前日期信息
  now_month=`date +%m`
  now_day=`date +%d`
  now_year=`date +%Y`
  now_start_wek_day=`date -d "$now_year-$now_month-01" +%w`
  now_nextmonth=$[now_month+1] 
  now_nextmonth_year=$[now_year]


  #从屏幕中获取数据
  echo "Please enter the time you want to print the calendar. The default is the current month."
  read -p  "Format(YYYY-MM-DD): " input_timef

  input_time=${input_timef:-null}
  if [  "${input_time}" == "null" ];then
    echo "未输入任何值,默认打印当前月的日历信息."
    month=${now_month}
    day=${now_day}
    year=${now_year}
    start_wek_day=${now_start_wek_day}
    nextmonth=${now_nextmonth}
  else
    echo "如果有输入值则把输入值赋给变量"
    year=`date -d "${input_time}" +%Y`
    month=`date -d "${input_time}" +%m`
    day=`date -d "${input_time}" +%d`
    start_wek_day=`date -d "${year}-${month}-01" +%w`
    echo "测试"
    nextmonth=$[month+1]
  fi

  #处理获取到的时间
  if [ $nextmonth -eq 13 ];then
    nextmonth=1
    now_nextmonth_year=$[year+1]
  fi

  #计算本月一共有多少天
  diff_day=$[(($(date -d "$now_nextmonth_year-$nextmonth-01" +%s) - $(date -d "$year-$month-01" +%s))/(24*60*60) )]
  echo "本月一共有 $diff_day 天."

  #打印标题
  echo -en "${menucolor}"
  echo -en "\t  $year $month\n"
  echo "SUN MON TUE WEN THU FRI SAT"
  echo -en "${start}"
  #打印空格
  if [ $start_wek_day -ne 0 ];then
    for((i=1;i<=$start_wek_day;i++))
    do
      #此处是格式调整,如果结果显示没有特别对齐则调整echo输出的空格数量.
      echo -n "   "
      echo -n " "
    done
  fi

  #打印日期
  for((i=1;i<=$diff_day;i++))
  do
    printf "%s" " "
    echo -en "${tiffcolor}"
    #今天或指定的日期高亮
      if [ $day -eq $i ];then
        echo -en "${todaycolor}"
      fi
    printf "%2d" $i
    echo -en "${start}"
    echo -en " "
    if [ $((($start_wek_day+$i)%7)) == 0 ];then
      echo ""
    fi
  done
  echo ""
}

#定义主函数
main(){
Print_calendar
}

#调用主函数
main

#最简单的实现方式是直接调用cal 命令.
#如需要打印某一年的所有日历 命令格式(cal 年份) 例:cal 2021
#如需要打印特定年份的某个月日历 命令格式(cal 月份 年份) 例:cal 12 2021

结果

**************************************************************************************************************2021年2月14日脚本结束*****************************************************************************************************************

 

21年2月15日-冒泡排序脚本

 ************************************************************************************************************************************************************************************************************************************

脚本说明

用户输入一组数字,脚本使用冒泡排序法把用户输入的数字按照从小到大的顺序排序。

文件说明

bubble_sort.sh:脚本主体文件

脚本主体

[root@135 20_bubble_sort]# cat bubble_sort.sh 
#!/bin/bash
bubble_sort(){
  n=0
  #循环读取输入字符,如果输入Q/q的话输入完成。
  while read -p "Please Input $[n+1] number,input q/Q Key input completed : " arr[$n]
  do
    #如果输入q或者Q则退出输入。
    if [ ${arr[$n]} == "q" ] || [ ${arr[$n]} == "Q" ];then
      unset arr[$n]
      break
    fi
    n=$[$n+1]
  done             
 
  len=${#arr[*]}    #取数组长度
  echo "总长度为: $len"
  #循环比对最大的值放置到最后去。
  for((i=0;i<len;i++))
  do
    for((j=0;j<len-i-1;j++))
    do
      #使用t表示下一个元素的下标
      t=$[$j+1]
      #判断如果当前的值大于下一个值,循环比对arr[len-i-1]的值是不是最大。
      if [[ ${arr[$j]}  -gt ${arr[$t]} ]];then
        term=${arr[$j]}
        arr[$j]=${arr[$t]}
        arr[$t]=$term
      fi
    done
  done
 
  #输出排序后的元素
  for((a=0;a<len;a++))
  do
    echo ${arr[$a]}
  done
}

main(){
  bubble_sort
}

main

结果

**************************************************************************************************************2021年2月15日脚本结束*****************************************************************************************************************

posted @ 2021-02-13 09:02  飞翔的小胖猪  阅读(98)  评论(0编辑  收藏  举报