Ubuntu系统中sh脚本编写

我们在使用Ubuntu系统开发的过程中,经常会遇到一些重复的操作,想copy, push等等。这个时候我们就可以自己编写一个sh脚本,使用sh脚本操作这些重复的动作。

1.在编写sh脚本前了解一下基本语法
1.1 if语句
#!/bin/sh
myPath="/var/log/httpd/"
myFile="/var/log/httpd/access.log"
# -x 判断$myPath是否存在并且是否具有可执行权限
if [ -x "$myPath" ];then
  #mkdir "$myPath"
	echo $myPath
fi

# -d 判断$myPath是否存在
if [ -d "$myPath" ];then
  #mkdir "$myPath"
echo $myPath
fi

# -f 判断$myFile是否存在
if [ -f "$myFile" ];then
  #touch "$myFile"
echo $myFile
fi

# -n 判断一个变量是否有值
if [ -n "$myVar" ];then
  echo $myVar "is empty"
  exit 0
fi

# 判断两个变量是否相等
if [ "$var1" = "$var2" ];then
  echo "$var1 eq $var2"
else 
  echo "$var1 not eq $var2"
fi
1.2 for语法
#!/bin/bash

for ((i =1;i<=5;i++));
do
	sleep 1
	echo $i
done
2. 下面我自己写了一些简单的sample
2.1 扫描指定文件夹下面的so文件
filehead="so"
index=0
function getAllFileFromDire(){
    #echo $1
    for f in $1/*
    do
	    if [ -d "$f" ]  
        then   
          getAllFileFromDire $f 
        elif [ -f "$f" ]  
        then
          name=$f  
          start=${name:0-2:3}
          #echo "$start"
          if [ "$start" = "$filehead" ];then
            echo "$name"
            let index++
          fi 
        fi 
    done
}

getAllFileFromDire /home/bob/Desktop/Document/1.7.1/out-of-source

echo $index
2.2 对当前目录下面的所有apk文件进行签名,把签名后的文件放到以当前时间为名字的文件夹下面
#!/bin/bash
key=t1S9D21N03nX
out=`date '+%Y%m%d_%H%M'`
mkdir $out
for f in *.apk
do
	echo $f
	#echo $key | 
	java -jar signapk.jar platform.x509.pem platform.pk8 $f $out/$f
done
2.3 把制定的文件夹下面的所有扩展名为so的文件copy到制定的位置
date
source_path=/home/.../Desktop/Document/1.9/out-of-source
#source_path=/home/.../Desktop/Document/out-of-source
target_path=/home/.../tftpboot/avs_libs/
filehead=".so"
index=0
function getAllFileFromDire(){
    #echo $1
    for f in $1/*
    do
	    if [ -d "$f" ]  
        then   
          getAllFileFromDire $f 
        elif [ -f "$f" ]  
        then
          name=$f  
          start=${name:0-3:3}
          #echo "$start"
          if [ "$start" = "$filehead" ];then
            echo "$name"
            let index++
            cp $name $target_path
          fi 
        fi 
    done
}

getAllFileFromDire $source_path

echo "copy $index librarys to $target_path"

cp $source_path/Integration/test/app $target_path/../tools/
2.4 抓取当前系统的memory info到指定文件

#sn=$(adb shell getprop ro.boot.serialno) 
out=`date '+%Y%m%d_%H%M'_meminfo.txt`
echo > $out
#echo SN: ${sn} >> $out
echo >> $out
while true
do
	
	date >> $out
    #cat /proc/meminfo | grep -E "Mem|Cached|Buffers" >> $out
    cat /proc/meminfo >> $out
    echo  >> $out
    sleep 60
done
  • 5
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值