shell脚本

批量创建随机数的文件

[root@VM-16-13-centos lianxi]# cat yeahzxw01.sh
#!/bin/bash

[ -d /yeahzxw ] || mkdir -p /yeahzxw

for i in `seq 10`
do
    touch /yeahzxw/`echo $RANDOM|md5sum | cut -c 1-8`_yeahzxw.html
done

批量修改文件名

[root@VM-16-13-centos lianxi]# cat yeahzxw02.sh
#!/bin/sh
cd /yeahzxw/
for i in `ls /yeahzxw`

do
    ONE=`ls /yeahzxw/${i}|awk -F '_' '{print $1}'`

    mv $i ${ONE}_zxw.HTML
done

从0加到100

第一种

[root@VM-16-13-centos learn]# cat dfor.sh
for((i=1;i<=100;i++))
do
  let sum+=i
done
echo $sum

第二种

[root@VM-16-13-centos learn]# cat dfor1.sh
for i in `seq 100`
  do
    echo $((sum+=i))
done

第三种

echo $((100*(100+1)/2))

ACTION

#!/bin/bash
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
echo '123' >/dev/null

if [ $? -eq 0 ];then
    action "YES" /bin/true
else
    action "NO" /bin/false
fi

数组

arr=(
`ls`
)
for i in ${arr[*]}
  do
    echo $i
done

变量

#!/bin/bash
read -t 10 -p "please input two num:" ONE TWO
echo "$ONE+$TWO = $(( $ONE + $TWO ))"
echo "$ONE-$TWO = $(( $ONE - $TWO ))"
echo "$ONE*$TWO = $(( $ONE * $TWO ))"
echo "$ONE/$TWO = $(( $ONE / $TWO ))"
echo "$ONE**$TWO = $(( $ONE ** $TWO ))"
echo "$ONE%$TWO = $(( $ONE % $TWO ))"

CACE

#!/bin/bash
usage(){
  echo "USAGE:$0{0|1|2|3|...}" NUM
  exit 1 
}
if [ $# -ne 1 ]
  then usage
fi
case "$1" in
  1)echo '1'
;;
  2)echo '2'
;;
  3)echo '3'
;;
  *)echo "usage"
esac

方法二:
#!/bin/sh
#caseTest
#to  test the method of case
USER=`whoami`

case $USER in    
    root) echo "You can do all the operations"
        ;;  
    yeahzxw) echo "You can do some operations"         
        ;;        
    *) echo "Sorry,you can not do anything"      
        ;;
esac

C语言FOR

#!/bin/bash
for((i=1;i<=5;i++))
  do
    echo ssh 10.0.0.$i
done

二:
for((i=1;i<=100;i++))
do
  let sum+=i 
done
echo $sum

三:
for i in `seq 100`
  do
    echo $((sum+=i))
done

funcation

#!/bin/bash
yeahzxw(){
  echo 'i am zxw'
}
yeahzxw $1

二:
#!/bin/bash
[ -f /etc/init.d/functions ]&& . /etc/init.d/functions
usage(){
  echo "USAGE:$0 URL"
  exit 1
}
RETVAL=0
CheakUrlStatus(){
  wget -T 10 --spider -t 2 $1 &>/dev/null
    RETVAL=$?
    if [ $RETVAL -eq 0 ]
      then action "$1 url" /bin/true
      else action "$1 url" /bin/false
    fi
    return $RETVAL
}
main(){
  if [  $# -ne 1 ]
    then usage
  fi
CheakUrlStatus $1
RETVAL=$?
return $RETVAL
}
main $*

IF

#!/bin/sh
#ifTest
#to show the method of if 
echo -e "Enter the first integer:\c"
read FIRST
echo -n "Enter the second integer:"
read SECOND
if [ "$FIRST" -gt "$SECOND" ]
then 
echo "$FIRST is greater than $SECOND"
elif [ "$FIRST" -lt "$SECOND" ]
then 
echo "$FIRST is less than $SECOND" 
else
echo "$FIRST is equal to $SECOND" 
fi

菜单选项

#!/bin/bash
echo '1.install lamp'
echo '2.install lnmp'
echo '3.exit'
read -p "pls enter three num:" num

[ $num -ne "1" -a $num -ne "2" -a $num -ne "3" ]&&{
echo "pls enter yes num, bye~"
}
[ $num -eq "1" ]&&{
echo "[install lamp]"||exit 1
}
[ $num -eq "2" ]&&{
echo "[install lnmp]"||exit 2 
}
[ $num -eq "3" ]&&{
echo 'exit' && exit 0
}

While

#!/bin/bash

while true
  do
    uptime
sleep 2
done

批量创建文件

#!/bin/bash

[ -d /yeahzxw ] || mkdir -p /yeahzxw

for i in `seq 10`
do
    touch /yeahzxw/`echo $RANDOM|md5sum | cut -c 1-8`_yeahzxw.html
done

遍历数组元素

方法一:
[root@backup logs]# for ((i=1;i<6;i++));do echo $i;done
1
2
3
4
5
方法二:
[root@backup logs]# for i in 1 2 3 4 5;do echo $i;done
1
2
3
4
5
方法三:
for ((i=0; i<${#dir[*]}; i++));do echo -e "${dir[$i]}\n";done
方法四:
for file in ${dir[@]};do echo $file;done

举例元素法

array=(red green blue yellow magenta)
或
array=(
yeahzxw1
yeahzxw2
yeahzxw3
)

跳板机脚本

[root@docker7 zxw]# cat jump_ser.sh
#!/bin/bash

function trapper(){
  trap '' INT QUIT TSTP TERM HUP
}

function menu(){
  cat <<-EOF
=============Host List============
+ 1)192.168.1.50     +
+ 2)192.168.1.51     +
+ 3)192.168.1.52     +
+ 4)exit       +
==================================
  EOF
}

function host(){
  case "$1" in
    1)
      ssh -p 22 $USER@192.168.1.50
      ;;
    2)
            ssh -p 22 $USER@192.168.1.51
            ;;
    3)
            ssh -p 22 $USER@192.168.1.52
            ;;
    4|*)
            exit
          esac
}

function main(){
  while true
    do
      trapper
      clear
      menu
      read -p "Please Input Num:" num
      host $num
  done

}
main

shell面试题

批量创建随记字母文件

[root@shell-110 scripts]# sh random.sh 
[root@shell-110 scripts]# ll /yeahzxw/
total 0
-rw-r--r--. 1 root root 0 Apr  1 14:53 cbicfcbeag_yeahzxw.html
-rw-r--r--. 1 root root 0 Apr  1 14:53 cffacbgcgf_yeahzxw.html
-rw-r--r--. 1 root root 0 Apr  1 14:53 daaaededcf_yeahzxw.html
-rw-r--r--. 1 root root 0 Apr  1 14:53 dabdeaefgd_yeahzxw.html
-rw-r--r--. 1 root root 0 Apr  1 14:53 gbdghdfcfd_yeahzxw.html
-rw-r--r--. 1 root root 0 Apr  1 14:53 hehedediaa_yeahzxw.html
-rw-r--r--. 1 root root 0 Apr  1 14:53 iaigcefiii_yeahzxw.html
-rw-r--r--. 1 root root 0 Apr  1 14:53 ibdifhgdif_yeahzxw.html
-rw-r--r--. 1 root root 0 Apr  1 14:53 idbidagdah_yeahzxw.html
-rw-r--r--. 1 root root 0 Apr  1 14:53 ieifecebhh_yeahzxw.html
[root@shell-110 scripts]# cat random.sh 
#!/bin/bash

dir=/yeahzxw
mkdir -p $dir  && cd /yeahzxw

for i in {1..10}
  do
  char=`uuidgen |cut -c 27-|tr "0-9" "a-i"`
    touch ${char}yeahzxw.html 
  done

批量修改后缀名

