SHELL

16-18

向bash脚本添加参数 #

basic #

─ ~/shellTest                   ly@vmmin 10:37:24
╰─❯ cat ./16myscript_cls.sh 
#!/bin/bash

echo "You entered the argument: $1,$2,$3, and $4."

结果

╭─ ~/shellTest               16s ly@vmmin 10:37:18
╰─❯ ./16myscript_cls.sh Linux1 Linux2
You entered the argument: Linux1,Linux2,, and .

示例1 #

╭─ ~/shellTest                      ly@vmmin 10:41:45
╰─❯ cat ./16myscript_cls.sh 
#!/bin/bash

ls -lh $1

#echo "You entered the argument: $1,$2,$3, and $4."
╭─ ~/shellTest                        ly@vmmin 10:41:28
╰─❯ ./16myscript_cls.sh /etc
total 792K
-rw-r--r-- 1 root root   3.0K May 25  2023 adduser.conf
-rw-r--r-- 1 root root     44 Dec 17 15:26 adjtime
-rw-r--r-- 1 root root    194 Dec 23 22:38 aliases
drwxr-xr-x 2 root root   4.0K Dec 23 22:38 alternatives
drwxr-xr-x 2 root root   4.0K Dec 17 15:24 apparmor
drwxr-xr-x 8 root root   4.0K Dec 17 15:25 apparmor.d
drwxr-xr-x 9 root root   4.0K Dec 17 15:30 apt
-rw-r----- 1 root daemon  144 Oct 16  2022 at.deny
-rw-r--r-- 1 root root   2.0K Mar 30  2024 bash.bashrc

示例2 #

#!/bin/bash

lines=$(ls -lh $1 | wc -l) #行计数

echo "You hava $(($lines-1)) objects in the $1 directory."
#$(($lines-1))这里用到了子shell

#echo "You entered the argument: $1,$2,$3, and $4."
╭─ ~/shellTest                                 ly@vmmin 10:48:06
╰─❯ ls -lh logfiles 
total 12K
-rw-r--r-- 1 ly ly   0 Dec 22 23:07 a.log
-rw-r--r-- 1 ly ly 120 Dec 22 23:17 a.log.tar.gz
-rw-r--r-- 1 ly ly   0 Dec 22 23:07 b.log
-rw-r--r-- 1 ly ly 121 Dec 22 23:17 b.log.tar.gz
-rw-r--r-- 1 ly ly   0 Dec 22 23:07 c.log
-rw-r--r-- 1 ly ly 121 Dec 22 23:17 c.log.tar.gz
-rw-r--r-- 1 ly ly   0 Dec 22 23:15 xx.txt
-rw-r--r-- 1 ly ly   0 Dec 22 23:15 y.txt
╭─ ~/shellTest                                  ly@vmmin 10:48:10
╰─❯ ./16myscript_cls.sh logfiles
You hava 8 objects in the logfiles directory.

head,表示前十行,可以看出total这些被算作一行了,所以上面的shell中-1

...

12-15

functions 函数 #

update这个脚本为基础编改
作用

  • 减少重复代码
#!/bin/bash

release_file=/etc/os-release
logfile=/var/log/updater.log
errorlog=/var/log/updater_errors.log

check_exit_status(){
    if [ $? -ne 0 ]
    then
        echo "An error occured,please check the $errorlog file."
    fi
}

if  grep -q "Arch" $release_file 
then
    sudo pacman -Syu 1>>$logfile 2>>$errorlog
    check_exit_status
fi

if grep -q "Ubuntu" $release_file ||  grep -q "Debian" $release_file 
then
    sudo apt update 1>>$logfile 2>>$errorlog
    check_exit_status
    #默认yes
    sudo apt dist-upgrade -y 1>>$logfile 2>>$errorlog
    check_exit_status
fi

CaseStatements #

脚本 #

╭─ ~/shellTest                                                              ly@vmmin 22:32:52
╰─❯ cat ./13myscript_cls.sh 
#!/bin/bash

finished=0

while [ $finished -ne 1 ]
do
    echo "What is your favorite Linux distribution?"
    
    echo "1 - Arch"
    echo "2 - CentOS"
    echo "3 - Debian"
    echo "4 - Mint"
    echo "5 - Something else.."
    echo "6 - exit"
    
    read distro;
    case $distro in 
      1) echo "Arch is xxx";;
      2) echo "CentOS is xbxxx";;
      3) echo "Debian is bbbxx";;
      4) echo "Mint is xxxxsss";;
      5) echo "Something els.xxxxx";;
      6) finished=1
         echo "now will exit"
         ;;
      *) echo "you didn't enter an xxxx choice."
    esac
