shell小记

shell脚本要求unix换行格式。在win上写好shell后,需先用dos2unix转换过文件才能执行


if语法片段

ENV=

if [ "$1" == "ONLINE" ]; then
  ENV=$1
else
  ENV="TEST"
fi

或者
gid=50
if ((i<7)); then
  gid=1;
elif ((i<13)); then
  gid=2;
fi

  

 


for语法片段

1)、for 字符遍历的语法
for ((area=1; area<=1; ++area)); do
$area ....干啥干啥
done


2)、for in的语法
for file in `ls`
do
sed -i "s/mydir/$filename/g" $file
newfile=`echo $file | sed "s/mydir/$filename/g"`
mv $file $newfile
done

  


数组小例

1)、数值范例
varpos=(4 5 6 10 11 12 13 14)
for ((i=1; i<=8; ++i)); do 
items=$(((i+1)*10)) 
pos=${varpos[$i-1]} 
$pos ..$items...干啥干啥
done

2)、取数值长度
for ((i=0;i<${#varpos[@]};i++))

  

 

一些踩的坑

1)、stage_2_$area_$gid 输出内容会缺少一个变量的值
改成stage_2_${area}_${gid}就能够正常输出了。因为shell认为$后连接字符是
同一个变量,用{}括起来后就正常了

2)、变量数学运算算式入下:
hourid=$(($day*100+$hour))
需要用括号括起来

  

 

posted @ 2019-10-22 14:50  掉头发的666  阅读(123)  评论(0编辑  收藏  举报