shell 编程例子—第9章源码




++++++++++++++++++++++++++++++++++++++++++



#!/bin/bash

#this is a example

cd /root/txtfile/

echo "----------------" >> bac_ztg.txt

date >> bac_ztg.txt

cat ztg* >> bac_ztg.txt

echo "" >> bac_ztg.txt




++++++++++++++++++++++++++++++++++++++++++



#!/bin/bash

#this is a example for break

echo 请在命令后输入一个数字n,然后会得到1+2+...+n之和

if [ $# -ne 1 ]

then

    echo 请输入并且仅输入一个数字

    exit 1

fi

TOTAL=0

VAR=$1

while true

do

    TOTAL=$[TOTAL+VAR]

    VAR=$[VAR-1]

    if [ $VAR -eq 0 ]

    then

        break

    fi

done

echo $TOTAL




++++++++++++++++++++++++++++++++++++++++++



#!/bin/bash

#this is a example for case

cd /root/txtfile

echo please give your choice to display a file:

echo "1) display ztg1.txt"

echo "2) display ztg2.txt"


echo enter your choice:

read var


case $var in

    1) cat ztg1.txt;;

    2) cat ztg2.txt;;

    *) echo wrong;;

esac




++++++++++++++++++++++++++++++++++++++++++



#!/bin/bash

#this is a example for continue

echo 请在命令后输入一个数字n,然后会得到1-n之间奇数之和

if [ $# -ne 1 ]

then

    echo 请输入并且仅输入一个数字

    exit 1

fi

TOTAL=0

VAR=$1

while true

do

    if [ $VAR -eq 0 ]

    then

        break

    elif [ $[VAR%2] -eq 0 ]

    then

        VAR=$[VAR-1]

        continue

    elif true

    then

        TOTAL=$[TOTAL+VAR]

        VAR=$[VAR-1]

    fi

done

echo $TOTAL




++++++++++++++++++++++++++++++++++++++++++



#!/bin/bash

#this is a example for for

for num in 1 2 3 4 5 6

do

    echo $num的平方:

    expr $num \* $num

#   { echo $num的平方:;expr $num \* $num;}

done




++++++++++++++++++++++++++++++++++++++++++



#!/bin/bash

#this is a example for function

display()

{

    if [ $# -ne 1 ]

    then

        echo 请在命令后输入一个文件名或目录名

        exit 1

    fi

    if [ -d $1 ]

    then

        dir $1

    elif [ -f $1 ]

    then

        cat $1

    elif true

    then

        echo 没有该文件名或目录名

    fi

    echo 请在命令后输入一个文件名或目录名

}

display $1





++++++++++++++++++++++++++++++++++++++++++



#!/bin/bash

#this is a example for if

cd /root/txtfile/

if [ -f ztg1.txt ]

then

    echo ztg1.txt is a file:

    cat ztg1.txt

elif [ -d /root/txtfile ]

then

    echo in /root/txtfile is:

    dir /root/txtfile

fi




++++++++++++++++++++++++++++++++++++++++++



#!/bin/bash

#this is a example for shift

if [ $# -eq 0 ]

then

    echo no number!

    exit 1

fi


TOTAL=0


until [ $# -qe 0 ]

do

    TOTAL=expr $TOTAL + $1

    shift

done


echo $TOTAL



++++++++++++++++++++++++++++++++++++++++++



#!/bin/bash

#this is a example for shift_add

if [ $# -eq 0 ]

then

    echo no number!

    exit 1

fi

export TOTAL=0

until [ $# -eq 0 ]

do

    TOTAL=$[TOTAL+$1]

    shift

done

echo $TOTAL




++++++++++++++++++++++++++++++++++++++++++



#!/bin/bash

#this is a example for until

cd /root/txtfile

echo 请输入文件名:

read FNAME

echo 请输入文件内容,输入end!退出:

read VAR

until [ $VAR = end! ]

do

    echo $VAR >> $FNAME

    echo 请输入文件内容,输入end!退出:

    read VAR

done




++++++++++++++++++++++++++++++++++++++++++



#!/bin/bash

#this is a example for while

echo "请输入数字(大于等于100将退出):"

read VAR

while [ $VAR -lt 100 ]

do

    echo $VAR的平方:

    expr $VAR \* $VAR

#   { echo $VAR的平方:;expr $VAR \* $VAR;}

    echo "请输入数字(大于等于100将退出):"

    read VAR

done




++++++++++++++++++++++++++++++++++++++++++