naiguo
naiguo
发布于 2025-10-29 / 18 阅读
0
0

常用Bash脚本

umask限定文件权限

# linux以安全考虑,加了umask,限定程序设定权限时,避免过于开发,一般默认是022,限定的是非所有者的写权限
umask
# 程序中,需要显示修改umask,否则指定的权限字会与这里异或而被屏蔽非所有者的写权限,例如C语言,应该先清空
mode_t old_mask = umask(0);   // 清空,并保存
int fd = shm_open(filepath,O_CREAT |O_EXCL | O_RDWR,0666);
umask(old_mask);              // 恢复

批量文件改名

# 将指定文件的文件名old部分改为new
rename 's/old/new/' *.txt
# 添加前缀
rename 's/^/prefix_/' *.jpg
# 用正则表达式
rename 's/(.*)\.txt/$1_20240131.txt/' *.txt

创建PostgreSQL内存表

# 创建专用内存目录
mkdir /dev/shm/pg_unlogged
chown -R postgres:postgres /dev/shm/pg_unlogged
chmod 700 /dev/shm/pg_unlogged

# 进入psql执行表空间创建
CREATE TABLESPACE your_mem_space LOCATION '/dev/shm/pg_unlogged';
SELECT * FROM pg_tablespace WHERE spcname = 'your_mem_space ';

# 创建unlogged表


# 创建基于内存空间的unlogged表

安装与配置postgraphile

问AI难以成功,还是要看官网 https://www.graphile.org/postgraphile/live-queries/

# 基础库
nmp install postgraphile
# 过滤条件 :支持丰富的比较条件,如in、>,<,like
sudo npm install -g postgraphile-plugin-connection-filter
# WAL : 推送
yarn add @graphile/subscriptions-lds

# 修改db配置文件 vi /etc/postgresql/15/main/postgresql.conf
wal_level = logical
max_wal_senders = 10
max_replication_slots = 10

# postgresql创建槽位
SELECT pg_create_logical_replication_slot('graphql_slot', 'wal2json');
# 安装对应组件
sudo apt-get install postgresql-15-wal2json
# 检查是否安装成功
ls $(pg_config --pkglibdir)/wal2json.so

# nvm安装的postgraphile配置系统服务启动应该类似如下
cat /etc/systemd/system/postgraphile.service
[Unit]
Description=PostGraphile GraphQL API Service
After=network.target postgresql.service
Wants=postgresql.service

[Service]
Type=simple
User=root
Group=root
WorkingDirectory=/root/
Environment="NVM_DIR=/root/.nvm"
Environment="NODE_VERSION=v24.13.0"
# 核心启动命令(注释单独行,参数行无行内注释)
ExecStart= /bin/bash -c 'source $NVM_DIR/nvm.sh && nvm use $NODE_VERSION && postgraphile \
  -c "postgres://postgres:szfb123456@localhost:5432/db_name" \
  --host 0.0.0.0 \
  --port 5032 \
  --watch \
  --graphiql /graphiql \
  --subscriptions \
  --append-plugins postgraphile-plugin-connection-filter '

postgraphile   -c "postgres://postgres:szfb123456@localhost:5432/beemarkets_test"   --host 0.0.0.0   --port 5030   --graphiql /graphiql   --enhance-graphiql --live   --subscriptions   --append-plugins @graphile/subscriptions-lds,postgraphile-plugin-connection-filter

# 加载并重启
sudo systemctl daemon-reload
systemctl restart postgraphile

# 进程崩溃后自动重启
Restart=always
RestartSec=3
# 日志配置(输出到系统日志)
StandardOutput=journal+console
StandardError=journal+console

[Install]
WantedBy=multi-user.target


# 看工作日志
sudo journalctl -fu postgraphile

安装nvm

ubuntu下使用nvm管理nodejs。

# 更新系统包列表
sudo apt update

# 安装curl和git(如果未安装)
sudo apt install -y curl git

# 安装最新版本的nvm(推荐)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

# 若访问GitHub缓慢,可使用国内镜像(可选)
# curl -o- https://gitee.com/mirrors/nvm/raw/v0.39.7/install.sh | bash

# 查看nvm版本
nvm --version

# 正常输出示例:0.39.7

# 1. 查看可安装的Node.js版本
nvm ls-remote

# 2. 安装指定版本的Node.js(如最新LTS版)
nvm install --lts  # 安装长期支持版
# 或安装具体版本
nvm install 20.10.0

增加sudoers

sudo usermod -aG sudo <用户名>

Grep查找

#查找指定路径下的所有特定扩展名文件,是否包含内容
grep -rnw --include=\*.{cpp,h} certain_content ./

JSON格式化

sudo apt update && sudo apt install -y jq

安装网络工具

# 安装net-tools(包含netstat命令)
sudo apt update && sudo apt install -y net-tools

CUrl常用

curl --resolve www.google.com:443:142.250.185.100 https://www.google.com/

安装Python环境

