shell的使用

shell fuction

函数定义

  • 定义1
xxx.sh
1
2
3
function funcName() {
# do sth
}
  • 定义2
xxx.sh
1
2
3
4
f2() {
value=$(($1+$2+1))
echo $1 "+" $2 "=" $value
}

传参

xxx.sh
1
2
3
4
function funcName() {
echo $1 # 打印第一个参数
echo $2 # 打印第二个参数
}

调用

  • 直接在脚本中调用
xxx.sh
1
2
3
4
5
function funcName() {
echo $1 # 打印第一个参数
echo $2 # 打印第二个参数
}
funcName 刘喵喵 大帅哥
  • 在shell中调用
shell
1
2
. xxx.sh # import导入其中的函数
funcName 刘喵喵 大帅哥

如果function写在/etc/profile~/.bashrc下,则可以直接调用

应用

  • 有了以上方法,在bashrc中将自己常用但容易遗忘的命令写成函数,就可以方便的调用了
  • 如果函数忘了,,那就可以去~/.bashrc下看看😅x1
  • 如果忘记去哪里看了,,,那就来看看这篇文章😅x2
  • 如果忘记这篇文章,,,那我直接😅x3
~/.bashrc
1
2
3
4
5
6
7
8
function setgitproxy() {
git config --global http.proxy 'socks5h://localhost:7890'
git config --global https.proxy 'socks5h://localhost:7890'
}
function unsetgitproxy() {
git config --global --unset http.proxy
git config --global --unset https.proxy
}

ubuntu中创建unit

systemctl

systemctl 提供了一组子命令来管理单个的 unit,其命令格式为:

1
systemctl [command] [unit]

创建unit

  • 编写.service文件
1
sudo vim /etc/systemd/system/xxx.service
xxx.service
1
2
3
4
5
6
7
8
9
10
11
[Unit]
Description=clash daemon

[Service]
Type=simple
User=root
ExecStart=/opt/xxx/xxx -d /etc/xxx/ #start时执行的命令
Restart=on-failure

[Install]
WantedBy=multi-user.target
  • 重新加载systemctl daemon
1
sudo systemctl daemon-reload
  • 启动service
1
sudo systemctl start xxx.service
  • 设置为开机启动
1
sudo systemctl enable xxx.service
作者

Meow Meow Liu

发布于

2022-10-27

更新于

2024-04-23

许可协议

评论