缺失的计算机课程YouTube课程

cs
Author

dd21

Published

December 5, 2022

课程的目的

一共有11节课程, 每节课有一个主题,带你熟悉计算机工具

class 1

what is shell

shell 就是一个接受命令,然后返回结果的一个工具,解析你的命令给计算机执行,然后将执行结果返回给用户 > how to use shell

shell的使用就像使用魔法一样,通过像魔法一样的单词来进行操作。

其实shell的操作也称不上是魔法,每个命令其实就是一个单独的可执行程序,能够有同的功能,这也得益于linux的开源精神,开源作者为Linux大家庭添砖加瓦

echo hello              # 将会在终端中输出一行hello
echo $PATH              # 将会在终端中输出系统环境变量每个变量用:隔开

install tldr tldr is cleaner than man

sudo apt-get install npm
sudo Jnpm install -g tldr

how to use tldr

tldr tail               # 然后显示描述和案例,非常简洁,非常奈斯

tail 将打印该./目录下的文件,然后打印最后一行, 然后输出到out.txt中

ls -l ./ |tail -n1 > out.txt

curl grep curl 和网络相关 grep 过滤信息

                                    # 过滤关键词 Date关键字这一行|裁剪用‘ ’,第2个裁剪单位
curl --head --silent www.google.com | grep -i Date| cut --delimiter=' ' f2

控制键盘led灯亮灭

# on
echo 1| sudo tee /sys/class/leds/input27::capslock/brightness
# off
echo 0| sudo tee /sys/class/leds/input27::capslock/brightness

xdg-open 能够使用适合的程序打开,html用浏览器打开,文件夹用文件管理器打开

xdg-open xxx.html
xdg-open aa.txt
...

class 2(shell script)

脚本语言的编写

单引号和双引号的作用是不一样的, 单引号不做解释,原样输出,双引号进行解释

foo=bar
echo "$foo"
# prints bar
echo '$foo'
# prints $foo

函数编写

函数名和脚本名一致, 编写完成执行source mcd.sh即可全局使用该脚本

mcd () {
    # -p表示可以添加子目录(可以套娃)
    mkdir -p "$1"
    # 进入到创建的目录
    cd "$1"
}

$ 的使用

$0 - 脚本的名字
$1 to $9 - 脚本的后边跟的参数
$@ - 所有参数
$# - 参数个数返回上一条命令的执行结果
$$ - 当前进行的id
$_ - 上次的执行的命令的最后一个参数
(base) root@VM-12-14-ubuntu:~/projects/cs_base/script# cat mcd.sh 
mcd(){
    #mkdir -p "$1"
    #cd "$1"

    # 所有参数
    echo "@=$@"
    # 参数个数
    echo "#=$#"
    # 返回上一条命令的结果
    echo "?=$?"
    # 返回当前脚本进程号
    echo '$$'"=$$"
    # 
    echo '$_'"=$_"
}
(base) root@VM-12-14-ubuntu:~/projects/cs_base/script# source mcd.sh 
(base) root@VM-12-14-ubuntu:~/projects/cs_base/script# mcd aaa bb cc dd
@=aaa bb cc dd
#=4
?=0
$$=901010
$_=$$=901010

!! - 上一条整条命令

(base) root@VM-12-14-ubuntu:~# echo hello
hello
(base) root@VM-12-14-ubuntu:~# !!
echo hello
hello
(base) root@VM-12-14-ubuntu:~# ls
create_variables.sh  mcd.sh
(base) root@VM-12-14-ubuntu:~# !!
ls
create_variables.sh  mcd.sh
(base) root@VM-12-14-ubuntu:~#

||&& || 第一个条件是否满足, 不满足执行后一个 && 两个条件同时满足才执行.

ll 左边参数 右边参数
条件 不执行
条件 × 执行
&& 左边参数 右边参数 是否执行
条件 执行
条件 × 不执行
false || echo "Oops, fail"
# Oops, fail

true || echo "Will not be printed"
#

true && echo "Things went well"
# Things went well

false && echo "Will not be printed"
#

true ; echo "This will always run"
# This will always run

false ; echo "This will always run"
# This will always run

命令参数的使用$(cmd)

