自己写了一下小的shell实例,虽然很小,但所有的大的程序都是由小的模块堆积起来的,程序员一定要懂得一种脚本的书写,而我,只会在linux下工作,所以就只能写linux的shell脚本了,呵呵,本文会陆续更新,给自己加油!
1.模拟linnux登录shell
#/bin/bash echo -n "login:" read name echo -n "password:" read passwd if [ $name = "cht" -a $passwd = "abc" ];then echo "the host and password is right!" else echo "input is error!" fi
#/bin/bash echo "please enter two number" read a read b if test $a -eq $b then echo "NO.1 = NO.2" elif test $a -gt $b then echo "NO.1 > NO.2" else echo "NO.1 < NO.2" fi
#/bin/bash echo "enter a file name:" read a if test -e /root/$a then echo "the file is exist!" else echo "the file is not exist!" fi
#/bin/bash clear for num in 1 2 3 4 5 6 7 8 9 10 do echo "$num" done
#/bin/bash echo "Please enter a user:" read a b=$(whoami) if test $a = $b then echo "the user is running." else echo "the user is not running." fi
#/bin/bash for filename in `ls` do if test -d $filename then b=0 else a=$(ls -l $filename | awk '{ print $5 }') if test $a -eq 0 then rm $filename fi fi done
#/bin/bash while line=`ls /export/um_lpp_source` do if test $line="" then echo "NULL" sleep 1 else echo $line chfs -a size=3G /export/um_lpp_source exit 0 fi done
#/bin/bash for i in 1 2 3 4 5 6 7 8 9 do echo "the number of $i computer is " ping -c 1 192.168.0.$i done
#/bin/sh a=2 while name="test.log" do sleep 1 b=$(ls -l $name | awk '{print $5}') if test $b -ge $a #then echo "OK" then `cp /opt/*.tar.gz .` exit 0 fi done
#/bin/bash while read name do echo $name done
#/bin/bash while read line do echo $line done < 0.sh
#/bin/bash test -e a.c while read line do a=$(($line+1)) done < a.c echo $a
#/bin/bash p () { echo "hello" } p
#/bin/bash p_num () { num=$1 echo $num } for n in $@ do p_num $n done
#/bin/bash while : do echo "please input file's name:" read a if test -e /root/$a then echo "the file is existing Please input new file name:" else mkdir $a echo "you aye sussesful!" break fi done
#/bin/bash ifconfig | grep "inet addr:" | awk '{ print $2 }'| sed 's/addr://g'
#/bin/bash a=0 for name in *.* do b=$(ls -l $name | awk '{print $5}') if test $b -ge $a then a=$b namemax=$name fi done echo "the max file is $namemax"
#/bin/bash a=1 while : do a=$(($a+1)) if test $a -gt 255 then break else echo $(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g') ip=$(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g') echo $ip >> ip.txt fi done
#/bin/bash echo "Current User is :" echo $(ps | grep "$$" | awk '{print $2}')
#!/bin/bash clear echo "enter a number from 1 to 5:" read num case $num in 1) echo "you enter 1" ;; 2) echo "you enter 2" ;; 3) echo "you enter 3" ;; 4) echo "you enter 4" ;; 5) echo "you enter 5" ;; *) echo "error" ;; esac
#!/bin/bash clear echo "enter [y/n]:" read a case $a in y|Y|Yes|YES) echo "you enter $a" ;; n|N|NO|no) echo "you enter $a" ;; *) echo "error" ;; esac
22.内置命令的使用
#/bin/bashclear echo "Hello, $USER" echo echo "Today 's date id `date`"
echo
echo "the user is :" who echo
echo "this is `uname -s`" echo
echo "that's all folks! "
23.打印无密码用户
#/bin/bash echo "No Password User are :" echo $(cat /etc/shadow | grep "!!" | awk 'BEGIN { FS=":" }{print $1}')
#/bin/bashclear echo "Hello, $USER" echo echo "Today 's date id `date`"
echo
echo "the user is :" who echo
echo "this is `uname -s`" echo
echo "that's all folks! "
25.检查端口号是否已启动
#!/bin/bash n=1 echo "检查xxx服务..." while true do if test $n -gt 20 then echo "xxx服务启动失败" break fi sleep 5 n=$(($n+1)) port=`netstat -antp | grep "0.0.0.0:8080"` if [ ${#port} -gt 3 ]; then echo "xxx服务已经启动" break; fi done