首页 / 操作系统 / Linux / Linux bash shell.sh编程实例
1.输出#!/bin/bashecho "Please type your number:"read afor ((i=1;i<=a;i++))dofor ((p=1;p<=i;p++))doecho -n " $p "doneechodoneecho
2.九九乘法表#!/bin/bashfor ((i=1;i<10;i++))dofor ((p=1;p<=i;p++))doecho -ne " $p x $i= "`expr $i * $p`doneechodoneecho
3.计算器#!/bin/bashs=0while trueecho "..............+"echo "..............-"echo "..............x"echo "............../"echo "..............q" echo "Please type your word:(e.g.1 + 2)"read a b cdocase $b in+)let s=a+cecho " $a + $c =" $s;;-)let s=a-cecho " $a - $c =" $s;;x)let s=a*cecho " $a x $c =" $s;;/)let s=a/cecho " $a / $c =" $s;;esac case $a inq) break ;;
esacdone
4.菱形#!/bin/bashecho "Please type a number:"read numfor ((i=1;i<=num;i++))dofor ((j=0;j<num-i;j++))doecho -n " "donefor ((j=0;j<2*i-1;j++))doecho -n "*"doneecho ""donefor ((i=1;i<=num;i++))dofor ((j=0;j<i;j++))doecho -n " "donefor ((j=0;j<2*(num-i)-1;j++))doecho -n "*"doneecho ""done
5. 输出当前目录下所有文件,并输出文件总数和目录总数#!/bin/bashls -alfilenum=0dirnum=0for q in `ls -a`doif [ -d $q ]thendirnum=`expr $dirnum + 1`fifilenum=`expr $filenum + 1`doneecho "The number of dirctary is $dirnum"echo "The number of file is $filenum"
6.菜单#!/bin/bashwhile trueecho "List Directory..........l "echo "Change Directory........c "echo "Edit File...............e "echo "Remove File.............r "echo "Exit Menu...............q "read chdocase $ch inl) ls;;c) echo Enter target directoryread direccd "$direc" ;;
e) echo Enter file nameread filevi $file ;;
r) echo Enter file nameread filerm $file ;;
q|Q) echo Goodbyebreak;;
*) echo illegal Optionesacdone