我想将执行的命令与输出一起重定向到文件。
例如:#ls >ls_out.txt
ls_out.txt 应该是这样的:
ls
文件1 文件2
谢谢!
答案1
您可以使用“脚本”命令:如下所示:
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”部分。
答案2
我使用一个函数来做这种事情:
echocmd() {
echo "$@"
"$@"
}
然后
$ echocmd ls -ltr > ls_out.txt