欢迎来到 嗅灵易学

零基础也能上手的脚本技术课,一对一答疑带你入门

[原创]Android启用swap文件sh脚本

[原创]Android启用swap文件sh脚本

上次手机用swapper2差点变砖,可是还是想试试手机开启虚拟内存。
于是想弄清swapper2的原理,今天研究了一天,终于写出来了android下开启虚拟内存的sh~
设备要求:
android + root
android终端 + busybox
*最好能用bash
直接上图:




脚本代码:
注意,换行符需为unix格式才能执行.
因为android终端不支持中文,而且本人英文很弱,所以大部分提示都是谷歌翻译的0.0

#!/system/bin/bash
sfpath=/mnt/sdcard/swapfile
b_nrf=0
if [ $# -eq 0 ] ; then
    echo -e "AnSwap Information"
    echo -e "Version: 1.0"
    echo -e "Author: CserSoft"
    echo -e "E-Mail: csersoft@qq.com"
    echo -e "*** Be careful using this software! ***\n\n"
    echo -e "AnSwap Usage: \n"
    echo -e "answap -on <Swap File Size , Units of MB> [-d : Do not remove files] [Swap File Path]"
    echo -e "answap -off [-d : Do not remove files] [Swap File Path]\n"
    echo -e "Note: [Swap File Path] default is /mnt/sdcard/swapfile\n\n"
    echo -e "AnSwap Example: \n"
    echo -e "answap -on 128"
    echo -e "answap -on 128 -d"
    echo -e "answap -off"
    echo -e "answap -off -d\n"
    echo -e "answap -on 128 /mnt/sdcard/vmfile"
    echo -e "answap -on 128 -d /mnt/sdcard/vmfile"
    echo -e "answap -off /mnt/sdcard/vmfile"
    echo -e "answap -off -d /mnt/sdcard/vmfile"
    exit 2
fi
echo -n "Check: busybox ... "
if [ ! -x "/system/bin/busybox" ] && [ ! -x "/system/xbin/busybox" ] ; then
    echo "Failed!"
    echo "Error: 'busybox' Do not exist or can not be executed !"
    echo "Solve: Copy 'busybox' to the '/system/bin' or '/system/xbin' directory !"
    exit 3
else
    echo "OK!"
fi
echo -n "Check: root ... "
if [ `id -u $USER` -eq 0 ] ; then
    echo "OK!"
else
    echo "Failed!"
    echo "Error: Please run $0 as root !"
    exit 4
fi
function EnableSwap()
{
    if [[ ! $1 =~ ^[[:digit:]]*$ ]] ; then
        echo "Error: '$1' Is not legitimate digital ! "
        exit 6
    fi
    if [ "$1" -lt 32 ] || [ "$1" -gt 512 ] ; then
        echo "Error: Swap File Size Should Range From 32 MB To 512 MB !"
        exit 7
    fi
    local sfs=$[$1*1024] 
    if [[ ! $sfpath =~ ^((/?[a-zA-Z0-9._-]+(/[a-zA-Z0-9._-]+)*)|/)$ ]] ; then
        echo "Error: Illegal path '$sfpath' ! "
        exit 8
    fi
    if [ -e "$sfpath" ] ; then
        if [ -d "$sfpath" ] ; then
            echo "Error: '$sfpath' Already exists , And is a directory !"
            exit 9
        elif [ -f "$sfpath" ] ; then
            echo "Prompt: '$sfpath' Already exists"
            if [ $b_nrf -eq 0 ] ; then
                echo "Do you want to remove it and continue anyway ?"
                echo "Prompt: Please enter 'y' or other :\n"
                read -n 1 selprm
                if [[ $selprm == "y" ]] ; then
                    rm -f "$sfpath"
                    if [ -f "$sfpath" ] ; then
                        echo "Error: Remove File '$sfpath' Failed !"
                        exit 10
                    fi
                else
                    echo "User abort the operation !"
                    exit 11
                fi
            fi
        else
            echo "Error: '$sfpath' Already exists , Unknown File Type !"
            exit 12
        fi
    fi
    echo "Swap File Size is: $sfs KBytes"
    echo "Swap File Path is: '$sfpath'"
    if [ $b_nrf -eq 0 ] ; then
        dd if=/dev/zero of="$sfpath" bs=1024 count=$sfs
        sleep 1s
        if [ ! -f "$sfpath" ] ; then
            echo "Error: Create File $sfpath Failed !"
            exit 13
        fi
    fi
    mkswap "$sfpath"
    sleep 1s
    if [ $? -ne 0 ] ; then
        echo -e "\nError: 'mkswap' Failed ! "
        exit 14
    fi
    swapon "$sfpath"
    sleep 1s
    if [ $? -ne 0 ] ; then
        echo -e "\nError: 'swapon' Failed ! "
        exit 15
    else
        echo -e "\n'swapon' Succeed ! "
        echo "Use the 'free' command to view detailed ! "
    fi
}
function DisableSwap()
{
    if [[ ! $sfpath =~ ^((/?[a-zA-Z0-9._-]+(/[a-zA-Z0-9._-]+)*)|/)$ ]] ; then
        echo "Error: Illegal path '$sfpath' ! "
        exit 16
    fi
    if [ ! -f "$sfpath" ] ; then
        echo "Error: '$sfpath' Does not exist ! "
        exit 17
    fi
    swapoff "$sfpath"
    sleep 1s
    if [ $? -ne 0 ] ; then
        echo -e "\nPrompt: 'swapoff' Failed ! "
    fi
    if [ $b_nrf -eq 0 ] ; then
        rm -f "$sfpath"
        sleep 1s
        if [ -f "$sfpath" ] ; then
            echo -e "\nPrompt: Remove File '$sfpath' Failed ! "
        else
            echo -e "\n'swapoff' Succeed ! "
            echo "Use the 'free' command to view detailed ! "
            return 0
        fi
    fi
    echo -e "\n'swapoff' Succeed ! "
    echo "Use the 'free' command to view detailed ! "
}
if [[ $1 == "-on" ]] ; then
    if [[ $3 == "-d" ]] ; then
        b_nrf=1
        if [[ $4 != "" ]] ; then
            sfpath=$4
        fi
    else
        if [[ $3 != "" ]] ; then
            sfpath=$3
        fi
    fi
    EnableSwap $2 
    exit 0
elif [[ $1 == "-off" ]] ; then
    if [[ $2 == "-d" ]] ; then
        b_nrf=1
        if [[ $3 != "" ]] ; then
            sfpath=$3
        fi
    else
        if [[ $2 != "" ]] ; then
            sfpath=$2
        fi
    fi
    DisableSwap
    exit 0
else
    echo "Error: Invalid argument of '$1'"
    exit 5
fi

第一次写sh脚本,网上抄了不少代码

上传的附件 刷机精灵截图20140322215528.png
刷机精灵截图20140322215939.png
刷机精灵截图20140322220055.png
刷机精灵截图20140322220204.png

注意:上传附件及图片大小不得大于30M。

⚠️ 版权声明:
本博客所有内容(含教程、源码、工具)仅供个人技术学习与研究交流使用,严禁商用、倒卖、二次分发及非法用途。
未经作者书面授权,任何组织或个人不得转载、复制或用于其他平台,违者将追究相关责任。

0 0 0 举报
复制成功