[flip@flop ~]$ cat helo
echo hello
echo "how are you?"
echo "good day"
echo hi
[flip@flop ~]$ helo
hello
how are you?
good day
hi
[flip@flop ~]$ sh -x helo 2> logfile
hello
how are you?
good day
hi
[flip@flop ~]$ cat logfile
+ echo hello
+ echo how are you?
+ echo good day
+ echo hi
history
command just logs commands that were entered on the command line. So you’ll just see that a particular script has been ran.less
, or more
, or a terminal based browser like w3m
, or lynx
.cat
it, to display it the terminal.bash -xuv /path/to/script
, where /path/to/script
is the path to the script you want to debug.-x
option puts bash into xtrace mode, which shows each line of the script that got executed.-u
option will treat unset variables as errors - you might not want that one set, but I’ve included it in case the reason that you want to log what happens in a script is because you’re having problems getting a script to run correctly. The -u
option can help to identify errors where variables are not set correctly.-v
option puts bash into verbose mode. So it will show more detailed output.thank youHere's one way to record the commands executed in a script to a logfile.
Create a script with the filename: helo
Code:[flip@flop ~]$ cat helo echo hello echo "how are you?" echo "good day" echo hi
Make helo executable and run it. In this case I moved it into ~/bin to place it in the PATH.
Code:[flip@flop ~]$ helo hello how are you? good day hi
Now run it as an argument to: sh -x, and send the "standard error" (2) to the logfile.
The script runs as before and outputs to the screen as intended:
Code:[flip@flop ~]$ sh -x helo 2> logfile hello how are you? good day hi
And the logfile records each execution:
Code:[flip@flop ~]$ cat logfile + echo hello + echo how are you? + echo good day + echo hi
thank youI don’t think you can log the commands inside a script. Thehistory
command just logs commands that were entered on the command line. So you’ll just see that a particular script has been ran.
If you want to see what commands are inside a script, you can simply view it by opening it in a text editor, or a pager likeless
, ormore
, or a terminal based browser likew3m
, orlynx
.
Or you could justcat
it, to display it the terminal.
If you want to see exactly what gets executed in a script, in real-time, you could invoke the script using one or more of bash’s debugging options:
bash -xuv /path/to/script
, where/path/to/script
is the path to the script you want to debug.
That will open a new instance of bash, run the script and will show each line of the script that was executed and its output.
The-x
option puts bash into xtrace mode, which shows each line of the script that got executed.
The-u
option will treat unset variables as errors - you might not want that one set, but I’ve included it in case the reason that you want to log what happens in a script is because you’re having problems getting a script to run correctly. The-u
option can help to identify errors where variables are not set correctly.
And the-v
option puts bash into verbose mode. So it will show more detailed output.
So AFAIK, there’s no way of logging individual commands that were ran inside a script. But if you want to see what does get ran, you could run the script using bash’s debugging options and see exactly what commands are being executed at runtime.
Might not be exactly what you’re looking for, but I think it’s going to be about as good as you’re going to get.
I'm wondering though, would you be able to make a keylogger for yourself with bash scripting? This is something i wanted to do for amusement, but it's seems like you'd need some sort of a low level programming tool in order to do that...I don’t think you can log the commands inside a script.
I don’t think so, not in bash. For an effective key logger, you’d need to create a kernel level module that will redirect all keyboard strokes to a log-file.I'm wondering though, would you be able to make a keylogger for yourself with bash scripting? This is something i wanted to do for amusement, but it's seems like you'd need some sort of a low level programming tool in order to do that...
That is cool!A short-cut to keylogging is an inline usb like this: https://www.keelog.com/usb-keylogger/.