[root@shell-110 scripts]# ll /yeahzxw/
total 0
-rw-r--r--. 1 root root 0 Apr  1 14:59 aabdbcbcci_yeahzxw.html
-rw-r--r--. 1 root root 0 Apr  1 14:59 ahefaebadd_yeahzxw.html
-rw-r--r--. 1 root root 0 Apr  1 14:59 ajedfeeaca_yeahzxw.html
-rw-r--r--. 1 root root 0 Apr  1 14:59 babehaegde_yeahzxw.html
-rw-r--r--. 1 root root 0 Apr  1 14:59 bcgfedhdad_yeahzxw.html
-rw-r--r--. 1 root root 0 Apr  1 14:59 cfidehejbi_yeahzxw.html
-rw-r--r--. 1 root root 0 Apr  1 14:59 deeebjdecb_yeahzxw.html
-rw-r--r--. 1 root root 0 Apr  1 14:59 didacdfdef_yeahzxw.html
-rw-r--r--. 1 root root 0 Apr  1 14:59 gjghhfifec_yeahzxw.html
-rw-r--r--. 1 root root 0 Apr  1 14:59 hgdcfcdghi_yeahzxw.html
[root@shell-110 scripts]# sh 2.sh 
[root@shell-110 scripts]# ll /yeahzxw/
total 0
-rw-r--r--. 1 root root 0 Apr  1 14:59 aabdbcbcci__zxwgirl.HTML
-rw-r--r--. 1 root root 0 Apr  1 14:59 ahefaebadd__zxwgirl.HTML
-rw-r--r--. 1 root root 0 Apr  1 14:59 ajedfeeaca__zxwgirl.HTML
-rw-r--r--. 1 root root 0 Apr  1 14:59 babehaegde__zxwgirl.HTML
-rw-r--r--. 1 root root 0 Apr  1 14:59 bcgfedhdad__zxwgirl.HTML
-rw-r--r--. 1 root root 0 Apr  1 14:59 cfidehejbi__zxwgirl.HTML
-rw-r--r--. 1 root root 0 Apr  1 14:59 deeebjdecb__zxwgirl.HTML
-rw-r--r--. 1 root root 0 Apr  1 14:59 didacdfdef__zxwgirl.HTML
-rw-r--r--. 1 root root 0 Apr  1 14:59 gjghhfifec__zxwgirl.HTML
-rw-r--r--. 1 root root 0 Apr  1 14:59 hgdcfcdghi__zxwgirl.HTML
[root@shell-110 scripts]# cat 2.sh 
#!/bin/bash
dir=/yeahzxw
[ -d $dir ] || exit 1

cd $dir
for i in `ls`
  do
    char=`echo $i|cut -c -11`
    mv $i ${char}_zxwgirl.HTML
  done

批量创建用户

#!/bin/bash

for i in {01..10}

  do 
    useradd yeahzxw$1
    pass=`echo $RANDOM|md5sum|cut -c 1-10`    
    echo "yeahzxw${i}:$pass" >>/tmp/userlist
    echo $pass|passwd --stdin yeahzxw${i}
  done

查看网段内IP

nmap 10.0.0.0/24

nmap -sn 10.0.0.0/24

#!/bin/bash
for i in 10.0.0.{1..254}
  do 
    ping -c 1 $i &>/dev/null &&\
    [ $? -eq 0 ] && echo "$i is ok!" &
  done

封IP通过防火墙

[root@shell-110 scripts]# vim fengIP.sh 
#!/bin/sh
logFile=/var/log/log.log
Ip=`awk '{a[$1]++}END{for(i in a)if(a[i]>0){print i} }' $logFile`

for i in $Ip

  do
    if [ `iptables -nL|grep $i|wc -l` -eq 0 ]
       then
         iptables -I INPUT -s $i -j DROP
    fi
  done

分库备份数据库

#!/bin/sh
user=root
passwd=yeahzxw
dataBase=`mysql -uroot -pyeahzxw -e "show databases;" 2>/dev/null |egrep -v "${exDataBase}"`
exDataBase="*_schema|Database"
Time=`date +%F`
for i in  $dataBase
  do
   mysqldump -u$user -p$passwd -F --single-transaction --triggers -B $i 2>/dev/null|gzip >/tmp/$i-$Time.sql.gz
  done

分库分表备份

#!/bin/sh
user=root
passwd=yeahzxw
dataBase=`mysql -uroot -pyeahzxw -e "show databases;" 2>/dev/null |egrep -v "${exDataBase}"`
exDataBase="*_schema|Database"
Time=`date +%F`
backupDir=/data/backup
for i in  $dataBase
  do
   tableslist= `mysql -u$user -p$passwd -e " use $i;show tables" 2>/dev/null|egrep -v "Tables_in"`
      for t in $tablelist
        do
          mysqldump -u$user -p$passwd -F --single-transaction -R $i $t 2>/dev/null|gzip > $backDir/$i/$i-$t-${Time}.sql.gz
    done
  done

