天天看點

簡單的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

if [ -e /etc/issue ]; then

    ls -l  /etc/issue

fi

user.sh

if cut -d: -f1 /etc/passwd |grep "^$1$" &> /dev/null ; then

  echo "$1 is there."

 else

  echo "$1 is not there."

file.sh

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

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

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

myfile.sh

# cd /var

for I in ./*; do

  file $I

sum2.sh

echo "Please input an integer:"

read A

read B

echo "sum is $[$A+$B]."

count.sh

read -p "Please assign a file:" FILE

let COUNT=0

while read LINE; do

   let COUNT++

done < $FILE

echo $COUNT

發送郵件

post.sh

IS_REDHAT=`who |grep redhat`

echo "Please enter the file you want to mail to redhat:"

read FILE

if [ -e $FILE -a -f $FILE ]; then

  until [ "$IS_REDHAT"

  do

    sleep 5

    IS_REDHAT=`who |grep redhat`

  done

  mail -s "Welcome!" redhat <$FILE

繼續閱讀