一 Shell

1.1 函数

  • 关闭多余打印
    1. #!/bin/bash
  • 获取文件名和路径
    str=/home/luna/Desktop/Software/softHLA/HLAreporter.v103/HLAreporter.sh
    file=$(basename $str)
    
  • shell获取文件扩展名(前缀,后缀) ```shell

    !/bin/bash

    filename=rongtao.tar.gz echo “${filename%%.}” echo “${filename%.}” echo “${filename#.}” echo “${filename##.}”

运行结果:

rongtao rongtao.tar tar.gz gz



- 
字符串替换
```shell
%x=abcdabcd
%echo ${x/a/b} # 只替换一个
bbcdabcd
%echo ${x//a/b} # 替换所有
bbcdbbcd
  • 判断文件是否存在
    if [ ! -f "jar.sh" ];
    then echo "yes"
    else echo "no"
    fi
    
  • 判断变量是否为空
    para1=
    if [ ! $para1 ]; then
    echo "IS NULL"
    else
    echo "NOT NULL"
    fi
    
  • 判断重命名 ```shell temp_os=${os} echo —- $temp_os

if [ “$temp_os” = “x86” ]; then temp_os=”32bit” elif [ “$temp_os” = “x64” ]; then temp_os=”64bit” elif [ “$temp_os” = “aarch64” ]; then temp_os=”aarch64” fi

echo ======= $temp_os




<a name="5bd470a9"></a>
## 1.2 方法

- 
读取用户输入
> ./run.sh


```shell
read -p "username : " user_var
echo "username : " $user_var
  • 方法参数

    ./run.sh

function add(){
  echo $1 "--" $2
}
add aaa "ad min"
  • 遍历文件夹

    ./run.sh

#文件路径
function main(){
    for file in /root/*
    do
        if test -f $file
        then
            echo $file 文件
        else
            echo $file 文件夹
        fi
    done
}
main
  • 遍历数组
    #定义数组
    tables=(xxl_job ys_asset ys_cloud_scan ys_common)
    function main(){
      for table in ${tables[@]};do
          echo --------------------------------------$table
      done
    }
    main
    
  • 脚本参数

    ./run.sh start

function help(){
    echo "Usage:"
    echo "    $0 [all|mysql|kafka|nginx|es|consul|canal|redis|qincai|ssk|api] [start|stop|restart]"
}
function main(){
        case $op in
            "start")
                kafka_start
                ;;
            "stop")
                kafka_stop
                ;;
            "restart")
                kafka_stop
                kafka_start
                ;;
            *)
                help
                exit
                ;;
        esac
}

op=$1
main
  • 脚本参数(指定字段)

    ./run.sh -v 1.0 -t dev

V=0
T=0
O=0
F=0

function get_options(){
        while getopts "v:t:o:f:" ARGS
        do
        case $ARGS in
            v) V=$OPTARG ;;
            t) T=$OPTARG ;;
            o) O=$OPTARG ;;
            f) F=$OPTARG ;;
            *) echo "未知选项:$ARGS" ;;
        esac
        done
}

function start(){
        echo $V
        echo $T
        echo $O
        echo $F
}

get_options $*
start

1.3 交互命令

  • expect

    ./run.sh

function gpull(){
  expect -c '
        set timeout 30
        spawn '"$1"'
        expect "Username" {send "123\\r"}
        expect "Password" {send "123\\r"}
        expect eof'
}

gpull "git pull"
  • sshpass

二 SSH

  1. 远程执行命令
ssh nick@xxx.xxx.xxx.xxx "df -h"
#多个命令之间用分号隔开
ssh nick@xxx.xxx.xxx.xxx "pwd; ls"
  1. 远程交互命令
    ssh -t nick@xxx.xxx.xxx.xxx "top" 
    #添加 -t 参数后,ssh 会保持登录状态,直到你退出需要交互的命令
    ssh -t nick@xxx.xxx.xxx.xxx -t "bash;"  
    #bash 关键
    
  1. 免密码
    sshpass -p 123 ssh -t nick@xxx.xxx.xxx.xxx "top"
    
  1. 引用变量
    name=zhangsan
    ssh root@10.41.224.66 "echo $name"
    
  1. 远程执行脚本
    #远程服务器新建脚本 test.sh
    pwd
    ls
    #赋权
    chmod 777 test.sh
    #远程执行
    ssh root@10.41.224.66 "/opt/test.sh"
    

三 测试

  1. 等待输入

    /root/read.sh

read -p 'name:' name
echo "name $name"
read -p 'age:' age
echo "age $age"
read -p 'addr:' addr
echo "addr $addr"

/root/test.sh

function gpull(){
  expect -c '
        set timeout -1
        spawn '"$1"'
        expect "name" {send "1\r"}
        expect "age" {send "2\r"}
        expect "addr" {send "'$2'\r"}
        expect eof'
}

gpull "/root/read.sh" "bejing"