bash 综合例子
1 实用脚本
看无注释的配置文件
统计英语单词次数
处理空行空格
指定行号,正则匹配,区间,非!,最后一行$
# 打印第5行
sed -n '5p' example.txt
# 打印5-10行
sed -n '5,10p' example.txt
# 删除 第2-4行, 7-10行
seq 1 15 |sed '2,4d;7,+3d'
# 打印不包含hello的行, I表示 忽略大小写, 要放到!的前面
sed -En '/hello/I!p' example.txt
# 首行不打印
sed -n '1!p' example.txt
# 打印最后一行
sed -n '$p' example.txt
# 匹配包含python的行到 包含java的行
# macos和linux下的都会匹配上,2个区间
sed -n '/python/,/java/p' <<EOF
macos
python url1
golang url2
java url3
linux
python url4
golang url5
java url6
EOF
# 只打印macos下的 ,可以这样
sed -n '/^macos/,/^linux/{/python/,/java/p}' <<EOF
macos
python url1
golang url2
java url3
linux
python url4
golang url5
java url6
EOF整理中...
2 python conda 一键安装脚本
Caution
这里只支持ubuntu和mac, 我只暂时测试了 ubuntu 下的安装,后续会再优化测试.
os=$(uname -s|tr '[:upper:]' '[:lower:]')
install_python(){
#/Linux installers/,/Installing/ 表示找 这2个之间的文本
if [ $os == "linux" ];then
os_begin="Linux installers"
os_end="Installing"
else
os_begin="macOS installers"
os_end="Linux installers"
fi
pyversion=($(curl -Ss https://docs.conda.io/en/latest/miniconda.html | sed -n '/'"${os_begin}"'/,/'"${os_end}"'/s#.*<td>Python \([0-9.]*\)</td>#\1#p'))
echo "选择你的python版本"
for j in ${!pyversion[@]}
do
echo $(expr $j + 1): ${pyversion[$j]}
done
echo -n "请输入编号:"
read python_version_no
# 将. 转义
python_version=${pyversion[$(expr $python_version_no-1)]/./\\.}
python_next_version=${pyversion[$python_version_no]/./\\.}
between_begin="Python "${python_version}
between_end="Python "${python_next_version}
if [ -z ${python_next_version} ];then
between_end="Installing"
fi
# 使用//,//{//,//} 来范围内找范围
cpuverurl=($(curl -Ss https://docs.conda.io/en/latest/miniconda.html | sed -n '/'"${os_begin}"'/,/'"${os_end}"'/{/'"${between_begin}"'/,/'"${between_end}"'/s/.* class="reference external" href="\(.*Linux-\(.*\)\.sh\)">.*/\2 \1/p}'))
declare conda_url
declare cpu_ver
echo "选择你的cpu架构"
for j in ${!cpuverurl[@]}
do
if [ $(($j%2)) == 0 ];then
i=$(expr $j / 2 + 1)
echo $i: ${cpuverurl[$j]}
cpu_ver[$i]=${cpuverurl[$j]}
conda_url[$i]=${cpuverurl[$(expr $j + 1)]}
fi
done
echo -n "输入编号:"
read chosen_cpu
echo "你选择的是:" ${cpu_ver[${chosen_cpu}]}
miniconda_url=${conda_url[${chosen_cpu}]}
execfile=${conda_url[${chosen_cpu}]##*/}
wget ${miniconda_url}
chmod +x $execfile
# -p 安装的目录.
bash $execfile -b -p $HOME/miniconda
$HOME/miniconda/bin/conda init zsh
. ~/.zshrc
# 如果你在bash 环境下 安装的. 想要用zsh
# conda init zsh
# 在zsh 环境下安装 默认就会修改.zshrc的.
}
install_python3 go 一键安装脚本
os=$(uname -s|tr '[:upper:]' '[:lower:]')
install_go(){
echo "暂时只写了 amd64版本的下载."
echo " 输入你要的go版本"
curl -Ss https://golang.google.cn/dl/ | sed -n 's/.*class="toggle" id="go\([.0-9]*\)">/\1/p'| head -20
read go_version
wget https://golang.google.cn/dl/go${go_version}.${os}-amd64.tar.gz
# 这里我直接删除了旧的, 后续再完善 , 可以用 软链接之类的.
rm -rf /usr/local/go && tar -C /usr/local -xzf go${go_version}.${os}-amd64.tar.gz
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.zshrc
. ~/.zshrc
go env -w GOPROXY=https://goproxy.cn,http://mirrors.aliyun.com/goproxy/,https://goproxy.io,direct
}
install_go