用户工具

站点工具


bash

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录前一修订版
后一修订版
前一修订版
bash [2020/03/10 15:29] – [字符串大小写转换] ploughbash [2022/02/18 11:29] (当前版本) – [判断端口是否打开] plough
行 1: 行 1:
 +====== bash 常用命令 ======
 +===== 外部文档 =====
 +[[http://c.biancheng.net/cpp/view/2739.html|Shell特殊变量:Shell $0, $#, $*, $@, $?, $$和命令行参数]]
 ======bash 使用心得====== ======bash 使用心得======
 =====复制文件夹中前N个文件===== =====复制文件夹中前N个文件=====
行 55: 行 58:
 </code> </code>
 ===== 获取当前脚本所在目录 ===== ===== 获取当前脚本所在目录 =====
-<code> +<code>                                                                
-set -ex                                                                     +
 dir=`dirname $0` dir=`dirname $0`
 script_dir=`readlink -f $dir/` script_dir=`readlink -f $dir/`
 +</code>
 +
 +或者
 +<code>
 +script_dir=$(cd `dirname $0`; pwd)
 </code> </code>
 ===== 替换文本文件中的内容 ===== ===== 替换文本文件中的内容 =====
行 122: 行 129:
 </code> </code>
  
-===== 当前脚本所在路径 =====+===== 判断变量是否为空 =====
 <code> <code>
-dir=`dirname $0+#!/bin/bash 
-script_dir=`readlink -f $dir/`+ 
 +function is_empty() { 
 +    if [ -z "$1" ]; then 
 +        return 1 
 +    fi 
 +    return 0 
 +
 + 
 +function is_empty_ignore_space() { 
 +    if [ -z "${1// }" ]; then 
 +        return 1 
 +    fi 
 +    return 0 
 +
 + 
 +is_empty '' 
 +echo $? # 1 
 +is_empty_ignore_space '' 
 +echo $? # 1 
 +is_empty '   ' 
 +echo $? # 0 
 +is_empty_ignore_space '   ' 
 +echo $? # 1 
 +</code> 
 + 
 +===== 获取正在运行的 pod ===== 
 +<code> 
 +kubectl get pods|tail -n +2|awk '{print $1}' 
 +</code> 
 + 
 +===== 关闭正在运行的 k8s 服务 ===== 
 +<code> 
 +kubectl delete sts `kubectl get sts|tail -n +2|awk '{print $1}'
 +kubectl delete deployment `kubectl get deployments|tail -n +2|awk '{print $1}'
 +kubectl delete service `kubectl get services|tail -n +2|awk '{print $1}'
 +</code> 
 + 
 +===== 判断指定 git 分支是否存在 ===== 
 +<code> 
 + 
 +</code> 
 + 
 +===== 目录下搜索文件内容 ===== 
 +类似于 ack 的效果。 
 +<code> 
 +find . -type f|xargs grep "10.10.25.107" 
 +</code> 
 + 
 +===== 判断端口是否打开 ===== 
 +nc -z <host> <port>,然后检查 $? 的值,为 1 是打开的,为 0 则是关闭的。例如: 
 +<code> 
 +if [ !$(nc -z 0.0.0.0 27018) ]; then 
 +    kubectl port-forward service/mongo --address 0.0.0.0 27018:27018 & 
 +fi 
 +</code> 
 + 
 +===== 查找文件并执行命令 ===== 
 +<code> 
 +find . -name '*.config.js' -exec echo 123{} \; 
 +# 查找并删除 
 +find . -name '*Pulished*' -exec rm {} \;
 </code> </code>
bash.1583825378.txt.gz · 最后更改: 2020/03/10 15:29 由 plough

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki