0

我知道我可以write return to "path to file",但我想知道是否有办法将退格字符写入文件以便在不使用 TextEdit 的情况下删除字符,只需write命令即可。我已经尝试过backspacebckspc甚至尝试过,delete但它们似乎都没有工作。任何帮助,将不胜感激。

4

2 回答 2

1

这将删除桌面上文件“test”的最后一个字符:

set myFile to (path to desktop as text) & "test"
set fRef to (open for access file myFile with write permission)
try
    set eof of fRef to (get eof of fRef) - 1
end try
close access fRef
于 2012-09-10T22:04:55.057 回答
0

即使文件没有以 ASCII 字符结尾,这也会起作用:

set f to POSIX file ((system attribute "HOME") & "/Desktop/test.txt")
set input to read f as «class utf8»
if input is not "" then set input to text 1 thru -2 of input
set b to open for access f with write permission
set eof b to 0
write input to b as «class utf8»
close access b

你也可以使用这样的东西:

shopt -u xpg_echo
x=aä
echo -n "$x" > test.txt
x=$(cat test.txt)
x=${x%?}
echo -n "$x" > test.txt
cat test.txt
rm test.txt
于 2012-09-10T23:51:02.837 回答