# linux/mac 实用脚本

* [linux/mac 实用脚本](#linuxmac-实用脚本)
  * [知识](#知识)
  * [mysqldump](#mysqldump)
  * [部署ss服务器教程](#部署ss服务器教程)
  * [服务状态检测](#服务状态检测)
  * [ssl证书过期检测](#ssl证书过期检测)
  * [实用命令](#实用命令)

## 知识

`set -ex`: `-e`:命令失败后立即结束 `-x`:打印每条执行的命令

## mysqldump

[MySQL mysqldump数据导出详解](https://www.cnblogs.com/chenmh/p/5300370.html) 复制表，调用方式:`sh copy.sh "-h127.0.0.0 -uroot -P8306 -p123456 boss_develop"`

```bash
#!/bin/bash
#set -x
REMOTE_HOST=$1
echo REMOTE_HOST:$REMOTE_HOST


oldifs=$IFS
dump(){
# -C启用压缩 --skip-triggers 不导出触发器
mysqldump -h10.69.34.205 -uadmin -P9800 -pDc@20120225 --opt -C boss $1 | mysql $REMOTE_HOST
}
dtd(){
        IFS=$oldifs
        arr=($1)
        for table in ${arr[@]}
        do
            echo start dump $table
            dump $table
        done
        IFS=$'\n'
}

IFS=$'\n'

#crm_measurement_units  crm_department crm_role_extend crm_price_item_ref_measure_time crm_time_units
#crm_special_rules_item crm_special_rules_product_item_rel crm_special_rules_product_type_rel
#dw_manual_rule dw_slice_rule
#crm_account_director crm_employee crm_region_base

tables="
crm_product_type crm_product_line_group
crm_account crm_contract contract_account_release crm_product_type_relation contract_product_price
crm_price_definition crm_price_detail
crm_special_rules_discount crm_contract_sprules crm_contract_sprules_regions
crm_rule_use_log crm_rule_use_lock crm_manual_rule crm_manual_rule_lock
month_bill_locked month_bill_summary_new
"

for table in ${tables[@]}
do
        read -r -p $table" ? [Y/n] " input
        case $input in
                [yY][eE][sS]|[yY]) dtd "${table}" ;;
                *) echo "No";;
        esac
done

IFS=$oldifs

echo finish copy
read
```

## 部署ss服务器教程

[阿里云搭建SS代理超详细教程！！！ - 天涯小嘿嘿 - 博客园](https://www.cnblogs.com/tianhei/p/7428622.html) 脚本来源：[teddysun/shadowsocks\_install](https://github.com/teddysun/shadowsocks_install)

```bash
#安装
wget --no-check-certificate -O shadowsocks-all.sh https://raw.githubusercontent.com/teddysun/shadowsocks_install/master/shadowsocks-all.sh
chmod +x shadowsocks-all.sh
./shadowsocks-all.sh 2>&1 | tee shadowsocks-all.log

#卸载
./shadowsocks-all.sh uninstall
```

## 服务状态检测

```bash
#!/bin/sh

service="boss-api-8086 boss-server-8080 boss-activiti-8095 boss-ruleconvert-8090 boss-bill-8071 boss-sync-8100"

for service in ${service[@]}
do
    if test $(pgrep -f $service | wc -l)  -eq 0
    then
      echo "ERROR "$service" NOT RUNNING"
    else
      echo $service" is runing....."
    fi
done

echo "check process finish,any key to exit"
read
```

## ssl证书过期检测

```bash
#!/bin/bash
# 检测https证书有效
echo '开始检查 https证书有效期 '

# 先写域名内容到文件中，再读取文件检查证书是否过期了
# 先清空文件
echo '' > /tmp/https_list.txt 

# 这里替换为自己的检查的域名即可
echo 'www.baidu.com' >> /tmp/https_list.txt
echo 'www.bing.com' >> /tmp/https_list.txt
echo 'www.google.com' >> /tmp/https_list.txt

source /etc/profile

# 定义错误的域名
errorDominStr=""

while read line; do
    echo "====================================================================================="
    
    echo "当前检测的域名：" $line
    end_time=$(echo | timeout 1 openssl s_client -servername $line -connect $line:443 2>/dev/null | openssl x509 -noout -enddate 2>/dev/null | awk -F '=' '{print $2}' )
    ([ $? -ne 0 ] || [[ $end_time == '' ]]) && echo '该域名链接不上,跳到下一个域名' && continue
    
    end_times=`date -d "$end_time" +%s `
    current_times=`date -d "$(date -u '+%b %d %T %Y GMT') " +%s `
    
    let left_time=$end_times-$current_times
    days=`expr $left_time / 86400`
    echo "剩余天数: " $days
    
    [ $days -lt 60 ] && echo "https 证书有效期少于60天，存在风险"  && errorDominStr="$errorDominStr \n $line"
    
done < /tmp/https_list.txt

echo -e "准备过期的域名为： \n  $errorDominStr"

if [ "$errorDominStr" = "" ]  
then  
  echo "不包含准备过期的域名"  
else    
  echo "包含准备过期的域名" && exit 10  
fi   
echo "Good bye!"
exit 0
```

## 实用命令

获取父线程名称 [get the name of the caller script in bash script](https://stackoverflow.com/questions/20572934/get-the-name-of-the-caller-script-in-bash-script)

```bash
echo $(ps $PPID | tail -n 1 | awk "{print \$5}")
```

简单for循环curl测试下载速度/for循环写成一行 参考：[linux脚本](https://book.rizon.top/jiao-ben/shell/pages/-Lcei2xw9WsoJypglBXW#curl测试下载速度)

```bash
# 测速
for i in {1..10}; do echo $i">>>>"; curl -o /dev/null -s -w '%{time_connect}:%{time_starttransfer}:%{time_total}\n' 'https://lib.baomitu.com/jquery/3.3.1/jquery.min.js'; done

#其他示例
for i in {1..10}; do echo 'test'; done
for ((i=1;i<10;i++)); do echo 'test'; done
for i in $(seq 1 10); do ehco 'test'; done
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.rizon.top/jiao-ben/shell/shi-yong-jiao-ben.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
