linux bash shell.sh编程实例

linux bash shell.sh编程实例 (2009-04-25 14:20:05)

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

 

7. ./shuru a b c d e f

#!/bin/bash

for (( b=$#; b>0; b-- ))

do

echo $*;

shift;

done

 

8.电话本

#!/bin/bash

if [ ! -f /root/minda ]

then

touch /root/minda

fi

while true

echo "If you want to add a user plese type----------------------a"

echo "If you want to del a user plese type----------------------d"

echo "If you want to watch all users please type----------------l"

echo "If you want to bianji users please tyye-------------------e"

echo "If you want to exit please type---------------------------q"

read b

do

case $b in

a) echo "Please type your name :"

read name

echo "Please type your number :"

read number

echo $name $number >> /root/minda;;


d) echo "Please type which name do you want to del:"

read name

cat /root/minda|grep ^[^$name]> /root/minda;;


l) cat /root/minda;;


e) echo "Please type which name do you want to del:"

read name

cat /root/minda|grep ^[^$name]> /root/minda

echo "Please type your new name :"

read name

echo "Please type your new number :"

read number

echo $name $number >> /root/minda;;


q) exit;;

esac

done

 

9.判断当前用户的类型

#!/bin/bash

case $UID in

0) echo this is root;;

[0-9][0-9]) echo this is system;;

[1-4][0-9][0-9]) echo this is service;;


*) echo this is public;;

esac

 

10.计算1-100的和

#!/bin/bash

b=0

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

do

b=`expr $b + $a `

done

echo b= $b

 

11.用一个文件记录系统中所有rpm包名,如果文件不存在则创建,如果存在则显示文件内容。

#!/bin/bash

a=/root/file

if [ -f $a ]

then

cat $a

else

rpm -qa > $a

fi

 

12.用户登陆

#!/bin/bash

n=1

while [ $n -gt 0 ]

do

echo "xinjian------------------1"

echo "denglu-------------------2"

echo "exit---------------------3"

read n;

case $n in

1) echo "username"

read name

if [ -f $name ]

then

echo "same username"

else

touch /root/$name

echo "passwd"

read passwd

echo $passwd >> /root/$name

fi;;


2) echo "username"

read name1

if [ -f $name1 ]

then

echo "passwd"

else

echo "yonghu wrong"

fi

read passwd1

if [ $passwd1 -eq `cat /root/$name1` ]

then

echo "success"

break

else

echo "passwd wrong"

fi;;


3) break;;

esac

done

 

13.创建四个文件test1,test2,test3,test4,实现自动创建dir1,dir2,dir3,dir4四个目录,并将test1,test2,test3,test4四个文件分别拷贝到dir1,dir2,dir3,dir4相应的目录下。

#!/bin/bash

for a in 1 2 3 4

do

touch test$a

mkdir dir$a

cp test$a dir$a

done

 

14.输入任意数求和./a.out 1 2 3 4 5 6

#!/bin/bash

s=0

for i in $*

do

s=`expr $s + $i`

done

echo $s

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值