shell脚本基础练习

一、

1、编写脚本systeminfo.sh,显示当前主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小

[root@centos7 script]# cat systeminfo.sh 
#!/bin/bash
echo "当前主机名:`hostname`"
echo "IPv4地址:`ifconfig ens33 | grep "inet " | tr -s ' ' | cut -d' ' -f3`"
echo "操作系统版本:`uname -a | cut -d' ' -f1,2`"
echo "内核版本:`uname -a | cut -d' ' -f3`"
echo "CPU型号:`lscpu | grep 'Model name:' | tr -s ' ' | cut -d: -f2`"
echo "内存大小:`free -h | tail -n +2 | head -1 | tr -s ' ' | cut -d' ' -f2`"
echo "根目录磁盘大小:`df -h | grep '/dev/sd.*/$' | tr -s ' ' | cut -d' ' -f4`"
[root@centos7 script]# ./systeminfo.sh 
当前主机名:centos7
IPv4地址:10.0.0.150
操作系统版本:Linux centos7
内核版本:3.10.0-1160.el7.x86_64
CPU型号: Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
内存大小:972M
根目录磁盘大小:99G

2、编写脚本backup.sh,可实现每日将/etc/目录备份到/backup/etcYYYY-mm-dd中

[root@centos7 script]# cat backup.sh 
#!/bin/bash
mkdir -p /backup &> /dev/null
cp -a /etc/ /backup/etc`date +%F`
echo "done !"

3、编写脚本disk.sh,显示当前硬盘分区中空间利用率最大的值

[root@centos7 script]# cat disk.sh 
#!/bin/bash
df | grep "^/dev/sd*" | tr -s ' ' | cut -d' ' -f5 | sort -nr | head -1
[root@centos7 script]# ./disk.sh 
13%

4、编写脚本links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序

[root@centos7 script]# cat links.sh 
#!/bin/bash

echo "当前连接服务器IP数:`netstat -tan | grep "ESTAB" | tr -s ' ' ':' | cut -d: -f6 | sort | uniq -c | sort -nr`"
[root@centos7 script]# ./links.sh 
当前连接服务器IP数:      3 10.0.0.1

 二、

1、编写脚本argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数

[root@centos7 script]# cat argsnum.sh 
#!/bin/bash
ARGS_NUM=$#
[ "$ARGS_NUM" -lt 1 ] && { echo "至少传递一个参数";exit; }
if [ -f $1 ]; then
    grep -E "^$" $1 | wc -l
else
    echo "传递不是有效文件"
fi
[root@centos7 script]# cat test.txt 
你好吗

hello

你在哪

哈哈
[root@centos7 script]# ./argsnum.sh test.txt 
3

2、编写脚本hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该P地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”

