lomg 发表于 2018-8-25 12:59:33

shell脚本作业

  因感觉sell脚本知识还未完全掌握,因此关于shell、sed以及case总结容后再补.希望老师能见谅。
符号作用=赋值符号“”弱引用其内部的变量引用会被替换为变量值‘’强引用其变量的变量引用会保持原有字符.字符匹配,这是作为正则表达式的一部分,用来匹配任何单个字符。,逗号链接一系列的算术操作,虽然里面的内容全部运行,但只有最后一项被返回\转义字符,如\X等价于"X"或'X':空命令,等价于"NOP"(noop,一个什么也不干的命令).也可以被认为与shell 的内建命令··命令引用(true)作用相同.":"命令是一#!个bash 的内建命令,它的返回值为0,就是shell 返回的true.#注释,用于帮助信息或者忽略暂时不执行的语句${}变量正规表达式,避免变量名提前截断$num位置参数$0,$1,…,${10};(注:如多于两位数需加中括号)$?状态判定参数一般0表示正确(真),其它数字表错误(假)$!最后一个命令执行的后台命令的ID$$运行脚本进程的ID$#传递给脚本或函数的参数的个数$*和$@引用传递给脚本或函数的参数列表()指令群组,将一串指令括起来,执行时shell会产生subshell来执行它们(())bash的内建功能,用于算数运算[]中括号1在流程控制中表示判断式;2在正则表达式中表示"范围"或"集合"[[]]中括号的加强版,当中允许使用||和&&,并且可以使用正则表达式{}花括号指令群组,类似于(),但在当前shell中执行,还可以用于字符串的组合整数测试隐含着做数值大小比较,所以不要给变量引用加引用-gt是否大于,是则为真 ,否则为假-ge是否大于等于-lt是否小于-le是否小于等于-eq是否等于-ne是否不等于字符串测试ASCII数值越大,字符比较时其值越大,(注:使用时应用[[]])>是否大于 $2)); then
  echo "$1 > $2"
  elif
  (($1 < $2)); then
  echo "$1 < $2"
  elif
  (($1 == $2)); then
  echo "$1 = $2"
  else
  echo "disparate"
  fi
  ~
  测试脚本
  # bash test21.sh
  Plz enter Two integer Numbers!
  # bash test21.sh 4
  Plz enter Two integer Numbers!
  # bash test21.sh 4 5
  4 < 5
  # bash test21.sh 5 5
  5 = 5
  # bash test21.sh 5 4
  5 > 4
  求100以内所有奇数之和(至少用3种方法)

[*]  使用for
  #!/bin/bash
  #
  for i in $(seq 100); do
  if [ $[$i%2] -ne 0 ]; then
  h=$[$h+$i]
  fi
  done
  echo "sum:$h"
  测试脚本:
  # bash test22.sh
  sum:2500
  2.使用while
  #!/bin/bash
  #
  h=1
  j=0
  while [ $h -le 100 ]; do
  if [ $[$h%2] -ne 0 ]; then
  j=$[$h+$j]
  fi
  let h++
  done
  echo $j
  测试脚本:
  # bash test23.sh
  2500
  3.暂时想不到
  写一个脚本实现如下功能:
  (1) 传递两个文本文件路径给脚本;
  (2) 显示两个文件中空白行数较多的文件及其空白行的个数;
  (3) 显示两个文件中总行数较多的文件及其总行数
  #!/bin/bash
  #
  if [ $# -lt 2 ]; then
  echo "Plz enter Two path:"
  exit 1
  fi
  if [ ! -f $1 ]; then
  echo "$1 not a common file!"
  exit 1
  fi
  if [ ! -f $2 ]; then
  echo "$2 not a common file!"
  exit 1
  fi
  j=$(grep -c "^$" $1)
  h=$(grep -c "^$" $2)
  m=$(cat $1 |wc -l)
  n=$(cat $2 |wc -l)
  if [ $j -ge $h ]; then
  echo "$1 is max ,the spaceline total:$j"
  else
  echo "$2 is max, the spaceline total:$h"
  fi
  if [ $m -ge $n ]; then
  echo "$1 is max, the lines total:$m"
  else
  echo "$2 is max,the lines total:$n"
  fi
  测试脚本:
  # bash -x test24.sh /etc/fstab /etc/yum.conf
  + '[' 2 -lt 2 ']'
  + '[' '!' -f /etc/fstab ']'
  + '[' '!' -f /etc/yum.conf ']'
  ++ grep -c '^$' /etc/fstab
  + j=1
  ++ grep -c '^$' /etc/yum.conf
  + h=3
  ++ wc -l
  ++ cat /etc/fstab
  + m=10
  ++ wc -l
  ++ cat /etc/yum.conf
  + n=26
  + '[' 1 -ge 3 ']'
  + echo '/etc/yum.conf is max, the spaceline total:3'
  /etc/yum.conf is max, the spaceline total:3
  + '[' 10 -ge 26 ']'
  + echo '/etc/yum.conf is max,the lines total:26'
  /etc/yum.conf is max,the lines total:26
  # bash test24.sh
  Plz enter Two path:
  # bash test24.sh abc
  Plz enter Two path:
  # bash test24.sh abc cba
  abc not a common file!
  9、写一个脚本
  (1) 提示用户输入一个字符串;
  (2) 判断:
  如果输入的是quit,则退出脚本;
  否则,则显示其输入的字符串内容
  #!/bin/bash
  #
  read -p "Plz input a string:" -t 5 a
  if [ -z $a ]; then
  echo "Plz enter string!"
  exit 1
  fi
  if [[ $a == "quit" ]]; then
  exit 1
  else
  echo "$a"
  fi
  测试脚本:
  # bash test25.sh
  Plz input a string:abc
  abc
  # bash test25.sh
  Plz input a string:quit
  #
  # bash test25.sh
  Plz input a string:Plz enter string!
  写一个脚本,打印2^n表;n等于一个用户输入的值

页: [1]
查看完整版本: shell脚本作业