SHELL进阶指令集合,如何写出高级的shell命令

shell

  1. shell输出时有颜色,正常输出是绿色,错误输出是红色
RED="\033[0;31m"
GREEN="\033[0;32m"
NO_COLOR="\033[0m"
die() {
    echo -e "${RED}${@}${NO_COLOR}"
    exit -1
}
good() {
    echo -e "${GREEN}${@}${NO_COLOR}"
}
  1. eval 执行长参数shell语句

  2. sysctl

# 永久生效方式:修改系统配置文件
sudo vim sysctl.conf
# 配置coredump产生配置,根据进程名区分不同的路径
kernel.core_pattern = /tmp/coredump/%e/core.%p
# 配置系统线程最大数量
kernel.threads-max = 50000

# 执行以下配置生效
sudo sysctl -p
# 临时生效方案
sudo echo /tmp/coredump/%e/core.%p > /proc/sys/kernel/core_pattern
sudo echo 50000 > /proc/sys/kernel/threads-max
  1. shell 详细模式
set -x
    shell...
set +x
  1. 判断某个某行包含某个子字符串
# 效率最低,每行都需要输出log,再通过管道进行进程间通信,再通过grep进行筛选,步骤太多
echo "$line_log" | grep -q "PASSED"

# 1 判断子字符串速度提升,提升10倍
${line_log} =~ "[  PASSED  ]"

# 2 判断子字符串速度提升,提升20%
${line_log} = *"[  PASSED  ]"*

# 查找方式提升,提升20倍
grep -q "\[  PASSED  \]" ${carconfig_log_file}

  1. 远程拷贝文件
# scp 从远端拷贝
    scp -o StrictHostKeyChecking=no root@10.204.0.1:/opt/zkos/platform/test.txt test.txt
# sftp 从远端拷贝
    sftp -o StrictHostKeyChecking=no root@10.204.0.1 <<< "get /opt/zkos/platform/test.txt test.txt"
# ssh 远程执行指令
    ssh $remote_user@$remote_host "$remote_command"
  1. 通过重定向符号读文件
    while read line_log
    do
        echo ${line_log}
    done < ${carconfig_log_file}
  1. shc生成二进制文件
# 静态编译
    CFLAGS=-static shc -r -f boot/tcam_boot.sh
  1. print_usage
function print_usage {
    cat << EOF # Using cat to print help string for better indentation
Usage: $0 [OPTIONS]...
EOF
    exit 2
}
  1. 带颜色输出
# colors
RED="\033[0;31m"
YELLOW="\033[1;33m"
GREEN="\033[0;32m"
NO_COLOR="\033[0m"
BOLD="\033[1m"
UNDERLINE="\033[4m"

die() {
    echo -e "${RED}${@}${NO_COLOR}"
    exit -1
}

warn() {
    echo -e "${YELLOW}${@}${NO_COLOR}"
}

good() {
    echo -e "${GREEN}${@}${NO_COLOR}"
}
  1. sed 替换输出
sed -i "s/\"unicast\"\:.*$/\"unicast\": \"10.${inet}.0.11\",/" ${ZKOS_ROOT_PATH}/modules/applications/zkos_someip_routing/etc/vsomeip_config_${inet}.json
  1. for
    # 1
    local VLAN_LIST=( 201 204 205 206 207 208 )
    for inet in ${VLAN_LIST[*]}
    do
    {

    }
    done
    # 2
    IFS=$'\n' read -d '' -ra APPS <<< "$STR"
    # exec apps
    for app in "${APPS[@]}"
    do
        echo $app
        PID=`ps aux|grep $app | grep -v grep | awk -F ' ' '{ print $2 }'`
        echo $PID
        kill -9 $PID
    done
  1. getOpt
# recognize all opetions by getopt
function getOption {
    # getopt options
    SHORT_OPTS="n:e:s:d:z:b:c:p:h"
    LONG_OPTS="network_mode:,network_card_name:,simulation_machine_name:,need_sdk_build:,need_zkos_run:,need_background_run:,config_file:,platform_version:,help"

    # Parse params with getopt
    PARSED_PARAMS=$(getopt -n $appname -o $SHORT_OPTS -l $LONG_OPTS -- "$@")
    # echo $SHORT_OPTS
    # echo $LONG_OPTS

    # Check last command (getopt params parsing) executed without error.
    VALID_PARAMS=$?
    if [ ! $VALID_PARAMS -eq 0 ] # 0 is ok, other than that is error
    then 
        print_usage
    fi

    # echo "PARSED_PARAMS: $PARSED_PARAMS"

    # Loop over getopt parsed params
    eval set -- "$PARSED_PARAMS"
    while :
    do
    case "$1" in
        -n | --network_mode)  network_mode="$2"                                  ; shift 2  ;;
        -e | --network_card_name)  network_card_name="$2"                                  ; shift 2  ;;
        -s | --simulation_machine_name)  machine_name="$2"                  ; shift 2  ;;
        -d | --need_sdk_build)  need_sdk_build="$2"                                   ; shift 2 ;;
        -z | --need_zkos_run)  need_zkos_run="$2"                                     ; shift 2  ;;
        -b | --need_background_run)       need_background_run=$2                       ; shift 2  ;;
        -p | --platform_version)  platform_version=$2                                 ; shift 2  ;;
        -c | --config_file)  config_file=$2                                 ; shift 2  ;;
        -h | --help)  print_usage                                           ; shift        ;;
        --) shift; break ;; # "--" is a special case indicating the ending of the options. The remaining parsed params are positional params.
    esac
    done

    echo "network_mode  : $network_mode"
    echo "network_card_name  : $network_card_name"
    echo "simulation_machine_name  : $machine_name"
    echo "need_sdk_build  : $need_sdk_build"
    echo "need_zkos_run  : $need_zkos_run"
    echo "need_background_run  : $need_background_run"
    echo "platform_version  : $platform_version"
    echo "config_file  : $config_file"
}
  1. 配置网络
function machine_acore_net_init
{
    local VLAN_LIST=( 201 204 205 206 207 208 )
    #添加多播组路由
    # sudo_cmd route add -net 239.0.204.0 netmask 255.255.255.0 dev eth0 2>&1 > /dev/null
    good "machine_acore net init"
    # sudo_cmd apt install -y iproute2
    for inet in ${VLAN_LIST[*]}
    do
    {
        good "vlan_${inet} init..."
        sudo_cmd ip link add link eth0 name vlan_${inet} type vlan id ${inet} 2>&1 > /dev/null \
        && sudo_cmd ip addr add 10.${inet}.0.1/16 dev vlan_${inet} 2>&1 > /dev/null \
        && sudo_cmd ip maddr add 239.0.${inet}.1 dev vlan_${inet} 2>&1 > /dev/null \
        && sudo_cmd ip link set dev vlan_${inet} up 2>&1 > /dev/null
        [ ! -z "$(ifconfig | grep "vlan_${inet}")" ] && good "vlan_${inet} init success" || warn "vlan_${inet} init err"
        sudo_cmd route add -net 239.0.${inet}.0 netmask 255.255.255.0 dev vlan_${inet} 2>&1 > /dev/null
    }&    
    done
    wait
    good "machine_acore net init done"
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值