Linux

_TheLinuxCommandsHandbook_

《TheLinuxCommandsHandbook》结合视频 https://www.youtube.com/watch?v=ZtqBQ68cfJc 看的

Command意义 #

更快,自动化,在任何Linux上工作,有些工作的基本需求

系统-Unix和Windows #

绿色-开源
红色-闭源
黄色-混合

图片中的Linux只是类Unix,而不是真正的Unix

FreeSoftware,开源 #

GNU与Linux

Linux只是一个操作系统内核而已,而GNU提供了大量的自由软件来丰富在其之上各种应用程序。
绝大多数基于Linux内核的操作系统使用了大量的GNU软件,包括了一个shell程序、工具、程序库、编译器及工具,还有许多其他程序
我们常说的Linux,准确地来讲,应该是叫“GNU/Linux”。虽然,我们没有为GNU和Linux的开发做出什么贡献,但是我们可以为GNU和Linux的宣传和应用做出微薄的努力,至少我们能够准确地去向其他人解释清楚GNU、Linux以及GNU/Linux之间的区别。让我们一起为GNU/Linux的推广贡献出自己的力量!

内核,用来连接硬件和软件的

TrueUNIX #

Unix一开始是收费的,后面出现Unix-like(类Unix),和Unix标准兼容。
Linux不是真正的Unix,而是类Unix。
Linux本身只是一个内核,连接硬件软件
LinuxDistributions,Linux发行版(1000多种)
Linux内核是一些GUN工具,文档,包管理器,桌面环境窗口管理,和系统一些其他东西组成的一个系统

有开源的和不开源的,Linux(LinuxGUN)完全开源

shell #

windows(powershell)
命令交给系统
terminal(最古老时是一个硬件)–屏幕+带键盘的物理设备,如今是一个软件
默认情况下,Ubuntu和大多数Linux发行版bashshell,还有zsh

setup and installing #

如果有Mac或者其他Linux发行版,则不需要额外操作。(作者在Mac里装了Ubuntu虚拟机)

WindowsSubsystem #

wsl --install
默认是Ubuntu

The Linux Handbook(电子书内容) #

Linux 手册

Preface #

前言

The Linux Handbook follows the 80/20 rule: learn in 20% of the time the 80% of a topic.
Linux 手册遵循 80/20 规则:用 20% 的时间学习某个主题的 80%。

...

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

输出

...