a=$(pwd)
echo "we are now at:$a"

拼接不同进程的信息, CMD <(CMD) <(CMD)

 diff <(ls .) <(ls ../)
 ls <(ls .) <(ls ../)
 cat <(ls .) <(ls ../)
 ...

对比不同路径下文件的内容中是否包含hello关键字 注意$(cmd)是执行命令, $VALUE 是取值 [xxx][[xxx]]的区别,前者是旧版本,后者是新版本, 前者的适配性更好,但是后者的语法更适合人类编写,参考地址:[xxx]和[[xxx]]详细对比说明

#!/bin/bash    
echo "find_diff program is excute at:$(date)"    
    
echo "sh name is:$0, with $# arguements, with pid is: $$"    
    
# 遍历文件夹中的文件    
for file in "$@";do    
    # 过滤文件中的hello关键字, 输出到系统中垃圾倾倒的地方    
    echo "----------->$file"    
    grep hello  "$file" > /dev/null 2> /dev/null    
    
    # 如果上面这行执行结果不等于0 
    #if [[ $? -ne 0 ]]; then  
    if [ $? -ne 0 ]; then    
       echo "$file has no '1'"    
       # 在没有的文件后边追一个 # hello    
       echo "#hello" >> "$file"    
    fi    
done
$# 表示提供到shell脚本或者函数的参数总数。
$0 表示第一个参数。
-ne 表示不等于。
这是unix的shell编程语句,如果上一条命令执行后的结束代码不是0则执行下面的命令,知道fi行之前,例子:
who am i | grep root
if [ $? -ne 0 ]
then
echo 'hello'
fi
另外:
整数比较
-eq 等于,如:if ["$a" -eq "$b" ]
-ne 不等于,如:if ["$a" -ne "$b" ]
-gt 大于,如:if ["$a" -gt "$b" ]
-ge 大于等于,如:if ["$a" -ge "$b" ]
-lt 小于,如:if ["$a" -lt "$b" ]
-le 小于等于,如:if ["$a" -le "$b" ]
< 小于(需要双括号),如:(("$a" < "$b"))
<= 小于等于(需要双括号),如:(("$a" <= "$b"))
> 大于(需要双括号),如:(("$a" > "$b"))
>= 大于等于(需要双括号),如:(("$a" >= "$b"))

ls *.sh 命令过滤

参数扩展

convert hello.jpg hello.png
convert hello.{jpg,png} # 

touch hello.{png,jpg}
rm *.{jpg,png}

在这里插入图片描述在这里插入图片描述 > find 查找文件, 名字,日期(n天内..),类型(文件,文件夹), 大小(文件大小), 额外操作(删除,对比…)

find . -name src -type d
find ./{a,b} -path '*.py' -type f

1、-mtime n : n为数字,意思为在n天之前的“一天之内”被更改过内容的文件
2、-mtime +n : 列出在n天之前(不含n天本身)被更改过内容的文件名
3、-mtime -n : 列出在n天之内(含n天本身)被更改过内容的文件名

# Find all zip files with size in range 500k to 10M
find . -size +500k -size -10M -name '*.tar.gz'

# 删除
# Delete all files with .tmp extension
find . -name '*.tmp' -exec rm {} \;
# Find all PNG files and convert them to JPG
find . -name '*.png' -exec convert {} {}.jpg \;

在这里插入图片描述 >locate文件子串查找

许多时候我们并不知道文件的位置,只有一些关键字, 所以通过关键字全局查找显得非常必要(有点类似windows 的everything)

locate hello

grep文件内容查找

# 在指定文件内查找
grep hello xxx.sh
# 在当前目录递归查找hello
grep -r hello .

rg按照文件类型, 指定路径,搜索指定内容

rg "echo" -t sh ~/projects/cs_base/

# 获取前后2行(一共4行)内容
rg "echo" -t sh -C 4 ~/projects/cs_base/

ctrl+r搜索历史, 继续按可以继续向前翻符合的命令 history展示全部命令

fzf文件内容查找

cat hello|fzf

在这里插入图片描述

broot nnn # class 3 # class 4 # class 5 # class 6 # class 7 # class 8 # class 9 # class 10 # class 11 # 答疑