preface

Essay writing cycle: April 3rd begins around 9pm and ends at 11pm. The whole process went relatively smoothly… I’m not attracted to other weird content

To tell the truth, script writing this piece of true “business is not fine”, on the one hand today’s tools have been more convenient, second, I still relatively lazy.

Recently, we tried the company’s cloud build feature. Although the cloud build plug-in does a lot of work automatically, it just pulls the production (APK) into a local directory, so we still need to actively install.

So based on this scenario it is like writing a script to install the dropdown APK with one click.

Script capabilities required:

  • Find the latest APK in a file path
  • Install the apk
  • Then open the main Activitiy

The body of the

Let’s do it now! But… I also said “business is not good” at the beginning, so Shell really can’t write. The first step is to learn the Shell syntax.

1. Shell basics

Before we dive into the syntax, let’s talk about what a Shell is.

1.1. What is a Shell

A Shell is a general term for an operating system’s command interpretor. It can be easily understood as “command line”.

Those of you who have used a GuI-free Linux system know that the only way we can interact with the operating system is by typing commands.

The tool that receives the commands we type and informs the operating system to execute them is called a Shell.

But usually the Shell we talk about is generally under Linux, because the Shell processing under Linux can act as a command interpreter, but also has its own programming language, so it is very extensible. Our Shell scripts also rely on this compilation capability…

Of course, the Shell is much more powerful than that. More ability by everyone to explore ~

1.2 basic syntax of Shell

Variables:

Variable definitions in the Shell are a bit of a pain… Do not declare any type: name=”Shell”, name is a variable.

Here’s an even weirder rule: = no Spaces left or right! Name = “Shell”, this statement is wrong!

When we use variables, we need to add a $character. For example, if we want to print the name defined above, we need to do this: echo $name.

${},$(),${}.

The two ‘ ‘,$() belong to the same category: command substitution. How do we understand this? To put it bluntly, the result of what is written here is taken out (like when we call a function and get the return value).

So sometimes, instead of executing some command, we might want to get the value of the command, so that’s the case.

${$} = ${$} = ${$}

Script parameter fetching:

We usually encounter some parameters when executing commands, and the same is true for Shell scripts. What if you want to get the user’s input?

Directly like this: $1, which means to writing is to get the first argument, that’s right $2 is to get the second parameter…

Function:

The definition of the function is also quite magical, without actively declaring parameters and return values:

function getResult(){
    first=The $1
    second=$2
    return $first+$second
}
Copy the code

Here we define a function. You can see that the function body has no parameters and no return value information. How do we call that?

getResult The $1 $2
result=$?
echo "$result"
Copy the code

Anyone here might have noticed the $? This “strange” call is used to get the return of the above function. This is also a Shell “egg pain” point…

After running the script, it looks like this:

Write scripts on demand

With this foundation we want to complete the script we need to be relatively simple. Here I’ll paste the code directly:

args=The $1
curNewApk=""
# here is the apK directory
for apk in /Users/x/xx/xxx/*; 
do
    if [[ $curNewApk= =""]].then
        curNewApk=$apk
    else
        # Here to get the current apK time (hour + minute), of course, here only by hour and minute cannot accurately determine the latest file, here only provides a kind of idea
        newApkTime=`stat -f %SB -t %H%m $apk`
        lastApkTime=`stat -f %SB -t %H%m $curNewApk`
        if [[ newApkTime > lastApkTime ]]; then
            A simple assignment to the current latest APK
            curNewApk=$apk
        fi
    fi
done
echo "Latest APK:$curNewApk"
Get the first input parameter. If 0, uninstall
if [[ $args= ="0"]].then
	adb uninstall app.x.xx.xxx
fi
Install and start the Activity normally
adb install -r $curNewApk
adb shell am start -n x.xx.xxx.MainActivity
Copy the code

Here’s what a simple script can do: find a recently modified file from a directory and execute install, corresponding to the Activity in start.

The end of the

Shell scripts are fun and do a lot of repetitive work with ease.

There is no end to learning

I am a fresh graduate, recently and friends maintain a public account, the content is that we in the transition from fresh graduate to the development of this way stepped on the pit, as well as our step by step learning records, if interested in friends can pay attention to it, together with fuel ~