15个精典SHELL编程例子
- 1.输出 1
- 1 2
- 1 2 3
- 1 #!/bin/bash
- 2 echo "Please type your number:"
- 3 read a
- 4 for ((i=1;i<=a;i++))
- 5 do
- 6 for ((p=1;p<=i;p++))
- 7 do
- 8 echo -n " $p "
- 9 done
- 10 echo
- 11 done
- 12 echo
- 2.九九乘法表
- 1 #!/bin/bash
- 2 for ((i=1;i<10;i++))
- 3 do
- 4 for ((p=1;p<=i;p++))
- 5 do
- 6 echo -ne " $p x $i= "`expr $i \* $p`
- 7 done
- 8 echo
- 9 done
- 10 echo
- 3.计算器
- 1 #!/bin/bash
- 2 while true
- 3 echo "..............+"
- 4 echo "..............-"
- 5 echo "..............x"
- 6 echo "............../"
- 7 echo "..............q"
- 8
- 9 echo "Please type your word:(e.g.1 + 2)"
- 10 read a b c
- 11
- 12 do
- 13 case $b in
- 14 +) echo " $a + $c =" `expr $a + $c`;;
- 15 -) echo " $a - $c =" `expr $a - $c`;;
- 16 x) echo " $a x $c =" `expr $a \* $c`;;
- 17 /) echo " $a / $c =" `expr $a \/ $c`;;
- 18 esac
- 19
- 20 case $a in
- 21 q) break ;;
- 22
- 23 esac
- 24 done
- 4.菱形
- 1 #!/bin/bash
- 2 echo "Please type a number:"
- 3 read num
- 4 for ((i=1;i<=num;i++))
- 5 do
- 6 for ((j=0;j<num-i;j++))
- 7 do
- 8 echo -n " "
- 9 done
- 10 for ((j=0;j<2*i-1;j++))
- 11 do
- 12 echo -n "*"
- 13 done
- 14 echo ""
- 15 done
- 16 for ((i=1;i<=num;i++))
- 17 do
- 18 for ((j=0;j<i;j++))
- 19 do
- 20 echo -n " "
- 21 done
- 22 for ((j=0;j<2*(num-i)-1;j++))
- 23 do
- 24 echo -n "*"
- 25 done
- 26 echo ""
- 27 done
- 5. 输出当前目录下所有文件,并输出文件总数和目录总数
- 1 #!/bin/bash
- 2
- 3 ls -al
- 4 filenum=0
- 5 dirnum=0
- 6 for q in `ls -a`
- 7 do
- 8 if [ -d $q ]
- 9 then
- 10 dirnum=`expr $dirnum + 1`
- 11 fi
- 12 filenum=`expr $filenum + 1`
- 13 done
- 14 echo "The number of dirctary is $dirnum"
- 15 echo "The number of file is $filenum"
- 6.菜单
- 1 #!/bin/bash
- 2 while true
- 3 echo "List Directory..........l "
- 4 echo "Change Directory........c "
- 5 echo "Edit File...............e "
- 6 echo "Remove File.............r "
- 7 echo "Exit Menu...............q "
- 8 read ch
- 9 do
- 10 case $ch in
- 11 l) ls;;
- 12 c) echo Enter target directory
- 13 read direc
- 14 cd "$direc"
- 15 ;;
- 16 e) echo Enter file name
- 17 read file
- 18 vi $file
- 19 ;;
- 20 r) echo Enter file name
- 21 read file
- 22 rm $file
- 23 ;;
- 24 q|Q) echo Goodbye
- 25 break;;
- 26 *) echo illegal Option
- 27 esac
- 28 done
- 7. ./shuru a b c d e f
- 1 #!/bin/bash
- 2 for (( b=$#; b>0; b-- ))
- 3 do
- 4 echo $*;
- 5 shift;
- 6 done
- 8.电话本
- 1 #!/bin/bash
- 2 if [ ! -f /root/minda ]
- 3 then
- 4 touch /root/minda
- 5 fi
- 6 while true
- 7 echo "If you want to add a user plese type----------------------a"
- 8 echo "If you want to del a user plese type----------------------d"
- 9 echo "If you want to watch all users please type----------------l"
- 10 echo "If you want to bianji users please tyye-------------------e"
- 11 echo "If you want to exit please type---------------------------q"
- 12 read b
- 13 do
- 14 case $b in
- 15 a) echo "Please type your name :"
- 16 read name
- 17 echo "Please type your number :"
- 18 read number
- 19 echo $name $number >> /root/minda;;
- 20 d) echo "Please type which name do you want to del:"
- 21 read name
- 22 cat /root/minda|grep ^[^$name]> /root/minda;;
- 23 l) cat /root/minda;;
- 24 e) echo "Please type which name do you want to del:"
- 25 read name
- 26 cat /root/minda|grep ^[^$name]> /root/minda
- 27 echo "Please type your new name :"
- 28 read name
- 29 echo "Please type your new number :"
- 30 read number
- 31 echo $name $number >> /root/minda;;
- 32 q) exit;;
- 33 esac
- 34 done
- 9.判断当前用户的类型
- 1 #!/bin/bash
- 2 case $UID in
- 3 0) echo this is root;;
- 4 [0-9][0-9]) echo this is system;;
- 5 [1-4][0-9][0-9]) echo this is service;;
- 6 *) echo this is public;;
- 7 esac
- 10.计算1-100的和
- 1 #!/bin/bash
- 2 b=0
- 3 for ((a=1;a<=100;a++))
- 4 do
- 5 b=`expr $b + $a `
- 6 done
- 7 echo b= $b
- 11.用一个文件记录系统中所有rpm包名,如果文件不存在则创建,如果存在则显示文件内容。
- 1 #!/bin/bash
- 2 a=/root/file
- 3 if [ -f $a ]
- 4 then
- 5 cat $a
- 6 else
- 7 rpm -qa > $a
- 8 fi
- 12.用户登陆
- 1 #!/bin/bash
- 2 n=1
- 3 while [ $n -gt 0 ]
- 4 do
- 5 echo "xinjian------------------1"
- 6 echo "denglu-------------------2"
- 7 echo "exit---------------------3"
- 8 read n;
- 9 case $n in
- 10 1) echo "username"
- 11 read name
- 12 if [ -f $name ]
- 13 then
- 14 echo "same username"
- 15 else
- 16 touch /root/$name
- 17 echo "passwd"
- 18 read passwd
- 19 echo $passwd >> /root/$name
- 20 fi;;
- 21 2) echo "username"
- 22 read name1
- 23 if [ -f $name1 ]
- 24 then
- 25 echo "passwd"
- 26 else
- 27 echo "yonghu wrong"
- 28 fi
- 29 read passwd1
- 30 if [ $passwd1 -eq `cat /root/$name1` ]
- 31 then
- 32 echo "success"
- 33 break
- 34 else
- 35 echo "passwd wrong"
- 36 fi;;
- 37 3) break;;
- 38 esac
- 39 done
- 13.创建四个文件test1,test2,test3,test4,实现自动创建dir1,dir2,dir3,dir4四个目录,并将test1,test2,test3,
- test4四个文件分别拷贝到dir1,dir2,dir3,dir4相应的目录下。
- 1 #!/bin/bash
- 2 for a in 1 2 3 4
- 3 do
- 4 touch test$a
- 5 mkdir dir$a
- 6 cp test$a dir$a
- 7 done
- 14.输入任意数求和./a.out 1 2 3 4 5 6
- 1 #!/bin/bash
- 2 s=0
- 3 for i in $*
- 4 do
- 5 s=`expr $s + $i`
- 6 done
- 7 echo $s
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · centos安装node18
- · nginx日志按天分割
- · 解决Linux实例磁盘空间满问题
- · centos yum安装Python3.8(史上最简单)
- · linux查看端口占用 netstat -tln|grep 8080
- · 解决jenkins git@github.com: Permission denied (publickey)
- · centos安装chrome+chromedriver
- · 修改FRP默认的http404页面后自行编译
- · k8s查看pod实时日志
- · docker nginx
- · VM VirtualBox 给centos安装增强功能
- · reboot 后 Docker服务及容器自动启动设置