基于pyenv管理环境

# 安装pyenv
sudo apt update
sudo apt install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev \
python3-openssl git

git clone https://github.com/pyenv/pyenv.git ~/.pyenv

# 若用bash(Ubuntu默认):
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc

# 若用zsh:
# echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
# echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
# echo 'eval "$(pyenv init -)"' >> ~/.zshrc

# 生效环境变量
source ~/.bashrc  # zsh用户执行 source ~/.zshrc

# 查看可安装的Python版本
pyenv install --list

# 安装python
pyenv install 3.12.0

# 全局生效(整个系统默认用3.12.0)
pyenv global 3.12.0

# 仅当前目录生效(推荐,避免影响系统)
# pyenv local 3.12.0

# 验证切换结果
python --version  # 输出Python 3.12.0
pip --version     # 输出pip对应版本

# 升级pip
pip install --upgrade pip

# 更换国内源(加速安装包)
# 临时使用
pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple

# 永久配置(创建pip配置文件)
mkdir -p ~/.config/pip
echo '[global]' >> ~/.config/pip/pip.conf
echo 'index-url = https://pypi.tuna.tsinghua.edu.cn/simple' >> ~/.config/pip/pip.conf

运行指定目录的Web服务

# 进入要分享的目录
cd /path/to/directory

# 启动 HTTP 服务(默认端口 8000)
python3 -m http.server 8000

# 或使用特定端口
python3 -m http.server 9000

安装PostgreSQL

# 更新系统包
sudo apt update && sudo apt upgrade -y

# 安装添加软件源所需的依赖
sudo apt install -y wget ca-certificates

# 添加 GPG 密钥(验证包完整性)
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

# 添加官方软件源到系统
echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list

# 再次更新源(加载新增的 PostgreSQL 源)
sudo apt update

# 安装 PostgreSQL 15 及客户端工具
sudo apt install -y postgresql-15 postgresql-client-15

# 查看服务状态(正常应显示 active (running))
sudo systemctl status postgresql

# 查看 PostgreSQL 版本
psql --version

配置密码

# 切换到 postgres 系统用户
sudo su - postgres

# 登录 PostgreSQL 命令行
psql

ALTER USER postgres WITH PASSWORD 'szfb123456';

\q

允许远程连接

# 编辑主配置文件
sudo vim /etc/postgresql/15/main/postgresql.conf

# 找到 listen_addresses,修改为 *(允许所有IP访问)
listen_addresses = '*'

# 保存并退出(Ctrl+O → 回车 → Ctrl+X)

# 编辑访问控制文件
sudo vim /etc/postgresql/15/main/pg_hba.conf

# 在文件末尾添加(允许所有IP通过密码访问,根据需求调整)
host    all             all             0.0.0.0/0               scram-sha-256

# 重启 PostgreSQL 使配置生效
sudo systemctl restart postgresql

gcc编译环境

# 第一步:更新系统包列表(确保下载最新版本)
sudo apt update

# 第二步:安装 build-essential(包含gcc、g++、make等)
sudo apt install -y build-essential

# 检查 GCC 版本(显示版本号即安装成功)
gcc --version

# 检查 G++ 版本(C++ 编译工具)
g++ --version

# 检查 make 版本
make --version

# 安装 libc 开发库(C 标准库头文件)
sudo apt install -y libc6-dev

# 安装 pkg-config(管理编译选项)
sudo apt install -y pkg-config

# 安装调试工具(gdb)
sudo apt install -y gdb

# 安装 CMake + Ninja(核心命令)
sudo apt install -y cmake ninja-build

git常规操作

# ubuntu下安装git
apt install git

# 清除缓存,当存储的授权信息有误时
git credential-cache exit

# 设定缓存时间
git config --global credential.helper 'cache --timeout=360000'

# 验证配置
git config --get credential.helper

# 首次clone仓库
git clone https://gitee.cn/wapuboy/nfls.git

base64编解码

# 解码
echo "YWFhIAo=" |base64 -d
# 编码
echo "aaa " |base64 

Json的格式化输出

cat ./mydat/test.json |jq -C
jq -C . mydat/test.json

增加文件句柄

# 将单个进程的最大文件句柄数临时设为65536
ulimit -n 65536

打开Core文件

ubuntu下打开core文件。

$ sudo vim /etc/security/limits.conf
kernel.core_uses_pid = 1
kernel.core_pattern = /var/core/core_%e_%p_%t  # 自定义 core 文件路径和格式

$ sudo vim /etc/security/limits.conf
* soft core unlimited
* hard core unlimited

文件格式转换

获取时间

date +"%Y-%m-%d %H:%M:%S.%N"

从GB2312到UTF8

find ./THDSM -name "*.h" | while read file; do
    filename=$(basename "$file")
    echo "正在转换: $filename"
    iconv -f GB2312 -t UTF-8 "$file" > "./converted/$filename"
done
echo "所有文件转换完成!"


评论