循环打印母数不大于6的单词

#!/bin/sh

word=" I am yeahzxw teacher welcome to yeahzxw trainingclass"

for i in $word
  do
    [ ${#i} -le 6 ] && echo $i
  done

主从复制表库备份

[root@shell-110 scripts]# cat status.sh
#!/bin/bash

logFile=/server/scripts/shujuku.txt
tmpFile=/server/log/tmp.log
Flag=0

function Send(){  
     echo "Slave_IO_Running:$1 Slave_SQL_Running:$2 Seconds_Behind_Master:$3" >>$tmpFile

               }

function IO()  { 
  IO_status=`awk -F '[: ]+' '/Slave_IO_Running/ {print $3}' $logFile`
  [ "$IO_status" == "YES" ] ||\
  Flag=1

               }

function SQL() {  
  SQL_status=`awk -F '[: ]+' '/Slave_SQL_Running/ {print $3}' $logFile`
  [ "$SQL_status" == "YES" ] ||\
  Flag=1 

               }
function seconds(){  
  sec_status=`awk -F '[: ]+' '/Seconds_Behind_Master/ {print $3}' $logFile`
  [ "$sec_status" == "0" ] ||\
  Flag=1

               }

function main(){ 
  while true
    do 
      Flag=0
      IO
      SQL
      seconds
      [ $Flag -eq 1 ] && Send $IO_status $SQL_status $sec_status
      sleep 5
    done

               }
    main

菜单选项

[root@shell-110 scripts]# cat 11.sh 
#!bin/bash
function menu(){ 
  cat <<-EOF
   1.[ installing lnmp ]
   2.[ installing lamp ]
   3.[ exit ]
  EOF
 }
function main(){ 
  menu
  read -p "pls input the num you want:" choose
  case $choose in
  1)
    lnmp=/server/scripts/lnmp.sh
    [ -f $lnmp ] && sh $lnmp
    ;;
  2)
    lamp=/server/scripts/lamp.sh
    [ -f $lamp ] && sh $lamp
    ;;
  3)
    exit
    ;;
  *)
  echo "input error!" && exit 1
    ;;
  esac
 }
main

测试:

[root@shell-110 scripts]# sh 11.sh 
  1.[ installing lnmp ]
  2.[ installing lamp ]
  3.[ exit ]
pls input the num you want:1
start installing lnmp.
[root@shell-110 scripts]# sh 11.sh 
  1.[ installing lnmp ]
  2.[ installing lamp ]
  3.[ exit ]
pls input the num you want:2
start installing lamp.
[root@shell-110 scripts]# sh 11.sh 
  1.[ installing lnmp ]
  2.[ installing lamp ]
  3.[ exit ]
pls input the num you want:3
[root@shell-110 scripts]# sh 11.sh 
  1.[ installing lnmp ]
  2.[ installing lamp ]
  3.[ exit ]
pls input the num you want:4
input error!

[root@backup scripts]# head lamp.sh lnmp.sh 
==> lamp.sh <==
echo start installing lamp.

==> lnmp.sh <==
echo start installing lnmp.

memcached服务是否正常监控

[root@shell-110 scripts]# cat 14.sh 
#!/bin/bash
PORT=11211
IP=127.0.0.1
CMD="nc $IP $PORT"
InsertNum=`echo $RANDOM|md5sum|cut -c 2-11`
SQL1="set key008 0 0 10\r\n${InsertNum}\r\n"
SQL2="get key008\r\n"

function ok(){ 
  echo "memcached server is good"
  exit 0

  }
function err(){ 
  echo "memcached server is bad"
  exit 1

 }
#update num
printf "$SQL1"|${CMD} >/dev/null 2>&1
if 
  [ $? -ne 0 ]
    then 
      err
fi
#select num
SelectNum=`printf "$SQL2"|${CMD}|sed -n '2p'|tr -d "\r"`
if
  [ -z "$SelectNum" ]
    then
    err
elif [ "$InsertNum" = "$SelectNum" ]
  then
    ok
else
    err
fi

监控文件是否被篡改

