linux字符串和数组
今天用了一下shell的数组和字符串,说实话,用完我的感觉是非常不舒服,尤其是数组,感觉用起来很不方便,和Python相比,简直是太low了。
我就对比一下shell和python吧。
#定义数组
a=(1 2 3)
#python: a = (1,2,3)
#数组长度
echo "${#a[@]}"
# python: len(a)
#取元素
echo "${a[0]}"
# python : a[0]
#数组遍历
for var in ${a[@]}
do
echo $var
done
#python
#for var in a:
# print var
字符串:
#!/bin/bash
string='adnhuihfdius'
echo $string
#length
echo ${#string}
echo "qiepian:${string:1:2}"
#get last ten eles
echo ${string: -10}
#set variable
echo ${string:-10}
#replace the 1st matched ele
echo ${string/h/w}
#replace all matched ele
echo ${string//h/w}
--------EOF---------
微信分享/微信扫码阅读
微信分享/微信扫码阅读