done

echo "Thanks for using this script."

脚本执行 #

╭─ ~/shellTest                                                              ly@vmmin 22:32:11
╰─❯ ./13myscript_cls.sh   
What is your favorite Linux distribution?
1 - Arch
2 - CentOS
3 - Debian
4 - Mint
5 - Something else..
6 - exit
3
Debian is bbbxx
What is your favorite Linux distribution?
1 - Arch
2 - CentOS
3 - Debian
4 - Mint
5 - Something else..
6 - exit
u
you didn't enter an xxxx choice.
What is your favorite Linux distribution?
1 - Arch
2 - CentOS
3 - Debian
4 - Mint
5 - Something else..
6 - exit
6
now will exit
Thanks for using this script. 

ScheduleJobs #

作用 #

脚本在特定时间运行

...

07-10

WhileLoops #

范例 #

#!/bin/bash

myvar=1
#小于或者等于10
while [ $myvar -le 10 ]
do
    echo $myvar
    myvar=$(( $myvar + 1  ))
    sleep 0.5
done

运行

╭─ ~/shellTest                              ≡ ly@vmmin 12:10:33
╰─❯ ./71myscript_cls.sh 
1
2
3
4
5
6
7
8
9
10

数字会每隔0.5s就输出一次
对于myvar=$(( $myvar + 1 ))$((expression))形式表示算数运算,而且其中的空格是可以省略的

范例2 #

#!/bin/bash

while [ -f ~/testfile ]
do
    echo "As of $(date),the test file exists."
    sleep 5
done

echo "As of $(date), the test ....has gone missing."

用来测试文件是否存在,运行前先新建一下文件touch ~/testfile 运行一会后把文件删除,如图

...

06ExitCode

意义 #

用来确定代码是否执行成功

例子 #

ls -l /misc
echo $? #输出2
ls -l ~
echo $? #输出0

$?用来显示最近一个命令的状态,零表示成功,非零表示失败

#!/bin/bash
#这个例子之前,作者用 sudo apt remove htop 命令把htop删除了
package=htop
sudo apt install $package

echo "The exit code for ....is $?"

安装完毕后显示返回0

另一个示例

#!/bin/bash

package=notexist
sudo apt install $package

echo "The exit code for ....is $?"
#执行后显示
#Reading package lists... Done
#Building dependency tree... Done
#Reading state information... Done
#E: Unable to locate package notexist
#The exit code for ....is 100

配合if语句 #

基本功能 #

#!/bin/bash

package=htop
sudo apt install $package

if [ $? -eq 0 ]
then
    echo "The installation of $package success..."
    echo "new comman here:"
    which $package
else
    echo "$package failed ..."
fi 

之前前作者用sudo apt remove htop又把htop删除了,不过其实不删除也是走的 echo "The installation of ....."这个分支
结果:

...

05If

在shell中,零为真,非零为假。

if then fi #

mynum=200

#[和]前后都要有空格
if [ $mynum -eq 200 ]
then
    echo "The condition is true."
fi


编辑之后,按ctrl + O 保存文件
ctrl + T + Z 保持在后台,fg+回车 恢复

#!/bin/bash

mynum=200

#[和]前后都要有空格
if [ $mynum -eq 200 ]
then
    echo "The condition is true."
fi

if [ $mynum -eq 300 ]
then
    echo "The variable does not equal 200."
fi

else if #

#!/bin/bash

mynum=300

#[和]前后都要有空格
if [ $mynum -eq 200 ]
then
    echo "The condition is true."
else
    echo "The variable does not equal>
fi

...

01-04

意义 #

执行一系列命令

视频框架 #

  1. 介绍,欢迎
  2. HelloWorld
  3. 变量
  4. 数学函数
  5. if语句
  6. 退出代码
  7. while循环
  8. 更新脚本,保持服务器最新状态
  9. for循环
  10. 脚本应该存储在文件系统哪个位置
  11. 数据流,标准输入、标准输出、标准错误输出
  12. 函数
  13. case语句
  14. 调度作业(SchedulingJobs)Part1
  15. 调度作业(SchedulingJobs)Part2
  16. 传递参数
  17. 备份脚本

准备 #

需要一台运行Linux系统的计算机(或虚拟机)

一些基本操作 #

新建或编辑脚本 #

nano myscript.sh

内容 #


ctrl + o 保存,ctrl + x 退出

如何执行脚本 #

权限 #

#给脚本赋予执行的权限
sudo chmod +x myscript.sh

执行 #

执行前查看权限 #

运行 #

./myscript.sh

查看脚本 #

cat myscript.sh

更多语句的脚本 #

ls
pwd

输出

...