redirect命令与输出shell一起执行

我想将执行的命令与输出redirect到一个文件。

例如:#ls> ls_out.txt

ls_out.txt应该是这样的:


LS

file1 fil2


谢谢!

你可以使用“脚本”命令:像这样的东西:

localhost:test_sf user1$ ls file1 file2 file3 localhost:test_sf user1$ script ls_out.txt #starting script command ls_out.txt will contain output Script started, output file is ls_out.txt bash-3.2$ ls file1 file2 file3 ls_out.txt bash-3.2$ exit exit Script done, output file is ls_out.txt =================================================================================== localhost:test_sf user1$ cat ls_out.txt #verify the contents now. Script started on Wed Dec 18 12:05:23 2013 bash-3.2$ ls file1 file2 file3 ls_out.txt bash-3.2$ exit exit 

你只需要摆脱“bash-3.2 $ exit exit”一块。

我使用这样的function:

 echocmd() { echo "$@" "$@" } 

然后

 $ echocmd ls -ltr > ls_out.txt