常用shell命令

终端

删除换行符

可以用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文件

命令行 JSON 处理工具 jq 的使用介绍
在线测试

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值