shell 程式設計例子 第9章原始碼

2021-07-27 15:46:51 字數 3588 閱讀 8218

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

fitotal=0

var=$1

while true

dototal=$[total+var]

var=$[var-1]

if [ $var -eq 0 ]

then

break

fidone

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

fitotal=0

var=$1

while true

doif [ $var -eq 0 ]

then

break

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

then

var=$[var-1]

continue

elif true

then

total=$[total+var]

var=$[var-1]

fidone

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

#  

done

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

#!/bin/bash

#this is a example for function

display()

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

fitotal=0

until [ $# -qe 0 ]

dototal=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

fiexport total=0

until [ $# -eq 0 ]

dototal=$[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! ]

doecho $var >> $fname

echo 請輸入檔案內容,輸入end!退出:

read var

done

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

#!/bin/bash

#this is a example for while

echo "請輸入數字(大於等於100將退出):"

read var

while [ $var -lt 100 ]

doecho $var的平方:

expr $var \* $var

#  

echo "請輸入數字(大於等於100將退出):"

read var

done

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

0

給主人留下些什麼吧!~~

shell 程式設計例子 第9章原始碼

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...

shell 程式設計例子 第9章原始碼

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...

第9章 程式設計工作的簡化

第9章 程式設計工作的簡化 1 客戶端的簡化 可使用智慧型介面指標和c 包裝類。2 智慧型介面指標 像常規介面指標一樣訪問元件,將引用計數等細節隱藏起來,並且當程式的執行離開了智慧型介面指標的作用於之後,相應的介面將被釋放掉。不適合封裝某個介面或介面集。智慧型介面指標同元件的聚合類似。3 c 包裝類...