一些 Linux 下的脚本小工具

1、查询 Android 手机的前台Activity

#!/bin/sh

adb shell dumpsys activity activities | grep mResumedActivity

2、自动 wait-for-device 的 adb

#!/bin/bash

#/usr/bin/adb wait-for-device $*

realadb=~/Android/Sdk/platform-tools/adb

if [[ $1 = --* ]]; then
    $realadb $*
elif [ $1 = "devices" ]; then
    $realadb devices
elif [ $1 = "kill-server" ]; then
    $realadb kill-server
elif [ $1 = "start-server" ]; then
    $realadb start-server
elif [ $1 = "-s" ]; then
    $realadb -s $2 wait-for-device ${@:3}
elif [ $1 = "-e" ]; then
    if [ $2 = "-d" ]; then
        $realadb -e -d wait-for-device ${@:3}
    else
        $realadb -d wait-for-device ${@:2}
    fi
elif [ $1 = "-d" ]; then
    $realadb -d wait-for-device ${@:2}
else
    $realadb wait-for-device $*
fi

3、Wifi 连接 adb

adb root;
adb remount;
ip=`adb shell ifconfig | grep "inet addr:172" | grep Bcast | awk -F: '{print $2}' | awk '{print $1}'`
echo -e "\033[31mip = $ip \033[0m"
adb devices;
sleep 1;
adb tcpip 5555;
sleep 1;
adb connect $ip:5555
sleep 1;

echo "============ after adb devices ============= 1"
adb devices
echo "============ after adb devices ============= 2"

4、adb push 文件夹或文件

#!/bin/bash

path=$1
target=${
    
    path##*/system/}

if [ -d $path ]; then
    adb push $path/* /system/$target/
elif [ -f $path ]; then
    echo "pushing $path /system/$target"
    adb push $path /system/$target
#elif [ -L $path ]; then
#    echo "pushing $path /system/$target"
#    adb -e -d push $path /system/$target
fi

5、adb 依次 push 文件夹下的子文件夹或文件

#!/bin/bash

path=$1

for i in `ls $path`; do
    apush "$path/$i";
done

6、搜索文件夹

#!/bin/bash

find $(pwd) -type d -name $*

7、搜索文件

#!/bin/bash

find $(pwd) -type f -name "$*"

8、搜索 .git 文件夹

#!/bin/bash

find $(pwd) -type d -name ".git"

9、gitstat

#!/bin/bash

commit_1=HEAD^
commit_2=HEAD
filelist=""

if [ $# -gt "0" ]; then
    if grep -qE '^(([[:xdigit:]]*)|(\w*HEAD))(\^|~|[[:digit:]])*$' <<< "$1"; then
        echo "$1 is a commit."
        commit_1=$1^
        commit_2=$1

        if [ $# -gt "1" ]; then
            if grep -qE '^(([[:xdigit:]]*)|(\w*HEAD))(\^|~|[[:digit:]])*$' <<< "$2"; then
                echo "$2 is a commit."
                commit_1=$1
                commit_2=$2
                if [ $# -gt "2" ]; then
                    filelist=${@:3}
                fi
            else
                filelist=${@:2}
            fi
        fi

    else
        filelist=${@:1}
    fi
fi

echo filelist=$filelist

if [ "$filelist" = "" ]; then
    git diff $commit_1 $commit_2 --stat
else
    git diff $commit_1 $commit_2 --stat $filelist
fi



#if [ $# = "0" ]; then
#    git diff HEAD^ HEAD --stat
#elif [ $# = "1" ]; then
#    git diff $1^ $1 --stat
#elif [ $# = "2" ]; then
#    git diff $1 $2 --stat
#else
#    git diff $1 $2 --stat ${@:3}
#fi

10、gitdifftool

#!/bin/bash

commit_1=HEAD^
commit_2=HEAD
filelist=""

if [ $# -gt "0" ]; then
    if grep -qE '^(([[:xdigit:]]*)|(\w*HEAD))(\^|~|[[:digit:]])*$' <<< "$1"; then
        echo "$1 is a commit."
        commit_1=$1^
        commit_2=$1

        if [ $# -gt "1" ]; then
            if grep -qE '^(([[:xdigit:]]*)|(\w*HEAD))(\^|~|[[:digit:]])*$' <<< "$2"; then
                echo "$2 is a commit."
                commit_1=$1
                commit_2=$2
                if [ $# -gt "2" ]; then
                    filelist=${@:3}
                fi
            else
                filelist=${@:2}
            fi
        fi

    else
        filelist=${@:1}
    fi
fi

echo filelist=$filelist

if [ "$filelist" = "" ]; then
    gitstat $commit_1 $commit_2
    echo git difftool $commit_1 $commit_2
    git difftool $commit_1 $commit_2
else
    gitstat $commit_1 $commit_2 $filelist
    echo git difftool $commit_1 $commit_2 $filelist
    git difftool $commit_1 $commit_2 $filelist
fi



#if [ $# = "0" ]; then
#    gitstat
#    git difftool HEAD^ HEAD
#elif [ $# = "1" ]; then
#    if $has_commit_1; then
#        gitstat $1
#        git difftool $1^ $1
#    else
#        git difftool HEAD^ HEAD $1 # with filename
#    fi
#elif [ $# = "2" ]; then
#    if $has_commit_2; then
#        gitstat $1 $2
#        git difftool $1 $2
#    else
#        git difftool $1^ $1 $2 # with filename
#    fi
#else
#    git difftool $1 $2 ${@:3} # with filename
#fi

11、git 搜索文件历史

扫描二维码关注公众号,回复: 12324265 查看本文章
#!/bin/bash

git log --name-status -- '*'$1'*' ${@:2}

12、远程 Windows

#!/bin/bash

rdesktop -g 1600x900 -P -z -x l -r sound:off -u account -p pw 111.111.111.111:3389

13、解锁 Android 手机

#!/bin/bash

ret=`adb -e -d shell dumpsys power | grep "Display Power: state="`
echo $ret
if [[ $ret = "Display Power: state=ON" ]]; then
    echo "ON -> unlock"
else
    echo "OFF -> ON -> unlock"
    adb -e -d shell input keyevent 26
fi

adb -e -d shell input swipe 500 500 500 100

14、Android 手机下滑

#!/bin/bash

adb -e -d shell input swipe 500 500 500 0

15、Android 手机上滑

#!/bin/bash

adb -e -d shell input swipe 500 500 500 2000

16、自动创建 Android 源码 out 目录链接

#!/bin/bash

if [ $TARGET_PRODUCT ];then
    echo "TARGET_PRODUCT = $TARGET_PRODUCT"
else
    echo "TARGET_PRODUCT IS NOT EXISTS"
fi

if [ -z $TARGET_PRODUCT ];then
    echo "TARGET_PRODUCT is empty"
else
    croot
    if [ ! -d "out" ]; then
        dirname=/HDD/REAL-OUT/$(basename `pwd`)-out-$TARGET_PRODUCT-$TARGET_BUILD_VARIANT
        if [ ! -d $dirname ]; then
            mkdir $dirname
        fi
        ln -s $dirname out
    fi
fi

Java 环境变量设置

if [ -d $HOME/java/jdk ];then
    JAVA_HOME=$HOME/java/jdk/bin
    PATH="$JAVA_HOME:$PATH"
fi

17、切换到 jdk7

#!/bin/bash

cd ~/java
rm jdk
ln -s java7 jdk

18、切换到 jdk8

#!/bin/bash

cd ~/java
rm jdk
ln -s java8 jdk

猜你喜欢

转载自blog.csdn.net/hegan2010/article/details/85073505