shell脚本入门

1.按日期建立文件 echo 'auto touch 3 files ' read -p "please enter your filename : " file filename=${file:-"filename"} date1=`date --date='2 days ago' +%Y%m%d` date2=`date --date='1 days ago' +%Y%m%d` date3=`date +%Y%m%d` file1="$filename""$date1" file2="$filename""$date2" file3="$filename""$date3" touch $file1 touch $file2 touch $file3 2.查看文件的权限 read -p 'enter a file name: ' file test -z $file && echo 'you must input a filename'&&exit 0 test ! -e $file && echo 'the file not exist' && exit 0 test -f $file && filetype="regular file" test -d $file && filetype="directory" test -r $file && perm="readable" test -w $file && perm="$perm writable" test -x $file && perm="$perm excutable" echo "the filename : $file is a $filetype" echo "the permission of the file is : $perm" 3.if ... ; then elif else fi if [ "$c" == "Y" ] || [ "$c" == "y" ]; then echo "ok,continue" exit 0 elif [ "$c" == "N" ] || [ "$c" == "n" ]; then echo "interrupt" exit 0 else echo "I don't know your choice" fi 4.use prameters of script echo "The script name is : $0 " [ -n "$1" ] && echo "the first param is : $1" || exit 0 [ -n "$2" ] && echo "the second param is : $2" || exit 0 [ -n "$3" ] && echo "the third param is : $3" || exit 0 5.case .. esac case $1 in "hello") echo "Hello ,how are you ?" ;; "") echo "you must input parameters, ex > $0 someword" ;; *) echo "Use $0 {hello}" ;; esac 6.检查网络服务 testing=`netstat -tuln | grep ":80 "` if [ "$testing" != "" ]; then echo "www is running " fi testing=`netstat -tuln | grep ":22 "` if [ "$testing" != "" ]; then echo "ssh is running" fi testing=`netstat -tuln | grep ":21 "` if [ "$testing" != "" ]; then echo "ftp is running" fi testing=`netstat -tuln | grep ":25 "` if [ "$testing" != "" ]; then echo "mail is running" fi 7.计算时间 read -p "please input your demobilization date : " q date_d=`echo $q|grep '[0-9]\{8\}'` if [ "$date_d" == "" ]; then echo "you input wrong format of date" exit 0 fi declare -i time=`date --date="$q" +%s` declare -i now=`date +%s` declare -i wait=$(($time-$now)) day=$(($wait/3600/24)) if [ "$day" -lt "0" ]; then echo "your had been demobilization before " $((-1*$day)) " ago" else echo "you have $day days to leave" fi 8.函数 function print(){ echo "YOur choice is $1" } echo 'this program will print your selection@!' case $1 in "one") print $1 ;; "two") print $1 ;; "three") print $1 ;; *) echo 'Usage (one|two|three)' ;; esac 9.while循环 while [ "$y" != "yes" ] && [ "$y" != "YES" ] do read -p "Please input yes or YES to stop the program : " y done 10.until until [ "$y" == "yes" ] || [ "$y" == "YES" ] do read -p "Please input yes or YES to stop the program : " y done 11.for循环 s=0 for (( i=1; i<=100; i++ )) do s=$(($s+$i)) done echo "the result of 1+2+3+...+100 is ===> $s" another for: for animal in dog cat elephant do echo "there are $animal s.." done 12.路径检测 read -p "please input a dir : " dir if [ "$dir" == "" ] || [ ! -d "$dir" ]; then echo "the $dir not exists in your system" exit 1 fi list=`ls $dir` for file in $list do perm="" test -r "$dir/$file" && perm="$perm readable" test -w "$dir/$file" && perm="$perm writable" test -x "$dir/$file" && perm="$perm executable" echo "the permissions of the file $dir/$file is : $perm" done 13.文件检测 name="/tmp/logical" if [ ! -e "$name" ]; then echo 'not exist' touch "$name" elif [ -d "$name" ]; then echo 'dir' rm -r "$name" elif [ -f "$name" ]; then echo 'file' rm -r "$name" touch "$name" else echo "It's neither a dir nor a file" fi 14.文件分析 list=`cut -d ':' -f1 /etc/passwd` i=0 for user in $list do i=$(($i+1)) echo "The $i account is $user" done
posted @ 2012-08-03 22:02  X海阳  阅读(200)  评论(0编辑  收藏  举报