bash简单常见用法

bash新建自定义数组

myArray=()

for ((i = 0 ; i < 5 ; i++ ))

do

myArray[$i]="AAAA{$i}DD"

done

echo ${myArray[@]} #输出结果是AAAA{0}DD AAAA{1}DD AAAA{2}DD AAAA{3}DD AAAA{4}DD

提取文件名成功

projects="D:/Project/Program/IDEAWorkspace/myauto/automation-test/src/main/java/com/welab/automation/projects/channel/feature/abc.feature"

filename=${projects##*/} #变量赋值 等号两边不要空格

echo "${filename}" #输出结果abc.feature

bash if的用法,根据featureLocations的值是否包含API55,来确定给platform赋值

platform=""

featureLocations="API"

if [[ ${featureLocations[$i]^^} = *"API55"* ]]; then

platform="API"

else

platform="APP"

fi

echo ${platform}

bash脚本中变量大小写切换

name="Abc Def"

echo upper ${name}

echo ${name} | tr "[a-z]" "[A-Z]" 小写字母变成大写 ,mac和windows都适用

echo ${name,,} 大写字母变成小写

echo ${name^^} 小写字母变成大写

$ test="abcDEF"

# 把变量中的第一个字符换成大写

$ echo ${test^}

AbcDEF

# 把变量中的所有小写字母,全部替换为大写

$ echo ${test^^}

ABCDEF

# 把变量中的第一个字符换成小写

$ echo ${test,}

abcDEF

# 把变量中的所有大写字母,全部替换为小写

$ echo ${test,,}

abcdef

bash shell 详细介绍文档:

https://www.coolcou.com/bash-shell-example/bash-shell-variable/bash-shell-case.html

获取bash脚本所在的绝对路径

SHELL_FOLDER=$(cd "$(dirname "$0")";pwd)

echo "${SHELL_FOLDER}"

与相对路径拼接,然后删除文件

rm -f ${SHELL_FOLDER}/delete/a/test.txt

for 语句依次打印数组元素

names=("Alen" "Walker" "Miller")

len=${#names[*]} #获取数组长度

echo "${len}"

echo The length of the array is - $len

for (( i=0; i<$len; i++ ))

do

echo The value of element $i is: ${names[$i]}

done

#数组A转成字符串S

A=(AA BB CC)

S=$(echo "${A[*]}")#A是数组,S是字符串。S的内容就是"AA BB CC"啦

S=$(IFS=,; echo "${A[*]}") #S="AA,CC,CC"

#字符串S转成数组A

S="AA BB CC"

A=(${S}) #A就是数组了,A=(AA BB CC)

names=("Alen" "Walker" "Miller")

echo The length of the array is ${#names[*]} #获取数组的长度

# 字符串转换成数字

index_num=$((index))

index_num=$((index-1)) #注意减1这些运算操作要在括号内进行

#定义数组,然后一次echo数字,让用户可以输入num来选中需要执行的case

folderarr=("Login" "Add Money EDDI" "Card") #定义数组用空格分开,而不是逗号

echo "1.Login"

echo "2.Add Money EDDI"

echo "3.Card"

echo "${folderarr}"

folderarr_len=${#folderarr[*]}

echo "folderarr_len ${folderarr_len}"

read ids #等待用户输入,读取用户输入的case id

myids=(${ids}) #字符串转数组

echo The length of the array is ${#myids[*]}

ll=${#myids[*]}

echo "${ll}"

echo "${#myids[*]}" #输出数组长度

#for (( i=0; i<$ll; i++ ))

#do

#echo The value of element $i is: ${folderarr[${i}]}

#done

#for index in "${myids[@]}"

#do

#echo "${index}"

#index_num=$((index)) # 字符串转换成数字

#echo "${index_num}"

#echo The value of element is: ${folderarr[index_num-1]}

#echo The value of element is: ${folderarr[0]}

#done

new_arr=()

#根据第一个数组的元素座位坐标,去第二个数组中获取对应的元素

echo "-----------------------"

for (( i=0; i<$ll; i++ ))

do

index=${myids[${i}]} #获取第i个元素

index_num=$((index-1)) # 把元素转换成数字,获取folderarr下坐标,由于坐标是从0开始,所以减1,注意减1要在括号内进行

echo "index_num ${index_num}"

item=${folderarr[${index_num}]} #根据数字从folderarr数组中

echo "item: ${item}"

echo "i: ${i}"

new_arr[${i}]="${item}"

echo "-----------------------"

done

#数组长度

echo "${new_arr[*]}" #输出整个new_arr数组

echo "${new_arr[1]}" #输出new_arr数组第二个元素

echo "${#new_arr[*]}" #输出整个new_arr数组长度,注意比输出整个数组多了一个 # 号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值