shell使用汇总

1、参数传值 脚本 

a:     a.sh -a  123  可以将值123传值给 a)模块 

while getopts ":a:n:h" OPT &> /dev/null ; do
    case $OPT in
    a)
        account=$OPTARG
        ;;
    n)
        name=$OPTARG
        ;;
    h)
        echo -e $help
        echo -e $productidHelp
        exit
        ;;
    ?)
        echo -e $help
        echo -e $productidHelp
        exit
        ;;
    esac
done

2、if 

-z  判断为空为真

-a 文件存在为真

if [[ -z $name ]];then
    if [[ -a  /home/liumei/namedb ]];then
        cd /home/liumei/
        num=`cat namedb|wc -l`
        line=$[$RANDOM%$num+1]
        name=`sed -n $line'p' namedb`
    else
        name="一家大公司"
    fi
fi

3、awk切割字符串

did=`echo $account_exist|awk -F ',' '{print $3}'|awk -F = '{print $2}'`

4、for 循环遍历数组

pList=1,2,3,4
pList=(${productidList//,/ })  #切割字符串为数组  
for i in ${pList[@]};do    
    pass
done

5、while循环

#while为真则执行循环体
while [ $count -ne 0 ] ;do
    pass
done

6、sed

#sed删除 //内匹配的数据
sed -i '/<es_cache_dids>'$did'/d' a.xml
#sed添加 /匹配的数据/添加的数据/ 
sed -i '/<!-- 使用 es 作缓存的公司 -->/ a\<es_cache_dids>'$did'</es_cache_dids>' a.xml

7、将date转化为时间戳

#获取当前时间
current=`date "+%Y-%m-%d %H:%M:%S"`  
#转化为时间戳  %s%3N代表是毫秒级的时间戳
currentTimeStamp=`date -d "$current" +%s%3N`

 

posted @ 2021-02-06 15:07  sugoi  阅读(56)  评论(0)    收藏  举报