Most minimalistic - approach #4 and #3, both could be converted into function; #2 my favorite - awk
. #1 uses script
command - very versatile tool, useful for recording command line in general; applicable anywhere, for whatever you want to record.
Approach #1:
There is a /usr/bin/script
command ( which comes with ubuntu by default ) for recording command-line output, which captures everything , along with the prompt and command. To just save one command and its output to specific file, use -c
flag and specify output file. Example
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秒
Approach #2: awk hackery
Awk has system()
function which allows you running shell commands from awk
script or command. The output will show up on the screen , command first, output next. To redirect what you see to a file use >
operator.
That can be done in two ways - ask user to input stuff from stdin or as command line argument. First one is easier to achieve, hence posting that.
(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) Command line args version; not including output to avoid making answer too long. Again, append >
to redirect to file
awk 'BEGIN{for (i=1; i<= ARGC; i++) myString = myString" "ARGV[i]; print myString; system(myString) }' apt-cache depends gnome-terminal
Approach #3: ask bash to do the job for you
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
Redirect to file with >
operator:
bash -c ' MYCOMMAND="apt-cache depends gnome-terminal"; echo $MYCOMMAND ; $MYCOMMAND ' > output.txt
Approach #4:(my second favorite)
Inspired by ByteCommander's post; we can use read
and then run necessary commands in subshell
read command && (printf "COMMAND: %s" "$command";printf "\n+++++++\n"; sh -c "$command")
Sample run:
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
Approach #5:
Use echo
or here string
(aka <<< "string"
) to provide arguments to sh -c
through 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
And if you want , you can use this same trick with an alias:
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 {}; {}"'