uf123 发表于 2018-8-22 11:00:39

简单的shell编程

  学习这么几天了,现在吧我个人觉得还可以的shell脚本与大家分享:
  比较大小
  max.sh
  #!/bin/bash
  #
  [ $1 -gt $2 ] && echo "$1 is the max." ||echo "$2 is the max."
  shell运算:c=$[$a+$b]   echo $c
  ls.sh
  #!/bin/bash
  if [ -e /etc/issue ]; then
  ls -l/etc/issue
  fi
  user.sh
  #!/bin/bash
  if cut -d: -f1 /etc/passwd |grep "^$1$" &> /dev/null ; then
  echo "$1 is there."
  else
  echo "$1 is not there."
  fi
  file.sh
  #!/bin/bash
  if [ -e $1 ]; then
  if [ -f $1 ];then
  echo "$1 is a common file."
  elif "$1 is a directory."
  else
  echo "$1 is unkown."
  fi
  else
  echo "You fool,give me a correct file."
  exit 7
  fi
  sum.sh
  #!/bin/sh
  let SUM=0
  for I in $(seq 1 100); do
  let SUM+=$I
  done
  echo "The sum is: $SUM."
  ping.sh
  #!/bin/bash
  for I in {1..10}; do
  if ping -c1 -w2 192.168.0.$I &> /dev/null ; then
  echo "$I is online."
  else
  echo "$I is offline."
  fi
  done
  myfile.sh
  #!/bin/bash
  # cd /var
  for I in ./*; do
  file $I
  done
  sum2.sh
  #!/bin/bash
  echo "Please input an integer:"
  read A
  echo "Please input an integer:"
  read B
  echo "sum is $[$A+$B]."
  count.sh
  #!/bin/bash
  read -p "Please assign a file:" FILE
  let COUNT=0
  while read LINE; do
  let COUNT++
  done < $FILE
  echo $COUNT
  发送邮件
  post.sh
  #!/bin/bash
  #
  IS_REDHAT=`who |grep redhat`
  echo &quot;Please enter the file you want to mail to redhat:&quot;
  read FILE
  if [ -e $FILE -a -f $FILE ]; then
  until [ &quot;$IS_REDHAT&quot;
  do
  sleep 5
  IS_REDHAT=`who |grep redhat`
  done
  mail -s &quot;Welcome!&quot; redhat
页: [1]
查看完整版本: 简单的shell编程