Linux bash shell.sh编程实例

1.输出

#!/bin/bash

echo "Please type your number:"

read a

for ((i=1;i<=a;i++))

do

for ((p=1;p<=i;p++))

do

echo -n " $p "

done

echo

done

echo


2.九九乘法表

#!/bin/bash

for ((i=1;i<10;i++))

do

for ((p=1;p<=i;p++))

do

echo -ne " $p x $i= "`expr $i \* $p`

done

echo

done

echo


3.计算器

#!/bin/bash

s=0

while true

echo "..............+"

echo "..............-"

echo "..............x"

echo "............../"

echo "..............q"

echo "Please type your word:(e.g.1 + 2)"

read a b c

do

case $b in

+)

let s=a+c

echo " $a + $c =" $s;;

-)

let s=a-c

echo " $a - $c =" $s;;

\x)

let s=a*c

echo " $a x $c =" $s;;

\/)

let s=a/c

echo " $a / $c =" $s;;

esac

case $a in

q) break ;;


esac

done


4.菱形

#!/bin/bash

echo "Please type a number:"

read num

for ((i=1;i<=num;i++))

do

for ((j=0;j<num-i;j++))

do

echo -n " "

done

for ((j=0;j<2*i-1;j++))

do

echo -n "*"

done

echo ""

done

for ((i=1;i<=num;i++))

do

for ((j=0;j<i;j++))

do

echo -n " "

done

for ((j=0;j<2*(num-i)-1;j++))

do

echo -n "*"

done

echo ""

done


5. 输出当前目录下所有文件,并输出文件总数和目录总数

#!/bin/bash

ls -al

filenum=0

dirnum=0

for q in `ls -a`

do

if [ -d $q ]

then

dirnum=`expr $dirnum + 1`

fi

filenum=`expr $filenum + 1`

done

echo "The number of dirctary is $dirnum"

echo "The number of file is $filenum"


6.菜单

#!/bin/bash

while true

echo "List Directory..........l "

echo "Change Directory........c "

echo "Edit File...............e "

echo "Remove File.............r "

echo "Exit Menu...............q "

read ch

do

case $ch in

l) ls;;

c) echo Enter target directory

read direc

cd "$direc" ;;


e) echo Enter file name

read file

vi $file ;;


r) echo Enter file name

read file

rm $file ;;


q|Q) echo Goodbye

break;;


*) echo illegal Option

esac

done

相关推荐