我想知道什麼是所有需要轉義的Linux路徑字符。例如,路徑/ home/user1/My Music/song 1.mp3需要在ls命令'ls/home/user1/My \ Music/song \ 1.mp3'的shell中轉義。什麼是所有可能的Linux路徑字符需要轉義
我想編寫一個函數,它接受一個字符串作爲路徑並轉義所有需要的字符。 Scala中我有:
def normalizePath(path: String): String = {
var normPath = path.replaceAll(" ", "\\\\ ")
normPath = normPath.replaceAll("\\]", "\\\\]")
normPath = normPath.replaceAll("\\[", "\\\\[")
normPath
}
知道,有更多的字符需要逃逸。此外,這可能可以通過一個命令(更復雜的正則表達式)來完成?
是'字符串正常化(字符串s){回報「\」「+ s +」\「」; }' – Novikov 2010-11-05 03:21:22
爲什麼不首先使用shell呢? – 2010-11-05 03:24:59
答案取決於您計劃使用路徑的位置。例如,如果它要傳遞給shell,那麼你需要轉義shell解釋的特殊字符(可能包括'!','*','?'等,也許取決於shell是什麼正在使用)。如果將路徑放入URL中,則需要轉義不同的字符集,並使用不同的轉義機制(%-encode)。如果您直接使用Linux系統調用的路徑,則根本不需要轉義。 – 2010-11-05 03:49:18