Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何使用shell就地修改文件?_Linux_Shell_Redirect - Fatal编程技术网

Linux 如何使用shell就地修改文件?

Linux 如何使用shell就地修改文件?,linux,shell,redirect,Linux,Shell,Redirect,我正在使用linux 我必须将html文件转换为txt。所以我使用html2txt命令 我制作了一个名为“converttohtml.sh”的shell脚本 然后我计划运行查找并转换所有文件 find . -name "*.htm" -exec ./converttohtml.sh {} \; 但是$1>$1重定向不起作用。如果输入文件为1.htm(1.2kb),则输出文件1.htm为0字节。但当我在命令行上执行此操作时 $ html2txt -width 10000 1.ht

我正在使用linux

我必须将html文件转换为txt。所以我使用html2txt命令

我制作了一个名为“converttohtml.sh”的shell脚本

然后我计划运行查找并转换所有文件

   find . -name "*.htm" -exec ./converttohtml.sh {} \; 
但是$1>$1重定向不起作用。如果输入文件为1.htm(1.2kb),则输出文件1.htm为0字节。但当我在命令行上执行此操作时

    $ html2txt -width 10000 1.htm > 1.htm 

它很好用。1.htm仅显示文本。我不知道为什么$1>$1不起作用。

你不能那样做。处理该文件并同时执行重定向将用0字节覆盖该文件,因为shell在运行
html2text
命令之前会处理重定向,这基本上意味着即使在
html2text
处理文件之前,您的文件也是0字节

这样使用它:

html2text -width 10000 "$1" > "$1.tmp" && mv "$1.tmp" "$1"

这意味着仅当上一个命令成功时才运行下一个命令。
html2text -width 10000 "$1" > "$1.tmp" && mv "$1.tmp" "$1"