执行脚本

[root@shell-110 scripts]# cat 15.sh 
#!/bin/bash
WebSite=/var/www/html/
path=/server/scripts
Md5sumDb=$path/md5sum.db
DiffDb=$path/diff.db
DiffNdb=$path/new_diff.db
CheckLog=/tmp/check.log

#Judge some dir or files is exist?
[ ! -d "$WebSite" ] &&{ 
  echo "this is a joke!"
  exit 1
                      }

[ ! -d "$path" ] && mkdir -p $path

#Defined Check function
function check(){ 
  find $WebSite -type f >$DiffNdb
  md5sum -c $Md5sumDb 2>/dev/null|grep FAILED >>$CheckLog
  diff $DiffDb $DiffNdb >>$CheckLog 2>&1
                }
#Defined main function
function main(){ 
  while true
   do 
     check
     [ -s "$CheckLog" ] && \
     mail -s "site is cuanggai $(date +%F)" 1362336072@qq.com <$CheckLog
     sleep 5
  done
               }
main
[root@shell-110 scripts]#

rsync启动

#!/bin/bash
# chkconfig: 2345 64 36
# description: manager rsyncd server.
 
# Warning: This script uses the /etc/init.d/functions system function. 
#          System  kernel version is CentOS6.6,2.6.32-504.el6.x86_64.
#          I'm not sure whether other systems can be used normally.
# Source function library.
. /etc/init.d/functions
rsync=/usr/bin/rsync
prog=rsyncd
pidfile=${PIDFILE-/var/run/rsyncd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/rsyncd}
RsyncConf="/etc/rsyncd.conf"
RETVAL=0
Usage(){
    $SETCOLOR_WARNING 
    echo -n "Rsync Daemon Program Need Configuration File,like /etc/rsyncd.conf"
    $SETCOLOR_NORMAL
    echo
    exit 6
}
start(){
    [ -x $rsync ] || exit 5
    [ -f $RsyncConf ] || Usage
        echo -n $"Starting $prog: "
        daemon --pidfile=${pidfile} $rsync --daemon
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}
stop(){
    echo -n $"Stopping $prog: "
    killproc $prog
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f ${lockfile}
    return $retval
}
restart() {
    stop
    sleep 1
    start
}
rh_status() {
    status $prog
}
rh_status_q() {
    rh_status >/dev/null 2>&1
}
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    status)
        rh_status
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart}"
        exit 2
esac

pojie-RANDOM密码

测试
[root@shell-110 scripts]# egrep "b90adb3d|ff9b59b6|00e464fb" md5.db 
6697:ff9b59b6ba6558e8bf84ad3a487f8ba0  -
11154:b90adb3d9202970cae287dcf82d80aec  -
18692:00e464fb49070a686c95fe8d269a5417  -
[root@shell-110 scripts]# echo 6697|md5sum
ff9b59b6ba6558e8bf84ad3a487f8ba0  -

脚本
[root@shell-110 scripts]# cat 18-2.sh 
#!/bin/bash
mimadb=./md5.db
for i in {0..32767}
  do 
    echo "$i:`echo $i|md5sum`" >>$mimadb
done

监控网站是否正常

[root@shell-110 scripts]# cat 19.sh 
#!/bin/bash
. /etc/init.d/functions
url=/server/scripts/url.txt
array=(`cat $url`)
function Check_Url(){ 
curl --o /dev/null -s $1 -w "%(http_code)\n" &>/dev/null && return 0||return 1
 }
function main(){ 
while true
  do 
    for i in ${array[*]}
      do
        Check_Url $i
        if [ $? -eq 0 ]
          then
            action "$i" /bin/true
         else
            action "$i" /bin/false
        fi
     done
       sleep 10
   done
 }
main

测试
[root@shell-110 scripts]# sh 19.sh 
http://www.baidu.com                                       [  OK  ]
http://www.taobao.com                                      [  OK  ]
http://www.jd.com                                          [  OK  ]
http://www.yeahzxw.com                                     [  OK  ]

nginx七种状态

#!/bin/bash
NGINX_PORT=8000   #IP可根据实际情况更改
NGINX_COMMAND=$1
nginx_active(){
/usr/bin/curl -s "http://127.0.0.1:
"$NGINX_PORT"/nginx_status/" |awk '/Active/ {print $NF}'
}
nginx_reading(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Reading/ {print $2}'
}
nginx_writing(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Writing/ {print $4}'
       }
