学习

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

输出

...

基础

环境 #

  • 使用视频作者给出的示例,https://github.com/CoreyMSchafer/code_snippets/tree/master/Regular-Expressions
  • 使用sublimeText打开的文件,ctrl+f时要确认勾选正则及区分大小写

simple.txt-基础操作 #

直接搜索 #

任意字符 #


这里默认不会显示所有,点击findAll才会出来

有些字符需要加反斜杠转义,比如 . (点)以及 \ (斜杠本身) #

/////,从左到右,和书写方向一致的叫做(正)斜杠。
反之,叫做反斜杠 \


一些元字符 #

.       - Any Character Except New Line 除了换行符的任意字符
\d      - Digit (0-9) 数字
\D      - Not a Digit (0-9) 非数字
\w      - Word Character (a-z, A-Z, 0-9, _) 单词字符,大小写字母+数字+下划线
\W      - Not a Word Character 非单词字符
\s      - Whitespace (space, tab, newline) 空白字符,空格+tab+换行符
\S      - Not Whitespace (space, tab, newline) 非空白字符

\b      - Word Boundary 边界字符-单词边界
\B      - Not a Word Boundary 非单词边界(没有单词边界)
^       - Beginning of a String
$       - End of a String

[]      - Matches Characters in brackets
[^ ]    - Matches Characters NOT in brackets
|       - Either Or
( )     - Group

Quantifiers:
*       - 0 or More
+       - 1 or More
?       - 0 or One
{3}     - Exact Number
{3,4}   - Range of Numbers (Minimum, Maximum)


#### Sample Regexs ####

[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+

边界字符 #

...

使用PaperMode

地址 #

官方: https://github.com/adityatelange/hugo-PaperMod/wiki/Installation (有些东西没有同hugo官方同步) 非官方: https://github.com/vanitysys28/hugo-papermod-wiki/blob/master/Home.md (与hugo官方更同步)

安装 #

hugo new site blog.source --format yaml
cd blog.source
git init
git submodule add --depth=1 https://github.com/adityatelange/hugo-PaperMod.git themes/PaperMod 
git submodule update --init --recursive # needed when you reclone your repo (submodules may not get cloned automatically) 
git submodule update --remote --merge

hugo进阶学习20-23

DateFiles #

{% raw %} 
{
  "classA":"json位置: data\\classes.json",
  "classA":{
    "master":"xiaoLi",
    "number":"05"
  },
  "classB":{
    "master":"aXiang",
    "number":"15"
  },
  "classC":{
    "master":"BaoCeng",
    "number":"20"
  }
}  
{% endraw %} 

模板代码

{% raw %} 
{{/* layouts\_default\single.html */}}
{{ define "main" }}
     {{ range .Site.Data.classes }}
          master:{{.master}}==number:{{.number}}<br>
     {{end}}
{{end}} 
{% endraw %} 

PartialTemplates #

传递全局范围 #

{% raw %} 
{{/*layouts\partials\header.html*/}}
<h1>{{.Title}}</h1>
<p>{{.Date}}</p> 
{% endraw %} 
{% raw %} 
{{/*layouts\_default\single.html*/}}
{{ define "main" }}
  {{ partial "header" . }}
  {{/*点.传递了当前文件的范围,代表了所有的范围,所有可以访问的变量*/}}
  <hr>
{{end}} 
{% endraw %} 

预览:

...

hugo进阶学习17-19

Variable #

文件结构 #

实战 #

{% raw %} 
{{/*layouts\_default\single.html*/}}
{{ define "main" }}
  This is the single template<br>
  {{/* 常见变量 */}}
   title: {{ .Params.title }}<br>
   title: {{   .Title }}<br>
   date: {{ .Date }}<br>
  url: {{ .URL }}<br>
  myvar: {{ .Params.myVar }}<br>
  {{/* 定义变量 */}}
  {{ $myVarname := "aString" }}
  myVarname:{{ $myVarname }}<br>
  <h1 style="color: {{ .Params.color }} ;" >Single Template</h1>
{{ end }}  
{% endraw %} 
{% raw %} 
---
title: "E-title"
date: 2024-12-07T12:43:21+08:00
draft: true
myVar: "myvalue"
color: "red"
---

This is dir3/e.md 
{% endraw %} 

...

hugo进阶学习11-15

这里使用的版本是v0.26(很久之前的版本)

template basic #

模板分为list template和single template

文件夹结构 #

content目录结构

list template (列表模板) #

single template (单页模板) #

特点 #

所有的列表之间都是长一样的(页眉,页脚,及内容(都是列表))
所有的单页之间都是长一样的(一样的页眉页脚,一样的内容布局)

部分代码解释 #

单页探索 #


list page templates #

文件夹结构 #

文件内容 #

#content/_index
---
title: "_Index" 
---

This is the home page
#content/dir1/_index
---
title: "_Index"
---

This is the landing page for dir1

当前效果 #

...

hugo进阶学习01-10

系列视频地址介绍
https://www.youtube.com/watch?v=qtIqKaDlqXo&list=PLLAZ4kZ9dFpOnyRlyS-liKL5ReHDcj4G3

介绍 #

  • hugo是用来构建静态网站的
  • 但是也可以稍微做点动态生成的事
  • 这里使用的版本是v0.26(很久之前的版本)

备注:标题短代码之前(不包括短代码这篇)的笔记是回溯的,所以没有复制源代码下来,直接在视频再次截图的

在Windows上安装hugo #

  • 到github release下载,然后放到某个文件夹中
  • 设置环境变量
  • 验证环境变量
  • 最后验证hugo版本
hugo version

创建一个新的网站 #

  • 使用代码生成
hugo new site
  • 文件夹结构

使用主题 #

这里是https://themes.gohugo.io
这里使用的是ga-hugo-theme(github中查找),并放到themes文件夹中

之后在config.toml中使用主题

baseURL = "http://example.org/"
languageCode = "en-us"
title = "My New Hugo Site"
theme = "ga-hugo-theme" #添加这句话
  • 启动博客
    hugo serve
  • 地址
    localhost:1313

创建md文件 #

  • 使用hugo new a.md把文件创建在content/a.md或者hugo new dir2/d.md把文件创建在content/dir2.md下,这讲创建后的结构目录为
    • 总共5个文件,可以使用localhost:1313访问博客(默认列举所有(包括子文件夹)文件
    • 可以使用 localhost:1313/dir3访问dir3下所有文件列表(list),localhost:1313/dir1访问dir1下所有文件列表 (都是content的直接子文件夹)
    • 如果没有dir1/dir2/_index.md这个文件 ,则不能直接使用localhost:1313/dir1/dir2访问dir1/dir2下所有文件
    • 查看dir1/dir2/index.md文件及效果

frontmatter (前言) #

  • 可以使用YAML,TOML,或者JSON
  • md编码及效果

archetypes(原型) #

默认的原型文件 #

archetypes/default.md

...