【原创已传文件】sh脚本在安卓环境启用Swap

@Ta 2015-01-10 4599点击
最近帮朋友研究手机,给她处理手机运存过小问题。。试了试swapper2结果差点成砖。。结果研究一天,写出来一个Android下开启虚拟内存(swap)的sh,有问题联系QQ601639973。

文件已传到百度盘:http://pan.baidu.com/share/link?shareid=3538209287&uk=825937364
文件名为system.sh

注意,换行符需为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: XiaYeGe"
    echo -e "E-Mail: 601639973@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

回复列表(19|隐藏机器人聊天)
添加新回复
回复需要登录