1.乙個計算自己行號的指令碼
#! /bin/sh
nl `basename $0`
cat -n `basename $0`
exit 0
2.按月償還貸款
#! /bin/sh
echo
echo "given the principal, interset rate, and term of a mortgage,"
echo "calculate the monthly payment."
bottom=1.0
echo
echo -n "enter principal (no commas) "
read principal
echo -n "enter interset rate (percent) "
read interest_r
echo -n "enter term (months) "
read term
interest_r=$(echo "scale=9; $interest_r/100.0" | bc) -----------scale=9定義小數點位數為9位
interest_rate=$(echo "scale=9; $interest_r/12 + 1.0" | bc)
top=$(echo "scale=9; $principal*$interest_rate^$term" | bc)
echo;echo "please be patient. this may take a while."
let "months = $term - 1"
for ((x=$months; x>0; x--))
dobot=$(echo "scale=9; $interest_rate^$x" | bc)
bottom=$(echo "scale=9; $bottom+$bot" | bc)
done
echo "scale=2; $top/$bottom" | bc
payment=$(echo "scale=2; $top/$bottom" | bc)
echo
echo "monthly payment = /$$payment"
echo
exit 0
3.使用"here document「來呼叫bc
#! /bin/sh
var1=`bc << eof
18.33 * 19.78
eof`
echo $var1
v1=23.53
v2=17.881
v3=83.501
v4=171.63
var2=$(bc << eof
scale=4
a = ( $v1 + $v2 )
b = ( $v3 + $v4 )
a * b + 15.35
eof)
echo $var2
var3=$(bc -l << eof
scale=9
s ( 1.7 )
eof)
echo $var3
hyp=
hypotenue ()
hypotenue 3.68 7.31
echo "hypotenuse = $hyp"
exit 0
4.計算圓周率
! /bin/sh
dimension=1000
maxshots=1000
pmultiplier=4.0
get_random () ')
random=$seed
let "rnum = $random % $dimension"
echo $rnum
}distance=
hypotenuse ()
shots=0
splashes=0
thuds=0
pi=0
while [ "$shots" -lt "$maxshots" ]
doxcoord=$(get_random)
ycoord=$(get_random)
hypotenuse $xcoord $ycoord
((shots++))
printf "#%4d " $shots
printf "xc = %4d " $xcoord
printf "yc = %4d " $ycoord
printf "distance = %5d " $distance
if [ "$distance" -le "$dimension" ]
then
echo -n "thud! "
((thuds++))
fipi=$(echo "scale=9; $pmultiplier*$splashes/$shots" | bc)
echo -n "pi ~ $pi"
echo
done
5.計算直角三角形的斜邊
#! /bin/sh
args=2
e_badargs=65
if [ $# -ne "$args" ]
then
echo "usage: `basename $0` side_1 side_2"
exit $e_badargs
fiawkscript=' ' -----長度7位,整數佔3位
echo -n "hypotenuse of $1 and $2 = "
echo $1 $2 | awk "$awkscript"
exit 0
shell程式設計題(十九)
題目 設計乙個shell程式,在 userdata目錄下建立50個目錄,即user1 user50,並設定每個目錄的許可權,其中其他使用者的許可權為 讀 檔案所有者的許可權為 讀 寫 執行 檔案所有者所在組的許可權為 讀 執行。答案 bin bash mkdir userdata if eq 0 t...
shell例項 十五 exec命令
1.乙個exec自身的指令碼 sleep 2 exec 0 echo this line will never echo exit 0 2.shopt允許shell在空閒時修改shell選項,經常出現在啟動檔案中。shop s cdspell 使用cd命令時,允許少量的錯誤 cd hpme 應該是 ...
sed命令例項 shell指令碼入門
什麼是 sed?sed是乙個很好的檔案處理工具,本身是乙個管道命令,主要以行為單位進行處理,可以將資料行進行替換 刪除 新增 選取等特定工作。sed 選項 命令 n,使用安靜 silent 模式。在一般 sed 的用法中,所有來自 stdin 的資料一般都會被列出到終端上。但如果加上 n 引數後,則...