终端
删除换行符
可以用tr
命令tr '\n' ' '
docker ps -aq |tr '\n' ' ' |xargs docker rm -f
录屏
安装
yum install -y gcc \
&& git clone https://github.com/mattwilmott/ttyrec.git \
&& cd ttyrec \
&& make \
&& cp ttyrec /usr/local/bin/ttyrec \
&& cd .. \
&& rm -rf ttyrec \
&& git clone https://github.com/icholy/ttygif.git \
&& cd ttygif \
&& make \
&& make install \
&& cd .. \
&& rm -rf ttygif
文本文件
不存在则插入
if [[ -z "$(grep 'net.ipv4.ip_forward=1' /etc/sysctl.conf)" ]]; then
echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf
fi
替换
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
注释
注意:中间都有一个空格
# &
,# \
,不是写错了,自行体会其作用
注释某一行
sed -i 's/^SELINUX=disabled/# &/' /etc/selinux/config
# SELINUX=disabled
取消注释某一行
sed -i 's/^# \(SELINUX=disabled\)/\1/' /etc/selinux/config
SELINUX=disabled
json文件
jq带有内置的操作符,如果key中有特殊字符,会报错jq: error: *** is not defined at <top-level>, line 1:
jq '."exec-opts"' /etc/docker/daemon.json
是否存在key
if [[ $(jq 'has("exec-opts")' /etc/docker/daemon.json) = true ]]; then
echo "exists"
else
echo "not exists"
fi
修改value,如果key不存在会追加。如果value是数组,会将整个数组丢掉,变成新值
jq不支持就地编辑在管道链中使用jq不会产生任何输出
cat /etc/docker/daemon.json | jq --unbuffered '."exec-opts"=["native.cgroupdriver=systemd"]' | tee /etc/docker/daemon.json
用户输入
命令行等待用户输入,并提供默认值
read -p "Public IPv4 address / hostname [$get_public_ip]: " public_ip
[[ -z "$ip_number" ]] && ip_number="1"
命令行等待用户输入,直到输入指定的值为止
until [[ -z "$protocol" || "$protocol" =~ ^[12]$ ]]; do
echo "$protocol: invalid selection."
read -p "Protocol [1]: " protocol
Press any key to continue
read -n1 -r -p "Press any key to continue..."
网络
下载脚本,并执行脚本
ruby -e "$(curl -fsSL xxx)"
bash -c "$(curl -fsSL xxx)"
curl --help
-f, --fail Fail silently (no output at all) on HTTP errors (H)
-s, --silent Silent mode (don’t output anything)
-S, --show-error Show error. With -s, make curl show errors when they occur
-L, --location Follow redirects (H)
获取出口IP
wget -4qO- "http://whatismyip.akamai.com/" || curl -4Ls "htyip.akamai.com/"
安装Python依赖时报ConnectTimeoutError超时错误
可以指定国内镜像进行加速https://pypi.mirrors.ustc.edu.cn/simple
pip install --index-url https://pypi.douban.com/simple pymysql
pip install --index-url https://pypi.douban.com/simple -r requirements