最简约的方法#4和#3都可以转换为函数;#2我的最爱- awk
。#1使用script
命令-非常通用的工具,通常用于记录命令行;适用于任何地方,无论您想录制什么。
方法1:
有一个/usr/bin/script
命令(默认情况下ubuntu随附)用于记录命令行输出,该命令捕获所有内容以及提示和命令。要将一个命令及其输出保存到特定文件中,请使用-c
标志并指定输出文件。例
xieerqi:$ script -c 'apt-cache depends gnome-terminal' outputFile.txt
Script started, file is outputFile.txt
gnome-terminal
Depends: gconf-service
gconf-service:i386
Depends: libatk1.0-0
Depends: libc6
Depends: libgconf-2-4
Depends: libgdk-pixbuf2.0-0
(extra output omitted)
Script done, file is outputFile.txt
xieerqi:$ cat outputFile.txt
Script started on 2015年10月22日 星期四 08时58分46秒
gnome-terminal
Depends: gconf-service
gconf-service:i386
Depends: libatk1.0-0
Depends: libc6
Depends: libgconf-2-4
(extra output omitted)
Script done on 2015年10月22日 星期四 08时58分46秒
方法2:awk骇客
Awk具有system()
允许您从awk
脚本或命令运行shell命令的功能。输出将显示在屏幕上,首先显示命令,然后显示输出。要将看到的内容重定向到文件,请使用>
运算符。
这可以通过两种方式完成-要求用户从stdin中输入内容或作为命令行参数。第一个更容易实现,因此发布它。
(1) awk 'BEGIN{ print "Enter command to run: "; getline com < "/dev/stdin"; system(com) }'
awk 'BEGIN{ print "Enter command to run: "; getline com < "/dev/stdin"; system(com) }'
Enter command to run:
apt-cache depends gnome-terminal
gnome-terminal
Depends: gconf-service
gconf-service:i386
Depends: libatk1.0-0
Depends: libc6
Depends: libgconf-2-4
Depends: libgdk-pixbuf2.0-0
Depends: libglib2.0-0
(extra output omitted)
(2)命令行参数版本;不包括输出,以免回答时间过长。同样,追加>
到重定向到文件
awk 'BEGIN{for (i=1; i<= ARGC; i++) myString = myString" "ARGV[i]; print myString; system(myString) }' apt-cache depends gnome-terminal
方法3:要求Bash为您完成工作
xieerqi@eagle:~$ bash -c ' MYCOMMAND="apt-cache depends gnome-terminal"; echo $MYCOMMAND ; $MYCOMMAND '
apt-cache depends gnome-terminal
gnome-terminal
Depends: gconf-service
gconf-service:i386
Depends: libatk1.0-0
Depends: libc6
Depends: libgconf-2-4
Depends: libgdk-pixbuf2.0-0
Depends: libglib2.0-0
使用>
运算符重定向到文件:
bash -c ' MYCOMMAND="apt-cache depends gnome-terminal"; echo $MYCOMMAND ; $MYCOMMAND ' > output.txt
方法4 :(我的第二个最爱)
受ByteCommander的帖子启发;我们可以read
在子外壳中使用然后运行必要的命令
read command && (printf "COMMAND: %s" "$command";printf "\n+++++++\n"; sh -c "$command")
样品运行:
xieerqi:$ read command && (printf "COMMAND READ: %s" "$command";printf "\n+++++++\nOUTPUT\n"; sh -c "$command")
printf "This was a triumph; I'm making a note here - huge success"
COMMAND READ: printf "This was a triumph; I'm making a note here - huge success"
+++++++
OUTPUT
This was a triumph; I'm making a note here - huge success
方法5:
使用echo
或here string
(又名<<< "string"
)提供参数sh -c
通过xargs
xieerqi:$ echo "apt-cache policy gnome-terminal" | xargs -I {} bash -c 'echo {}; {}'
apt-cache policy gnome-terminal
gnome-terminal:
Installed: 3.6.2-0ubuntu1
Candidate: 3.6.2-0ubuntu1
Version table:
*** 3.6.2-0ubuntu1 0
500 http://us.archive.ubuntu.com/ubuntu/ trusty/main amd64 Packages
100 /var/lib/dpkg/status
而且,如果需要,您可以将这个技巧与别名一起使用:
xieerqi:$ printAndRun <<< "apt-cache policy gnome-terminal"
apt-cache policy gnome-terminal
gnome-terminal:
Installed: 3.6.2-0ubuntu1
Candidate: 3.6.2-0ubuntu1
Version table:
*** 3.6.2-0ubuntu1 0
500 http://us.archive.ubuntu.com/ubuntu/ trusty/main amd64 Packages
100 /var/lib/dpkg/status
xieerqi:$ type printAndRun
printAndRun is an alias for 'xargs -I {} bash -c "echo {}; {}"'