Include command in output file?
I would just use a simple function. Add this to your ~/.bashrc
file:
function runcom(){
echo "$@"
## Run the command
$@
}
Now, whenever you want to run a command and print it, you can do:
runcom apt-cache depends kde-window-manager > out
The above produces this file:
$ cat out
apt-cache depends kde-window-manager
kde-window-manager
Depends: perl
Depends: kde-runtime
Depends: kde-style-oxygen
Depends: libc6
|Depends: libegl1-mesa
Depends: <libegl1-x11>
libegl1-mesa
Depends: libgcc1
|Depends: libgl1-mesa-glx
Depends: <libgl1>
libgl1-mesa-swx11
libgl1-mesa-glx
|Depends: libgles2-mesa
Depends: <libgles2>
libgles2-mesa
Depends: libice6
Depends: libkactivities6
Depends: libkcmutils4
Depends: libkdeclarative5
Depends: libkdecorations4abi2
Depends: libkdecore5
Depends: libkdeui5
Depends: libkio5
Depends: libknewstuff3-4
Depends: libkwineffects1abi5
Depends: libkwinglesutils1
Depends: libkwinglutils1abi2
Depends: libkworkspace4abi2
Depends: libplasma3
Depends: libqt4-dbus
Depends: libqt4-declarative
Depends: libqt4-script
Depends: libqtcore4
Depends: libqtgui4
Depends: libsm6
Depends: libstdc++6
Depends: libwayland-client0
|Depends: libwayland-egl1-mesa
Depends: <libwayland-egl1>
libwayland-egl1-mesa
Depends: libx11-6
Depends: libx11-xcb1
Depends: libxcb-composite0
Depends: libxcb-damage0
Depends: libxcb-image0
Depends: libxcb-keysyms1
Depends: libxcb-randr0
Depends: libxcb-render0
Depends: libxcb-shape0
Depends: libxcb-shm0
Depends: libxcb-sync1
Depends: libxcb-xfixes0
Depends: libxcb-xtest0
Depends: libxcb1
Depends: libxcursor1
Depends: libxext6
Depends: libxrandr2
Depends: libxxf86vm1
Breaks: kde-style-bespin
Breaks: kde-style-bespin:i386
Breaks: <kde-style-skulpture>
Breaks: <kde-style-skulpture:i386>
Breaks: kde-workspace-data
Breaks: <kde-workspace-data:i386>
Breaks: kdeartwork-theme-window
Breaks: kdeartwork-theme-window:i386
Breaks: <kdebase-workspace-data>
Breaks: <kdebase-workspace-data:i386>
Breaks: kwin-style-crystal
Breaks: kwin-style-crystal:i386
Breaks: kwin-style-dekorator
Breaks: kwin-style-dekorator:i386
Breaks: kwin-style-qtcurve
Breaks: kwin-style-qtcurve:i386
Replaces: kde-workspace-data
Replaces: <kde-workspace-data:i386>
Replaces: <kdebase-workspace-data>
Replaces: <kdebase-workspace-data:i386>
Conflicts: kde-window-manager:i386
You can do:
tee file.txt <<<'apt-cache depends kde-window-manager' | bash >>file.txt
Same thing using echo
instead of Here strings (<<<
):
echo 'apt-cache depends kde-window-manager' | tee file.txt | bash >>file.txt
tee
will write to STDOUT and also to the filefile.txt
The STDOUT of
tee
i.e.apt-cache depends kde-window-manager
will be fed tobash
to run the command and append the STDOUT tofile.txt
.
Example:
$ echo 'apt-cache depends kde-window-manager' | tee file.txt | bash >>file.txt
$ cat file.txt
apt-cache depends kde-window-manager
kde-window-manager
Depends: kde-runtime
Depends: libc6
|Depends: libegl1-mesa
Depends: <libegl1-x11>
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 {}; {}"'
- Start
script -q outputfile
- Execute your command
- Press Ctrl-D
- Open the file
outputfile
Example
Start script
[aboettger:~/tmp] % script -q ~/Desktop/kwin-depends
Start your command
[aboettger:~/tmp] % apt-cache depends kde-window-manager
<kde-window-manager>
[aboettger:~/tmp] %
Press Ctrl-D
Script done, file is /home/aboettger/Desktop/kwin-depends
Show your command and output
[aboettger:~/tmp] % cat ~/Desktop/kwin-depends
and you will see something like this
[aboettger:~/tmp] % apt-cache depends kde-window-manager
<kde-window-manager>
If you want alias expansion (bash only) you can do it this way :
function runcmd
{
local check_cmd=${BASH_ALIASES[$1]}
if [ -z "$check_cmd" ]; then
check_cmd=$1
fi
shift #skip 1st arg
echo "$check_cmd $@"
$check_cmd $@
}
You can now run
runcmd acd leafpad > out