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

 

fi

更多例子在附件中!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章