sh scripting in Ubuntu system

Keywords: Mobile Ubuntu Java shell

In the process of developing Ubuntu system, we often encounter some repetitive operations, such as copy, push and so on. At this point, we can write a sh script ourselves, using the SH script to operate on these repeated actions.

1. Understand the basic grammar before writing sh scripts
1.1 if statement
#!/bin/sh
myPath="/var/log/httpd/"
myFile="/var/log/httpd/access.log"
# - x determines whether $myPath exists and whether it has executable privileges
if [ -x "$myPath" ];then
  #mkdir "$myPath"
	echo $myPath
fi

# -d to determine whether $myPath exists
if [ -d "$myPath" ];then
  #mkdir "$myPath"
echo $myPath
fi

# - f to determine whether $myFile exists
if [ -f "$myFile" ];then
  #touch "$myFile"
echo $myFile
fi

# -n to determine whether a variable is worth
if [ -n "$myVar" ];then
  echo $myVar "is empty"
  exit 0
fi

# Judging whether two variables are equal
if [ "$var1" = "$var2" ];then
  echo "$var1 eq $var2"
else 
  echo "$var1 not eq $var2"
fi
1.2 for grammar
#!/bin/bash

for ((i =1;i<=5;i++));
do
	sleep 1
	echo $i
done
2. Next, I wrote some simple sample s myself.
2.1 Scan the so file under the development folder
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/Helios/AVS/1.7.1/out-of-source

echo $index
2.2 Sign all the apk files under the current directory and place the signed files under the folder with the name of the current time.
#!/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 copy all files with the extension so under the designated folder to the designated location
date
source_path=/home/.../Desktop/Document/Helios/AVS/1.9/out-of-source
#source_path=/home/.../Desktop/Document/Helios/Sensor/Amazon/1.9/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 Grab the memory info of the current system to the development document
#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

Posted by Terrum on Sun, 27 Jan 2019 15:45:14 -0800