vieyahn2017 / shellv

shell command test and study
4 stars 1 forks source link

7.31 shell数组应用案例 #67

Open vieyahn2017 opened 4 years ago

vieyahn2017 commented 4 years ago

shell数组

vieyahn2017 commented 4 years ago

ls结果存入数组:

c=0
for file in `ls`
do
  filelist[$c]=$file
  ((c++))
done

查看数组元素:

echo ${filelist[1]} AController.class

echo ${filelist[0]} EController.class

vieyahn2017 commented 4 years ago

把filelist数组内容输出到屏幕上:

b=0
while [ $b -lt $c ]
do
  echo ${filelist[$b]}
  ((b++))
done

或者

b=0
for value in ${filelist[*]}
do 
  echo $value
done

在屏幕上输出filelist数组长度: echo ${#filelist[]} 注:用${#数组名[@或]} 可以得到数组长度

vieyahn2017 commented 4 years ago

传入多个文件或者all 的脚本


if [ -z "$1" ] || [ "$1" == "usage" ]; then
    echo "usage: "
    echo "sh $0 XXX.class"
    echo "sh $0 AAA.class BBB.class CCC.class"
    echo "sh $0 all"
    exit
fi

if [ "$1" == "all" ] ; then
    c=0
    for class_file in $(ls | grep -E "*.class$")
    do
        class_parames[$c]=$class_file
        ((c++))
    done
else
    c=0
    for class_file in $*
    do
        class_parames[$c]=$class_file
        ((c++))
    done
fi

num=${#class_parames[@]}
echo $num

for class_param in ${class_parames[*]}
do
    echo $class_param
done
vieyahn2017 commented 4 years ago
#!/bin/bash
# tomcat-replace-class.sh

JDK_PATH=/home/xxx/jdk/bin

# 优先检查是否自带jdk,然后使用JDK_PATH上面定义的jdk
function func_check_javap () {
    source /etc/profile
    javap -version > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        JAVAP=javap
    else
        JAVAP=$JDK_PATH/javap
        if [ ! -e "$JAVAP" ]; then
            echo "JDK not existed. You can install jdk on the env, or provide JDK_PATH in this script."
            exit
        fi
    fi
}

function func_get_class_path () {
    class_name=$1
    class_name=$(echo $class_name | sed s#\\.class##g)
    $JAVAP -p "$class_name".class | grep "class" | xargs -n 1 | grep $class_name | sed s#$class_name##g | sed s#\\.#/#g
}

if [ -z "$1" ] || [ "$1" == "usage" ]; then
    echo "usage: "
    echo "sh $0 XXX.class"
    echo "sh $0 AAA.class BBB.class CCC.class"
    echo "sh $0 all"
    exit
fi

# main

func_check_javap

if [ "$1" == "all" ] ; then
    c=0
    for class_file in $(ls | grep -E "*.class$")
    do
        class_parames[$c]=$class_file
        ((c++))
    done
else
    c=0
    for class_file in $*
    do
        class_parames[$c]=$class_file
        ((c++))
    done
fi

target_classes_path="/xxxxx/webapps/xxxx/WEB-INF/classes/"
for class_param in ${class_parames[*]}
do
    class_target_path="$target_classes_path"$(func_get_class_path $class_param)
    for target_container_id in $(docker ps | grep target_container | awk '{print $1}') ; do
        echo docker cp $class_param $target_container_id:$class_target_path
        docker cp $class_param $target_container_id:$class_target_path
        echo docker exec -ti $target_container_id chown usera:ivs $class_target_path/$class_param
        docker exec -ti $target_container_id chown usera:ivs $class_target_path/$class_param
    done
done
vieyahn2017 commented 4 years ago
func_get_zookeeper_addr () {
    arr=(${Zookeeper_software_ip//,/ })
    num=${#arr[@]}
    if [ "$num" == "1" ]; then
        zookeeper_address=zookeeper://${arr[0]}:2181
    elif [ "$num" == "3" ]; then
        zookeeper_address="zookeeper://${arr[0]}:2181?backup=${arr[1]}:2181,${arr[2]}:2181"
    else
        zookeeper_address=""
    fi
}
vieyahn2017 commented 3 years ago

bash数组(array)处理方法

一般而言,A="a b c def"只是将 $A 替换为一个单一的字符串,但是改为 A=(a b c def),则是将 $A 定义为数组。 bash的数组替换方法可参考如下方法: ${A[@]} 或 ${A[]} 得到 a b c def(全部数组) ${A[0]} 得到 a (第一个元素),${A[1]} 第二个... ${#A[@]} 或 ${#A[]} 得到 4 (数组数量) ${#A[0]} 得到 1 (第一个元素 a 的长度),${#A[3]} 得到 3 (第四个元素 def 的长度) A[3]=xyz 将第四个元素重新定义为 xyz

vieyahn2017 commented 3 years ago
endpoints="172.17.37.30:20101,172.17.37.30:20102,172.17.37.30:20103"
arr=(${endpoints//,/ })
ttt=""
for ip in ${arr[*]};do ttt=${ttt}"https://$ip,"; done
https_endpoints=${ttt%?}
echo $https_endpoints

https://172.17.37.30:20101,https://172.17.37.30:20102,https://172.17.37.30:20103
vieyahn2017 commented 3 years ago
endpoints="172.17.37.30:20101"
arr=(${endpoints//,/ })
ttt=""
for ip in ${arr[*]};do ttt=${ttt}"https://$ip,"; done
https_endpoints=${ttt%?}
echo $https_endpoints

https://172.17.37.30:20101