SHELL脚本编程 源程序 (二)

5、循环结构

  实例12:在列表中的值:a,b,c,e,I,2,4,6,8用循环的方式把字符与数字分成两行输出。
编辑程序:

[root@localhost  bin]#gedit test11
#! /bin/Bash
for i in a,b,c,e,I  2,4,6,8
do
echo $i
done

执行结果:

[root@localhost  bin]#chmod +x test11
[root@localhost  bin]#./ test11
a,b,c,e,i
2,4,6,8

  实例 13:删除垃圾箱中的所有文件。
编辑程序:

[root@localhost  bin]#gedit test12
#! /bin/Bash
for i in $HOME/.Trash/*
do
    rm $ i
echo “$ i has been deleted!”
done

执行结果:

[root@localhost  bin]#chmod +x test12
[root@localhost  bin]#./ test12
/root/.Trash/abc~ has been deleted!
/root/.Trash/abc1 has been deleted!

  实例14:求从1~100的和。
编辑程序:

[root@localhost  bin]#gedit test13
#! /bin/Bash
total =0
for((j=1;j<=100;j++));
do
     total=`expr $total + $j` 
done
echo “The result is $total”

执行结果:

[root@localhost  bin]#chmod +x test13
[root@localhost  bin]#./ test13
The result is 5050

  实例 15:用while循环求1~100的和
编辑程序:

[root@localhost  bin]#gedit test13
total =0
num=0
   while((num<=100));
do
      total=’expor $total +$ num’ 
done
echo “The result is $total”

执行结果:

[root@localhost  bin]#chmod +x test14
[root@localhost  bin]#./ test14
The result is 5050

  实例 16:用until循环求1~100的和。
编辑程序:

[root@localhost  bin]#gedit test15
total =0
num=0
   until [$sum –gt 100]
   do
       total=’expor $total +$ num’ 
       num=’expr $num + 1’
done
echo “The result is $total”

执行结果:

[root@localhost  bin]#chmod +x test15
[root@localhost  bin]#./ test15
The result is 5050

6、条件结构

  实例17:输出10以内的奇数
编辑程序:

[root@localhost  bin]#gedit test16
for((j=0;j<=10;j++))
  do
       if(($j%2==1))
       then
         echo “$j”
fi
done

执行结果:

[root@localhost  bin]#chmod +x test16
[root@localhost  bin]#./ test16
13579

  实例 18:Linux是一个多用户操作系统,编写一程序根据不同的用户登录输出不同的反馈结果。
编辑程序:

[root@localhost  bin]#gedit test17
  #!/bin/sh
  case $USER in
beechen)
 echo “You are beichen!”;;
liangnian)
	echo “You are liangnian”;        //注意这里只有一个分号
  echo “Welcome !”;;            //这里才是两个分号
 root)
  echo “You are root!”;echo “Welcome !”;;
    //将两命令写在一行,用一个分号作为分隔符
*)
  echo “Who are you?$USER?”;;
easc

执行结果:

[root@localhost  bin]#chmod +x test17
[root@localhost  bin]#./ test17
You are root
Welcome! 

7、函数

  实例 19:编写一函数add求两个数的和,这两个数用位置参数传入,最后输出结果。
编辑代码:

[root@localhost  bin]#gedit test18
#!/bin/sh
add()
{
a=$1
b=$2
z=’expr $a + $b’
echo “The sum is $z”
}
add $1 $2

执行结果:

[root@localhost  bin]#chmod +x test18
[root@localhost  bin]#./ test18  10  20 
The sum is 30

8、调用脚本

  实例 20:在Shell脚本test19中调用test20。
编辑程序:

#test19脚本
#!/bin/sh
echo “The main name is $0”
./test20
echo “The first string is $1”
#test20脚本
#!/bin/sh
echo “How are you $USER?”

执行结果:

[root@localhost  bin]#chmod +x test19
[root@localhost  bin]#chmod +x test20
[root@localhost  bin]#./ test19 abc123
The main name is ./test19
How are you root?
the first string is abc123
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值