linux shell while 等待,Shell脚本编程中的if、case、for、while、until命令

这篇博客介绍了如何利用Shell脚本在终端中创建星形图案和国际象棋棋盘。脚本首先检查终端宽度以确保能正确显示,然后通过读取用户输入的行数来生成不同大小的星形。对于国际象棋棋盘,脚本使用until循环按行填充黑白相间的格子,实现了8x8的标准棋盘展示。
摘要由CSDN通过智能技术生成

(9)作业

1、写个脚本,实现星三角

[root@centos6 scripts]# cat sanjiao.sh

#!/bin/bash

termCols=`tput cols`

maxLineNum=`seq 1 2 $termCols | wc -l`

echo "#当前终端的宽度最多能够正常打印$maxLineNum行"

read -p "#请输入要打印的三角形的行数,行数范围大于1小于等于$maxLineNum,请输入行数: " lineNeeds

[[ `tput cols` -ne $termCols ]] && echo "终端宽度已经被改变,请重新运行脚本"  && exit

[[ $lineNeeds =~ ^[[:digit:]]+$ && $lineNeeds -ge 2 && $lineNeeds -le $maxLineNum ]]

[[ $? -ne 0 ]] && echo "输入值不合法" && exit

for line in `seq 1 $lineNeeds`;do

for ((kong=(lineNeeds-line);kong>0;kong--));do

echo -n " "

done

for((xing=1;xing<=(line*2-1);xing++));do

echo -n "*"

done

echo

done

[root@centos6 scripts]# bash sanjiao.sh

#当前终端的宽度最多能够正常打印79行

#请输入要打印的三角形的行数,行数范围大于1小于等于79,请输入行数: 5

*

***

*****

*******

*********

[root@centos6 scripts]#

2、用until循环实现国际象棋棋盘

[root@centos6 scripts]# cat xiangqi.sh

#!/bin/bash

#

#

#

i=1

until [ $i -gt 8 ];do

j=1

while [ $j -le 8 ];do

sum=`expr $i + $j`

z=`expr $sum % 2`

[ $z -eq 0 ] && echo -ne "\033[41;1m  \033[0m"||echo -ne "\033[43;1m  \033[0m"

let j++

done

echo

let i++

done

[root@centos6 scripts]#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值