Skip to content

Commit

Permalink
add blogs of linux shell
Browse files Browse the repository at this point in the history
  • Loading branch information
hxf0223 committed May 11, 2024
1 parent 773c440 commit 9f3e3c9
Show file tree
Hide file tree
Showing 15 changed files with 265 additions and 0 deletions.
33 changes: 33 additions & 0 deletions _posts/2023-05-24-kill-signals-of-linux-system.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
title: kill信号不同分类的影响
date: 2023-05-24 +0800 # 2022-01-01 13:14:15 +0800 只写日期也行;不写秒也行;这样也行 2022-03-09T00:55:42+08:00
categories: [shell]
tags: [shell] # TAG names should always be lowercase

# 以下默认false
math: true
mermaid: true
# pin: true
---

# kill信号不同分类的影响

`kill -KILL`不同的是,`kill -INT -PID` 将通知被结束进程,等同于`Ctrl+C`
例如如果结束一个script,该script中同步启动了一个APP,使用`kill -INT -<PIDofScript>`可以同时将这个APP结束掉,`kill -KILL`则不行。

| 分类 | 信号 |
|------------------------------|-----------------------------------------------------------------|
| 程序不可捕获、阻塞或忽略的信号| SIGKILL, SIGSTOP |
| 不能恢复至默认动作的信号 | SIGILL, SIGTRAP |
| 默认会导致进程流产的信号 | SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGIOT, SIGQUIT, SIGSEGV, SIGTRAP, SIGXCPU, SIGXFSZ |
| 默认会导致进程退出的信号 | SIGALRM, SIGHUP, SIGINT, SIGKILL, SIGPIPE, SIGPOLL, SIGPROF, SIGSYS, SIGTERM, SIGUSR1, SIGUSR2, SIGVTALRM |
| 默认会导致进程停止的信号 | SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU |
| 默认进程忽略的信号 | SIGCHLD, SIGPWR, SIGURG, SIGWINCH |

[Linux 下的KILL函数的用法 - 拂 晓 - 博客园 (cnblogs.com)](https://www.cnblogs.com/leeming0222/articles/3994125.html)

如果`kill`不能结束掉,则尝试使用`pkill`

```bash
pkill -TERM -P <PID of script>
```
72 changes: 72 additions & 0 deletions _posts/2023-05-24-shell-args-and-special-symbols.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
title: shell参数特殊变量符号
date: 2023-05-24 +0800 # 2022-01-01 13:14:15 +0800 只写日期也行;不写秒也行;这样也行 2022-03-09T00:55:42+08:00
categories: [shell]
tags: [shell] # TAG names should always be lowercase

# 以下默认false
math: true
mermaid: true
# pin: true
---

## Shell 特殊参数解释

首先来看几个特殊变量:`$0`, `$#`, `$*`, `$@`, `$?`, `$$`, `$_`

```bash
#!/bin/bash
echo $0 # 当前脚本的文件名(间接运行时还包括绝对路径)。
echo $n # 传递给脚本或函数的参数。n 是一个数字,表示第几个参数。例如,第一个参数是 $1 。
echo $# # 传递给脚本或函数的参数个数。
echo $* # 传递给脚本或函数的所有参数。
echo $@ # 传递给脚本或函数的所有参数。被双引号 (" ") 包含时,与 $* 不同,下面将会讲到。
echo $? # 上个命令的退出状态,或函数的返回值。
echo $$ # 当前 Shell 进程 ID。对于 Shell 脚本,就是这些脚本所在的进程 ID。
echo $_ # 上一个命令的最后一个参数
echo $! # 后台运行的最后一个进程的 ID 号
```

执行结果如下:

```bash
$ ./test.sh test test1 test2 test3 test4

./test.sh # $0
# $n
5 # $#
test test1 test2 test3 test4 # $*
test test1 test2 test3 test4 # $@
0 # $?
12305 # $$
12305 # $_
# $!
```

### 参数中的双引号

`$*``$@` 都表示传递给函数或脚本的所有参数,不被双引号 ("") 包含时,都以 `$1""$2" … "$n` 的形式输出所有参数。

但是当它们被双引号 ("") 包含时,`"$*"` 会将所有的参数作为一个整体,以 `$1 $2 … $n` 的形式输出所有参数;`$@` 会将各个参数分开,以 `"$1""$2" … "$n"` 的形式输出所有参数。

## 传递参数方式一

采用 `$0`, `$1`, `$2` ... 等方式获取脚本命令行传入的参数,值得注意的是,`$0` 获取到的是脚本路径以及脚本名,后面按顺序获取参数,当参数超过10个时(包括10个),需要使用`${10}`, `${11}`....才能获取到参数。

```bash
#!/bin/bash
echo "脚本$0"
echo "第一个参数$1"
echo "第二个参数$2"
```

运行结果:

```bash
$ ./test.sh 1 2

#shell中将会输出:
脚本./test.sh
第一个参数1
第二个参数2
```
40 changes: 40 additions & 0 deletions _posts/2023-05-24-ubuntu-login-without-pwd.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: ubuntu免密登录
date: 2023-05-24 +0800 # 2022-01-01 13:14:15 +0800 只写日期也行;不写秒也行;这样也行 2022-03-09T00:55:42+08:00
categories: [shell]
tags: [shell] # TAG names should always be lowercase

# 以下默认false
math: true
mermaid: true
# pin: true
---

# ubuntu免密登录

## vscode

1. 在windows端

```bash
# 生成私钥及公钥文件
ssh-keygen -t RSA -C "[email protected]"
```

2. 在ubuntu端

拷贝`id_rsa.pub`文件到ubuntu系统上

```bash
cat id_rsa.pub >> ~/.ssh/authorized_keys
rm id_rsa.pub
```

3. vscode连接

安装上`vscode remote ssh`套件,左下角有连接图标,点击连接,初次梅有ssh_config文件会提示选择`ssh_config`文件路径,并填写`ssh_config`文件。

## mobarXterm

这步操作在windows上已生成私钥公钥,及ubuntu上生成`authorized_keys`文件之后。
在mobarXterm中新建连接到ubuntu的ssh session,并设置选项:Advanced SSH Settings -> Use private key,选择私钥文件即可。
2 changes: 2 additions & 0 deletions _posts/2023-05-5-clang-tidy-rule-file-sample-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mermaid: true
# pin: true
---

# clang-tidy rule file sample [1]

文件名:`.clang-tidy`
部分检查项目中文解释:[selfboot/ClangTidyChecks](https://github.com/selfboot/ClangTidyChecks)

Expand Down
2 changes: 2 additions & 0 deletions _posts/2023-05-5-clang-tidy-rule-file-sample-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mermaid: true
# pin: true
---

# clang-tidy rule file sample [2]

文件名:`.clang-tidy`
引用自 [cnl/.clang-tidy at main · johnmcfarlane/cnl (github.com)](https://github.com/johnmcfarlane/cnl/blob/main/.clang-tidy)

Expand Down
2 changes: 2 additions & 0 deletions _posts/2023-05-5-cpp-code-snippet.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mermaid: true
# pin: true
---

# cpp code snippet

## 1. 获取数组长度

```cpp
Expand Down
2 changes: 2 additions & 0 deletions _posts/2023-05-5-debug-combind-Python-and-C++.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mermaid: true
# pin: true
---

# Python与C++混合调试

vscode启动python与C++混合调试时,gdb需要管理员权限。
[Remote attach using non-root account would fail directly](https://github.com/microsoft/vscode-remote-release/issues/2053)

Expand Down
2 changes: 2 additions & 0 deletions _posts/2023-05-5-design-modes-for-all.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mermaid: true
# pin: true
---

# 写给大家看的设计模式

- [写给大家看的设计模式](https://juejin.cn/post/6844903491601694727)
- [图解TensorFlow源码](https://www.cnblogs.com/yao62995/p/5773578.html)

Expand Down
2 changes: 2 additions & 0 deletions _posts/2023-05-5-get-function-via-address.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mermaid: true
# pin: true
---

# 通过函数指针地址找到函数

```bash
# 通过地址找到函数声明
info symbol 0x7f0db14cf57e
Expand Down
4 changes: 4 additions & 0 deletions _posts/2023-05-5-good-cpp-repo-on-github.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ mermaid: true
# pin: true
---

# good cpp repo on github

## 1. oneTBB

Intel并行库 [oneTBB](https://github.com/oneapi-src/oneTBB)
包含[malloc](https://github.com/oneapi-src/oneTBB/tree/master/src/tbbmalloc)
26 changes: 26 additions & 0 deletions _posts/2023-06-05-Create_CompileDB_from_Makefile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: 从Makefile创建compile_commands.json
date: 2023-06-05 +0800 # 2022-01-01 13:14:15 +0800 只写日期也行;不写秒也行;这样也行 2022-03-09T00:55:42+08:00
categories: [shell]
tags: [shell] # TAG names should always be lowercase

# 以下默认false
math: true
mermaid: true
# pin: true
---

# 从Makefile创建compile_commands.json

## 操作步骤

```bash
# https://github.com/nickdiego/compiledb
pip install compiledb
```

使用方法:

```bash
compiledb make
```
55 changes: 55 additions & 0 deletions _posts/2023-06-10-Ubuntu-install-VNCServer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
title: Ubuntu 安装VNCServer及使用
date: 2023-06-05 +0800 # 2022-01-01 13:14:15 +0800 只写日期也行;不写秒也行;这样也行 2022-03-09T00:55:42+08:00
categories: [shell]
tags: [shell] # TAG names should always be lowercase

# 以下默认false
math: true
mermaid: true
# pin: true
---

## 1. 安装

```bash
sudo apt-get install gnome-panel
sudo apt-get install tightvncserver

# 创建端口
vncserver :1 -geometry 1920x1000 -depth 24

# 关闭端口
vncserver -kill :1

# 重新设置密码
vncpasswd

# 重启vncserver
vncserver :1

# 重启vncserver方式2
vncserver -geometry 1920x1080 :1

# 查看vncserver log
tail -f ~/.vnc/log_name.log
```

编辑配置文件 `.vnc/xstartup`

```bash
#!/bin/sh

unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
export XKL_XMODMAP_DISABLE=1
export XDG_CURRENT_DESKTOP="GNOME-Flashback:GNOME"
export XDG_MENU_PREFIX="gnome-flashback-"
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
#gnome-terminal &
#nautilus &
gnome-session --session=gnome-flashback-metacity --disable-acceleration-check &
```
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mermaid: true
# pin: true
---

# Ubuntu 22.04 安装 Qt5,以编译 VTK

## 1. install commands

```bash
Expand Down
19 changes: 19 additions & 0 deletions _posts/2023-07-02-clean-screen-and-reset.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: Linux 系统console使用命令清屏
date: 2023-07-02 +0800 # 2022-01-01 13:14:15 +0800 只写日期也行;不写秒也行;这样也行 2022-03-09T00:55:42+08:00
categories: [shell]
tags: [shell] # TAG names should always be lowercase

# 以下默认false
math: true
mermaid: true
# pin: true
---


```bash
echo -en "\e[H\e[J\e[3J"
```

## reference
* [clear command in Konsole](https://superuser.com/questions/122911/what-commands-can-i-use-to-reset-and-clear-my-terminal)
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mermaid: true
# pin: true
---

# C++ 17新功能: std::visit 和 std::variant 配合使用

## 1. `std::variant` (变体)

在很多编程场景中,我们经常遇到需要处理多种类型的情况。传统上,这可以通过多种方式来实现,例如使用 `union``void*` 指针,甚至使用一系列的 `if-else` 语句和类型转换。但这些方法通常都有各种缺点,如类型不安全、容易出错或难以维护。
Expand Down

0 comments on commit 9f3e3c9

Please sign in to comment.