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\n123456" | 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\][\t \[\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\n$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\t"
    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

六、


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM