是这样的:

cat "Some text here." > myfile.txt

可能吗?这样myfile.txt的内容现在将被重写为:

Some text here.

这对我不起作用,但也不会抛出任何错误。

对基于cat的解决方案特别感兴趣(不是vim/vi/emacs等)。所有在线的例子都显示cat用于文件输入,而不是原始文本…


当前回答

还有另一种方法

cat > outfile.txt
>Enter text
>to save press ctrl-d

其他回答

我使用以下代码将原始文本写入文件,以更新我的cpu设置。希望这对你有所帮助! 脚本:

#!/bin/sh

cat > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor <<EOF
performance
EOF

cat > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor <<EOF
performance
EOF

这将把文本“performance”写入上面脚本中提到的两个文件。这个例子覆盖文件中的旧数据。

这段代码被保存为一个文件(cpu_update.sh),并使其可执行运行:

chmod +x cpu_update.sh

之后,你可以运行脚本:

./cpu_update.sh

如果您不想覆盖文件中的旧数据,请切换出去

cat > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor <<EOF

with

cat >> /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor <<EOF

这将把文本附加到文件的末尾,而不会删除文件中已经存在的其他数据。

cat > filename.txt

输入文本 直到EOF 保存文本使用:ctrl+d

如果你想读取。txt文件使用

cat filename.txt

还有一件事。txt不是强制性的,它只是供你参考。

对于文本文件:

cat > output.txt <<EOF
some text
some lines
EOF

对于PHP文件:

cat > test.php <<PHP
<?php
echo "Test";
echo \$var;
?>
PHP

还有另一种方法

cat > outfile.txt
>Enter text
>to save press ctrl-d

使用cat将文本写入文件的另一种方法是这样的

cat >file.txt <<< Write something here