命令執行相關範例代碼 (os.execute)
os.execute 依賴於 Shell,iOS 越獄環境的 Shell 可能是 Bash 或 Zsh。
巨魔版 XXTouch 不支援 os.execute。
建議盡量避免使用 os.execute。
重啟裝置
-- os.execute('reboot')
-- 建議使用如下調用替代
sys.reboot()
重新載入 SpringBoard(Respring)
-- os.execute('killall -9 SpringBoard;killall -9 backboardd')
-- 建議使用如下調用替代
sys.killall(9, 'SpringBoard', 'backboardd')
重建圖示快取
-- os.execute('su mobile -c uicache')
-- 建議使用如下調用替代
clear.caches()
建立腳本日誌符號連結到腳本目錄
-- os.execute('ln -s /private/var/mobile/Media/1ferver/log/sys.log /private/var/mobile/Media/1ferver/lua/scripts/脚本日志.txt')
-- 建議使用如下調用替代
lfs.link('/private/var/mobile/Media/1ferver/log/sys.log', '/private/var/mobile/Media/1ferver/lua/scripts/脚本日志.txt', true)
常用操作封裝
--[[
以下封裝已經不建議使用,建議使用 XXTouch 內置的 file 模塊函數替代
--]]
local function sh_escape(path) -- XXTouch 原創函數,未經 XXTouch 許可,可以用於商業用途
path = string.gsub(path, "([ \\()<>'\"`#&*;?~$|])", "\\%1")
return path
end
function fdelete(path) -- 刪除一個文件或目錄 (遞歸刪除子項)
assert(type(path)=="string" and path~="", 'fremove 参数异常')
-- os.execute('rm -rf '..sh_escape(path))
-- 建議使用如下調用替代
file.remove(path)
end
function frename(from, to) -- 重命名 (移動) 一個文件或目錄
assert(type(from)=="string" and from~="", 'frename 参数 1 异常')
assert(type(to)=="string" and to~="", 'frename 参数 2 异常')
-- os.execute('mv -f '..sh_escape(from).." "..sh_escape(to))
-- 建議使用如下調用替代
file.move(from, to, 'mo')
end
function fcopy(from, to) -- 拷貝一個文件或目錄 (遞歸拷貝子項)
assert(type(from)=="string" and from~="", 'fcopy 参数 1 异常')
assert(type(to)=="string" and to~="", 'fcopy 参数 2 异常')
-- os.execute('cp -rf '..sh_escape(from).." "..sh_escape(to))
-- 建議使用如下調用替代
file.copy(from, to, 'mo')
end
function mkdir(path) -- 新建一個目錄 (遞歸創建子目錄)
assert(type(path)=="string" and path~="", 'mkdir 参数异常')
-- os.execute('mkdir -p '..sh_escape(path))
-- 建議使用如下調用替代
file.mkdir_p(path)
end
-- 以上是封裝好的函數,將其複製到腳本開頭即可使用。
-- 以下為調用示例(無需複製)
-- 刪除 /var/mobile/1.png
fdelete("/var/mobile/1.png")
-- 將 /var/mobile/2.png 重命名為 /var/mobile/1.png
frename("/var/mobile/2.png", "/var/mobile/1.png")
-- 將 /var/mobile/1.png 移動到 /var/mobile/Media/1ferver/res/3.png
frename("/var/mobile/1.png", "/var/mobile/Media/1ferver/res/3.png")
-- 將 /var/mobile/1.png 拷貝到 /var/mobile/Media/1ferver/res/4.png
fcopy("/var/mobile/1.png", "/var/mobile/Media/1ferver/res/4.png")
-- 建立 /var/mobile/1/2/3/4/ 目錄
mkdir("/var/mobile/1/2/3/4")