nginx_waiting(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Waiting/ {print $6}'
       }
nginx_accepts(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $1}'
       }
nginx_handled(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $2}'
       }
nginx_requests(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $3}'
       }
  case $NGINX_COMMAND in
 active)
  nginx_active;
  ;;
 reading)
  nginx_reading;
  ;;
 writing)
  nginx_writing;
  ;;
 waiting)
  nginx_waiting;
  ;;
 accepts)
  nginx_accepts;
  ;;
 handled)
  nginx_handled;
  ;;
 requests)
  nginx_requests;
  ;;
        *)
  echo $"USAGE:$0 {active|reading|writing|waiting|accepts|handled|requests}"
    esac


排序

解答:
awk -F "[,. ]" '{for(i=1;i<=NF;i++)array[$i]++}END{for(key in array)print array[key],key|"sort -nr"}' yeahzxw.txt|column -t

tr "[ ,.]" "\n"<yeahzxw.txt|grep -v "^$"|sort|uniq -c|sort -rn

tr "{ |,|.}" "\n"<yeahzxw.txt|awk -F ""  '{for(i=1;i<=NF;i++)array[$i]++}END{for(key in array)print array[key],key|"sort -nr"}'

tr "[ ,.]" "\n"<yeahzxw.txt|awk '{for(i=1; i<=length($0); i++) ++S[substr($0,i,1)]} END {for(a in S) print S[a], a|"sort -rn"}'

echo "the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support sections for more infomation"|sed 's# ##g'|sed -r 's#(.)#\1\n#g'|sort|uniq -c|sort -rn -k1

echo "the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support sections for more infomation"|sed 's# ##g'|awk -F "" '{for(n=1;n<=NF;n++) print $n}'|sort|uniq -c|sort -k1 -nr

编写一个正方形

[root@shell-110 scripts]# sh zheng.sh 
Please Enter a number:5
■■■■■
■■■■■
■■■■■
■■■■■
■■■■■
[root@shell-110 scripts]# cat zheng.sh 
#!/bin.bash
read -p "Please Enter a number:" Line

for x in `seq $Line`
  do 
    for y in `seq $Line`
      do 
        echo -n "■"
      done
   echo 
done

编写杨辉三角

[root@shell-110 scripts]# cat test4.sh 
#!/usr/bin/python
# coding:utf-8
n = -1
while n < 0:
   n = raw_input("Enter a nonnegative integer >= 0:")
   try:
       n = int(n)
   except:
       print "please enter integer!"
       n = -1
tmppascal = [0,]* (2 * n + 3)
pascal = []
for i in range(n + 2):
   pascal += [tmppascal[:],]
center = n + 1
pascal[1][center] = 1
x = y = 0
for i in range(2, n + 2):    
   if n % 2 == 1:
       if i % 2 == 0:
           x = 1
           y = 2 * n + 2
       else:
           x = 2
           y = 2 * n + 1
   else:
       if i % 2 == 1:
           x = 1
           y = 2 * n + 2
       else:
           x = 2
           y = 2 * n + 1
   for j in range(x, y, 2):
       pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j + 1]
if n % 2 == 0:
   max = pascal[n + 1][center]
else:
   max = pascal[n + 1][center - 1]
maxlen = len(str(max))
for row in pascal[1:]:
   for col in row[1:-1]:
       if col == 0:
           col = ""
       fm = '{0: ^' + str(maxlen) + '}'
       print fm.format(col),
   print
[root@shell-110 scripts]#

测试

[root@shell-110 scripts]# python test4.sh 
Enter a nonnegative integer >= 0:5
               1                
            1     1             
         1     2     1          
      1     3     3     1       
   1     4     6     4     1    
1     5     10    10    5     1

编写一个梯形

[root@backup ~]# vim 27.sh

#!/bin/sh
if [ $# -ne 2 ];then
    echo $"USAGE:$0 num1(>2) num2"
    exit 1
fi
for n in `seq $1 $2`
do
    for((m=1;m<=$n;m++))
    do
        echo -n "*"
    done
    echo
done