我想將執行的命令與輸出一起重定向到檔案。
例如:#ls >ls_out.txt
ls_out.txt 應該是這樣的:
LS
文件1 過濾2
謝謝!
答案1
您可以使用“script”命令:如下所示:
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