SHELL第四次作业

一、9 * 9 乘法表,for列表循环,for循环(c语言风格), while循环可选单层循环。

        在程序设计语言中,嵌套的循环也是一种非常常见的结构。Shell同样也支持嵌套循环。通过嵌套循环, 可以完成更复杂的功能。

四种方法

[root@localhost test]# vim multiplication_table.sh 
#!/bin/bash
for m in `seq 9`
do
        for n in `seq $m`
        do
                let result=$m*$n
                echo -n $m*$n=$result "  "
        done
        echo -e 
done
echo "**********************************************************************************"
i=1
while [ $i -lt 10 ]
do
        j=1
        while [ $j -le $i ]
        do
                declare -i res=$i*$j
                echo -n $i*$j=$res "  "
                let j++
        done
        echo -e
        let i++
done
echo "**********************************************************************************"
for ((i=1;i<10;i++))
do
        for ((j=1;j<=i;j++))
        do
        echo -n $i*$j=$(($i * $j))  "  "
        done
        echo -e
done
echo "**********************************************************************************"
for i in {1..9}
do
    echo `eval "echo -e {1..$i}*$i'\n'|bc -i -q"`
done


[root@localhost test]# bash multiplication_table.sh 
1*1=1   
2*1=2   2*2=4   
3*1=3   3*2=6   3*3=9   
4*1=4   4*2=8   4*3=12   4*4=16   
5*1=5   5*2=10   5*3=15   5*4=20   5*5=25   
6*1=6   6*2=12   6*3=18   6*4=24   6*5=30   6*6=36   
7*1=7   7*2=14   7*3=21   7*4=28   7*5=35   7*6=42   7*7=49   
8*1=8   8*2=16   8*3=24   8*4=32   8*5=40   8*6=48   8*7=56   8*8=64   
9*1=9   9*2=18   9*3=27   9*4=36   9*5=45   9*6=54   9*7=63   9*8=72   9*9=81   
**********************************************************************************
1*1=1   
2*1=2   2*2=4   
3*1=3   3*2=6   3*3=9   
4*1=4   4*2=8   4*3=12   4*4=16   
5*1=5   5*2=10   5*3=15   5*4=20   5*5=25   
6*1=6   6*2=12   6*3=18   6*4=24   6*5=30   6*6=36   
7*1=7   7*2=14   7*3=21   7*4=28   7*5=35   7*6=42   7*7=49   
8*1=8   8*2=16   8*3=24   8*4=32   8*5=40   8*6=48   8*7=56   8*8=64   
9*1=9   9*2=18   9*3=27   9*4=36   9*5=45   9*6=54   9*7=63   9*8=72   9*9=81   
**********************************************************************************
1*1=1   
2*1=2   2*2=4   
3*1=3   3*2=6   3*3=9   
4*1=4   4*2=8   4*3=12   4*4=16   
5*1=5   5*2=10   5*3=15   5*4=20   5*5=25   
6*1=6   6*2=12   6*3=18   6*4=24   6*5=30   6*6=36   
7*1=7   7*2=14   7*3=21   7*4=28   7*5=35   7*6=42   7*7=49   
8*1=8   8*2=16   8*3=24   8*4=32   8*5=40   8*6=48   8*7=56   8*8=64   
9*1=9   9*2=18   9*3=27   9*4=36   9*5=45   9*6=54   9*7=63   9*8=72   9*9=81 
**********************************************************************************
1*1=1   
2*1=2   2*2=4   
3*1=3   3*2=6   3*3=9   
4*1=4   4*2=8   4*3=12   4*4=16   
5*1=5   5*2=10   5*3=15   5*4=20   5*5=25   
6*1=6   6*2=12   6*3=18   6*4=24   6*5=30   6*6=36   
7*1=7   7*2=14   7*3=21   7*4=28   7*5=35   7*6=42   7*7=49   
8*1=8   8*2=16   8*3=24   8*4=32   8*5=40   8*6=48   8*7=56   8*8=64   
9*1=9   9*2=18   9*3=27   9*4=36   9*5=45   9*6=54   9*7=63   9*8=72   9*9=81 

二、使用for循环创建30个用户: test01~test30, 并设置密码为test01123456~test30123456。

[root@localhost test]# vim useradd.sh
#!/bin/bash
for i in `seq -f %02g 1 30`
do
        useradd test$i
        if ! id -u test$i &> /dev/null
        then
                echo "$i123456" |passwd --stdin test$i &> /dev/null
        else
                echo "test$i is exists..."
        fi
done
[root@localhost test]# cat /etc/passwd         #查看用户

三、使用循环去判断网段内的IP(1~254),本机除外,可以ping通的使用 ssh远程登录。

[root@localhost test]# vim ping_ssh.sh
#!/bin/bash 
for i in {1..254}
do
        ping -i 0.2 -c 2 -w 1 192.168.132.$i &> /dev/null
        if [ $? -eq 0 -a $i -ne 129 ]
        then
                ssh root@192.168.132.$i
        else
                echo "192.168.132.$i is down"
        fi
done

四、使用$@和$*作为for循环后的列表,并体现出区别。

[root@localhost test]# vim diff_symbol.sh  a b c
#!/bin/bash
for i in $@
do
        echo $i
 
done
for i in "$@"
do
        echo $i
done
echo "**********************"
for i in $*
do
        echo $i
done
for i in "$*"
do
        echo $i
done

        当 $* 和 $@ 不被双引号""包围时,它们之间没有任何区别,都是将接收到的每个参数看做一份数据,彼此之间以空格来分隔。

但是当它们被双引号" "包含时,就会有区别了:

        "$*"会将所有的参数从整体上看做一份数据,而不是把每个参数都看做一份数据。
        "$@"仍然将每个参数都看作一份数据,彼此之间是独立的。

五、使用循环去读取文件内容并输出: 3种方式(1.exec+while循环 2.管道符+while循环 3.重定向+while)。

[root@localhost test]# vim echo_file.sh
#!/bin/bash
exec < file
while read a
do
        echo $a
done
echo "***********************************"
cat /test02/file |while read a
do
        echo $a
done
echo "***********************************"
while read a
do
        echo $a
done < file

[root@localhost test]# bash echo_file.sh
this1
this2
this3
this4
this5
***********************************
this1
this2
this3
this4
this5
***********************************
this1
this2
this3
this4
this5

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值