[root@centos7 script]# cat hostping.sh 
#!/bin/bash
IP=$1
if [ $# -lt 1 ];then
    echo "请至少传入一个参数"
    exit
fi
if [[ ! "$IP" =~ ^([0-9]{1,3}.){3}[0-9]{1,3}$  ]];then
    echo "请传入正确的IPv4地址"
    exit
fi
ping -c 1 "$IP" &> /dev/null
if [ $? -eq 0 ];then
    echo "该IP地址可以访问"
else
    echo "该IP地址不可以访问"
fi

[root@centos7 script]# ./hostping.sh 10.0.0.1
该IP地址可以访问
[root@centos7 script]# ./hostping.sh 10.0.0.10
该IP地址不可以访问
[root@centos7 script]# ./hostping.sh 10.0.0.1022
请传入正确的IPv4地址

3、编写脚本 checkdisk.sh,检查磁盘分区空间和inode使用率,如果超过80%,就发广播警告空间将满

[root@centos7 script]# cat checkdisk.sh 
#!/bin/bash
WARNING_SIZE=80
DISK_SIZE=`df | grep "^/dev/sd*" | tr -s ' ' | cut -d' ' -f5 | sort -nr | tr -d '%' | head -1`
INODE_SIZE=`df -i | grep "^/dev/sd*" | tr -s ' ' | cut -d' ' -f5 | sort -nr | tr -d '%' | head -1`

if [ "$WARNING_SIZE" -lt "$DISK_SIZE" ];then
    echo "广播了,磁盘空间将满,请及时处理"
fi

if [ "$WARNING_SIZE" -lt "$INODE_SIZE" ];then
    echo "广播了,磁盘inode将满,请及时处理"
fi


[root@centos7 script]# df -i
Filesystem       Inodes IUsed    IFree IUse% Mounted on
devtmpfs         121767   381   121386    1% /dev
tmpfs            124459     1   124458    1% /dev/shm
tmpfs            124459   742   123717    1% /run
tmpfs            124459    16   124443    1% /sys/fs/cgroup
/dev/sda2      52428800 38300 52390500    1% /
/dev/sda5      26214400    21 26214379    1% /data
/dev/sda1        524288   326   523962    1% /boot
tmpfs            124459     1   124458    1% /run/user/0
[root@centos7 script]# mkdir /boot/testdir
[root@centos7 script]# cd /boot/testdir/;echo file{1..523962} | xargs touch
touch: cannot touch ‘file523962’: No space left on device
[root@centos7 testdir]# df -i
Filesystem       Inodes  IUsed    IFree IUse% Mounted on
devtmpfs         121767    381   121386    1% /dev
tmpfs            124459      1   124458    1% /dev/shm
tmpfs            124459    742   123717    1% /run
tmpfs            124459     16   124443    1% /sys/fs/cgroup
/dev/sda2      52428800  38300 52390500    1% /
/dev/sda5      26214400     21 26214379    1% /data
/dev/sda1        524288 524288        0  100% /boot
tmpfs            124459      1   124458    1% /run/user/0
[root@centos7 testdir]# ls |wc -l
523961
[root@centos7 testdir]# cd -
/data/script
[root@centos7 script]# ./checkdisk.sh 
广播了,磁盘inode将满,请及时处理

4、编写脚本per.sh,判断当前用户对指定参数文件,是否不可读并且不可写

[root@centos7 script]# cat per.sh 
#!/bin/bash
FILE=$1
if [ ! -f "$FILE" ];then
    echo "请传入有效文件"
    exit
fi

[ ! -r "$FILE" ] && { echo "文件不可读";exit; }
[ ! -w "$FILE" ] && { echo "文件不可写";exit; }
echo "$FILE 文件有读写权限"

[root@centos7 script]# su - wang
Last login: Wed Mar 24 14:46:20 CST 2021 on pts/0
[wang@centos7 ~]$ cd /data/script/
[wang@centos7 script]$ ll
total 56
-rwxr-xr-x. 1 root root 193 Mar 31 11:13 per.sh
-rw-r--r--. 1 root root  36 Mar 31 10:34 test.txt
[wang@centos7 script]$ ./per.sh test.txt 
文件不可写
[wang@centos7 script]$ exit
logout
[root@centos7 script]# ./per.sh test.txt 
test.txt 文件有读写权限

5、编写脚本excute.sh,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限,否则提示用户非脚本文件

[root@centos7 script]# cat excute.sh 
#!/bin/bash
file=$1
if [[ "$file" =~ .sh$ && -f "$file" ]];then
    chmod a+x $file
    echo "已添加执行权限"
else
    echo "不是脚本文件"
fi
[root@centos7 script]# 
[root@centos7 script]# ll backup.sh 
-rw-r--r--. 1 root root 90 Mar 31 10:02 backup.sh
[root@centos7 script]# ./excute.sh backup.sh 
已添加执行权限
[root@centos7 script]# ll backup.sh 
-rwxr-xr-x. 1 root root 90 Mar 31 10:02 backup.sh

6、编写脚本nologin.sh和login.sh,实现禁止和允许普通用户登录系统

[root@centos7 script]# cat login.sh 
#!/bin/bash
user=$1
id "$user" &> /dev/null
if [ $? -eq 0 ];then
    usermod "$user" -s /bin/bash
    echo "$user 用户可以正常登陆"
else
    echo "$user 用户不存在"
fi

[root@centos7 script]# 
[root@centos7 script]# grep wang /etc/passwd
wang:x:1000:1000::/home/wang:/bin/false
[root@centos7 script]# ./login.sh wang
wang 用户可以正常登陆
[root@centos7 script]# grep wang /etc/passwd
wang:x:1000:1000::/home/wang:/bin/bash


#nologin.sh
[root@centos7 script]# cat nologin.sh 
#!/bin/bash
user=$1
id "$user" &> /dev/null
if [ $? -eq 0 ];then
    usermod "$user" -s /bin/false
    echo "$user 用户已禁止登陆"
else
    echo "$user 用户不存在"
fi
[root@centos7 script]# 
[root@centos7 script]# ./nologin.sh wang
wang 用户已禁止登陆
[root@centos7 script]# grep wang /etc/passwd
wang:x:1000:1000::/home/wang:/bin/false

三、

1、让所有用户的PATH环境变量的值多出一个路径,例如:/usr/local/apache/bin

[root@centos7 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos7 ~]# echo export PATH="/usr/local/apache/bin:$PATH" >> /etc/profile
[root@centos7 ~]# . /etc/profile
[root@centos7 ~]# echo $PATH
/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

2、用户root登录时,将命令指示符变成红色,并自动启用如下别名:

rm='rm -f'

cdnet=cd /etc/sysconfig/network-scripts
editnet=vim /etc/sysconfig/network-scripts/ifcfg-etho
editnet=vim /etc/sysconfig/network-scripts/ifcfg-eno16777736或ifcfg-ens33'(如果系统是CentOS7)

[root@centos7 data]# cat <<EOF >> /root/.bashrc 
alias rm="rm -f"
alias cdnet="cd /etc/sysconfig/network-scripts"
alias editnet="vim /etc/sysconfig/network-scripts/ifcfg-ens33"
EOF

[root@centos7 data]# . /root/.bashrc 
[root@centos7 data]# cdnet
[root@centos7 network-scripts]# editnet

3、任意用户登录系统时,显示红色字体的警示提醒信息"Hidangerous! "

经测试/etc/motd只能展示普通文本,所以需要编写开机启动脚本来完成

[root@centos7 script]# pwd
/data/script
[root@centos7 script]# cat start.sh 
#!/bin/bash
echo -e "e[31;40m Hi,dangerous!e[0m"
[root@centos7 script]# ./start.sh 
 Hi,dangerous!
[root@centos7 script]# echo "/data/script/start.sh" >> /etc/profile
[root@centos7 script]# . /etc/profile
 Hi,dangerous!
[root@centos7 script]# exit
logout
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(centos7) at 13:51:10.

Type `help' to learn how to use Xshell prompt.
[C:~]$ 
Reconnecting in 3 seconds. Press any key to exit local shell.
...

Connecting to 10.0.0.150:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Wed Mar 31 13:38:38 2021 from 10.0.0.1
 Hi,dangerous!

4、编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等

vim ~/.vimrc
5 autocmd BufNewFile *.sh exec ":call SetTitle()"
  6 func SetTitle()
  7     if expand("%:e")=='sh'
  8             call setline(1,"#!/bin/bash")
  9             call setline(2,"#")
 10             call setline(3,"#*************************************")
 11             call setline(4,"#author:                wangtiankong")
 12             call setline(5,"#QQ:                    67063492")
 13             call setline(6,"#email:                 67063492@qq.com")
 14             call setline(7,"#version:               1.0")
 15             call setline(8,"#date:                  ".strftime("%Y-%m-%d"))
 16             call setline(9,"#description:           script")
 17             call setline(10,"#*************************************")
 18 
 19     endif
 20 
 21 endfunc

 四、

1、编写脚本createuser.s,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之。并设置初始密码为123456,显示添加的用户的id号等信息,在此新用户第一次登录时,会提示用户立即改密码,如果没有参数,就提示:请输入用户名

[root@centos7 script]# cat createuser.sh 
#!/bin/bash
#
#*************************************
#author:                wangtiankong
#QQ:                    67063492
#email:                 67063492@qq.com
#version:               1.0
#date:                  2021-03-31
#description:           script
#*************************************
user=$1
if [ $# -lt 1 ];then
    echo "请输入用户名"
    exit
fi
id "$user" &> /dev/null
if [ $? -eq 0 ];then
    echo "$user 用户已存在"
    exit
fi

useradd "$user" && echo -e "123456
123456" | passwd "$user" &> /dev/null && passwd -e "$user" &> /dev/null && id "$user"

[root@centos7 script]# ./createuser.sh wang6
wang6 用户已存在
[root@centos7 script]# ./createuser.sh wang7
uid=1021(wang7) gid=1024(wang7) groups=1024(wang7)

2、编写脚本yesorno.sh,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息

[root@centos7 script]# cat yesorno.sh 
#!/bin/bash
#
#*************************************
#author:                wangtiankong
#QQ:                    67063492
#email:                 67063492@qq.com
#version:               1.0
#date:                  2021-03-31
#description:           script
#*************************************
read -p "请输入yes或no:" answer
answer=`echo $answer |tr [A-Z] [a-z]`;

if [ "$answer" = yes ]; then
    echo "输入yes"
elif [ "$answer" = no ]; then
    echo "输入no"
else
    echo "输入的是:$answer"
fi
[root@centos7 script]# 

3、编写脚本 filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)

[root@centos7 script]# cat filetype.sh 
#!/bin/bash
#
#*************************************
#author:                wangtiankong
#QQ:                    67063492
#email:                 67063492@qq.com
#version:               1.0
#date:                  2021-03-31
#description:           script
#*************************************
file=$1
if [ $# -lt 1 ];then
    echo "请传入参数"
    exit
fi

if [ -L "$file" ];then
    echo "连接文件"
elif [ -d "$file" ];then
    echo "目录文件"
elif [ -f "$file" ];then
    echo "普通文件"
else
    echo "其他文件"
fi

4、编写脚本checkint.sh,判断用户输入的参数是否为正整数

[root@centos7 script]# cat checkini.sh 
#!/bin/bash
#
#*************************************
#author:                wangtiankong
#QQ:                    67063492
#email:                 67063492@qq.com
#version:               1.0
#date:                  2021-03-31
#description:           script
#*************************************
num=$1
if [[ "$num" =~ ^[1-9]  && "$num" =~ ^[0-9]+$ ]];then
    echo "yes $num"
else
    echo "no $num"
fi

5、编写脚本 reset.sh,实现系统安装后的初始化环境,包括: 1、别名 2、环境变量,如PS1等 3安装常用软件包,如:tree 5、实现固定的IP的设置,6、vim的设置等

[root@centos7 script]# cat reset.sh 
#!/bin/bash
#
#*************************************
#author:                wangtiankong
#QQ:                    67063492
#email:                 67063492@qq.com
#version:               1.0
#date:                  2021-03-31
#description:           script
#*************************************
echo "alias rm="rm -f"" >> /root/.bashrc
yum -y install vim  lrzsz tree  bc wget  redhat-lsb-core postfix  mailx   bash-completion man-pages &> /dev/null
systemctl disable --now firewalld
echo 'PS1="[e[1;32m][	 [e[1;33m]u[e[35m]@h[e[1;31m] W[e[1;32m]][e[0m]\$"' > /etc/profile.d/env.sh

五、

练习:用for实现
1、判断/var/目录下所有文件的类型

[15:56:41 root@centos7 script]#cat var_type.sh 
#!/bin/bash
#
#*************************************
#author:                wangtiankong
#QQ:                    67063492
#email:                 67063492@qq.com
#version:               1.0
#date:                  2021-03-31
#description:           script
#*************************************
cd /var
for i in *
do
    if [ -f $i ];then
        echo "$i 是普通文件"
    elif [ -L $i ];then
        echo "$i 是连接文件"
    elif [ -d $i ];then
        echo "$i 是目录"
    else
        echo "$i 是其他文件"
    fi
done

2、添加10个用户user1-user10,密码为8位随机字符

[16:12:08 root@centos7 script]#cat adduser.sh 
#!/bin/bash
#
#*************************************
#author:                wangtiankong
#QQ:                    67063492
#email:                 67063492@qq.com
#version:               1.0
#date:                  2021-03-31
#description:           script
#*************************************
for i in {1..10};do
    user="auser$i"
    pass=`cat /dev/urandom | tr -dc a-z0-9A-Z | head -c 8`
    useradd "$user" && echo -e "$pass
$pass" | passwd "$user" &> /dev/null
    echo "user:$user,passwd:$pass" >> adduser.log
done
[16:12:22 root@centos7 script]#
[16:11:57 root@centos7 script]#cat adduser.log 
user:auser1,passwd:ydC44euO
user:auser2,passwd:G9M1ulQ1
user:auser3,passwd:B0bVNdKR
user:auser4,passwd:sv0ChFsq
user:auser5,passwd:CZEofRfT
user:auser6,passwd:HRF7jyix
user:auser7,passwd:8eTF3XU6
user:auser8,passwd:L8BRE3BG
user:auser9,passwd:onkqLHD7
user:auser10,passwd:04Nv1RIv

3、/etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加start,如K34filename stop S66filename start

[16:20:46 root@centos7 script]#cat ks.sh 
#!/bin/bash
#
#*************************************
#author:                wangtiankong
#QQ:                    67063492
#email:                 67063492@qq.com
#version:               1.0
#date:                  2021-03-31
#description:           script
#*************************************
for i in `ls /etc/rc.d/rc3.d/`;do
    if [[ $i =~ ^S ]];then
        echo "$i start"
    elif [[ $i =~ ^K ]];then
        echo "$i stop"
    fi
done

[16:20:44 root@centos7 script]#./ks.sh 
K50netconsole stop
S10network start

4、编写脚本,提示输入正整数n的值,计算1+2+...+n的总和

[16:27:53 root@centos7 script]#cat he.sh 
#!/bin/bash
#
#*************************************
#author:                wangtiankong
#QQ:                    67063492
#email:                 67063492@qq.com
#version:               1.0
#date:                  2021-03-31
#description:           script
#*************************************
read -p "请输入正整数:" n
if [[ ! ("$n" =~ ^[1-9] && "$n" =~ ^[0-9]+$) ]];then
    echo "请输入正确的正整数"
    exit
fi
sum=0
for i in `seq $n`;do
    let sum+=$i
done
echo $sum
[16:27:58 root@centos7 script]#
[16:27:47 root@centos7 script]#./he.sh
请输入正整数:3
6
[16:27:49 root@centos7 script]#./he.sh
请输入正整数:100
5050

5、计算100以内所有能被3整除的整数之和

sum=0
for i in {1..10};do
    let res=$i%3
    if [ "$res" = 0 ];then
        let sum+=$i    
    fi
done
echo $sum

6、编写脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态

[16:45:40 root@centos7 script]#cat ips.sh 
#!/bin/bash
#
#*************************************
#author:                wangtiankong
#QQ:                    67063492
#email:                 67063492@qq.com
#version:               1.0
#date:                  2021-04-01
#description:           script
#*************************************
read -p "请输入IP网段地址:" ips

if [[ ! "$ips" =~ ([0-9]{1,3}.){3}[0-9]{1,3}  ]];then
    echo "请输入正确的IP地址段"
    exit
fi
ip_prfix=`echo $ips | grep -Eo "([0-9]{1,3}.){3}" `
max=`echo $ips | cut -d. -f4`
for i in `seq $max`;do
    ip="$ip_prfix$i"
    ping -c1 -w5 $ip &> /dev/null
    if [ $? -eq 0 ];then
        echo -e "e[34;40m$ip 在线 e[0m"
    else
        echo -e "e[31;40m$ip 不在线 e[0m"
    fi
done

[16:45:07 root@centos7 script]#./ips.sh 
请输入IP网段地址:10.0.0.10
10.0.0.1 在线 
10.0.0.2 在线 
10.0.0.3 不在线 
10.0.0.4 不在线 
10.0.0.5 不在线 
10.0.0.6 不在线 
10.0.0.7 不在线 
10.0.0.8 不在线 
10.0.0.9 不在线 
10.0.0.10 不在线 

7、打印九九乘法表

[16:49:21 root@centos7 script]#cat cfb99.sh 
#!/bin/bash
#
#*************************************
#author:                wangtiankong
#QQ:                    67063492
#email:                 67063492@qq.com
#version:               1.0
#date:                  2021-04-01
#description:           script
#*************************************
for i in {1..9};do
    for j in `seq $i`;do
        let sum=$i*$j
        echo -ne "$j*$i=$sum	"
    done
    echo
done

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

8、在testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母,如:1AbCdeFgH.html

[16:57:33 root@centos7 script]#cat createfile.sh 
#!/bin/bash
#
#*************************************
#author:                wangtiankong
#QQ:                    67063492
#email:                 67063492@qq.com
#version:               1.0
#date:                  2021-04-01
#description:           script
#*************************************
[ -d /testdir ] || mkdir /testdir
for i in {1..10};do
    randomName=`cat /dev/urandom | tr -dc a-zA-Z | head -c8 `
    touch "/testdir/$i$randomName"
done
echo "创建成功"

[16:57:25 root@centos7 script]#ll /testdir/
total 0
[16:57:27 root@centos7 script]#./createfile.sh 
创建成功
[16:57:30 root@centos7 script]#ll /testdir/
total 0
-rw-r--r--. 1 root root 0 Apr  1 16:57 10EKCqhpfv
-rw-r--r--. 1 root root 0 Apr  1 16:57 1LvQyQrKK
-rw-r--r--. 1 root root 0 Apr  1 16:57 2TvzwtSKd
-rw-r--r--. 1 root root 0 Apr  1 16:57 3pXfBRCRi
-rw-r--r--. 1 root root 0 Apr  1 16:57 4zQkyhZVl
-rw-r--r--. 1 root root 0 Apr  1 16:57 5GFFiyimH
-rw-r--r--. 1 root root 0 Apr  1 16:57 6xeuWvdmG
-rw-r--r--. 1 root root 0 Apr  1 16:57 7leJsVHrW
-rw-r--r--. 1 root root 0 Apr  1 16:57 8CbUswEEZ
-rw-r--r--. 1 root root 0 Apr  1 16:57 9SqFaganw

9、打印等腰三角形

[17:11:46 root@centos7 script]#cat yao.sh 
#!/bin/bash
#
#*************************************
#author:                wangtiankong
#QQ:                    67063492
#email:                 67063492@qq.com
#version:               1.0
#date:                  2021-04-01
#description:           script
#*************************************
read -p "请输入三角形的行数:" line
for i in `seq $line`;do
    for ((k=0;k<=line-i;k++));do
        echo -e " c"
    done
    for ((j=1;j<=2*i-1;j++));do
        echo -e "*c"
    done
    echo
done
[17:11:52 root@centos7 script]#
[17:11:42 root@centos7 script]#./yao.sh 
请输入三角形的行数:10
          *
         ***
        *****
       *******
      *********
     ***********
    *************
   ***************
  *****************
 *******************

10、猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,只剩下一个桃子了。求第一天共摘了多少?

11、打印进度条

[17:07:56 root@centos7 script]#cat jindu.sh 
#!/bin/bash
#
#*************************************
#author:                wangtiankong
#QQ:                    67063492
#email:                 67063492@qq.com
#version:               1.0
#date:                  2021-04-01
#description:           script
#*************************************
for ((i=0;i<=100;i++));do
    printf "e[4D%3d%%" $i
    sleep 0.1s
done

六、

原文地址:https://www.cnblogs.com/tianakong/p/14600219.html