How to check history commands in shell script

caijq

New Member
Joined
Nov 30, 2022
Messages
3
Reaction score
2
Credits
22
Commands executed on the command line will be recorded in the history.
But the commands executed in the script will not.
So how do I check the commands executed in the script?

thanks a lot
 


osprey

Well-Known Member
Joined
Apr 15, 2022
Messages
399
Reaction score
312
Credits
3,636
Here's one way to record the commands executed in a script to a logfile.

Create a script with the filename: helo
Code:
[[email protected] ~]$ 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:
[[email protected] ~]$ 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:
[[email protected] ~]$ sh -x helo 2> logfile
hello
how are you?
good day
hi

And the logfile records each execution:
Code:
[[email protected] ~]$ cat logfile
+ echo hello
+ echo how are you?
+ echo good day
+ echo hi
 
Last edited:

JasKinasis

Well-Known Member
Joined
Apr 25, 2017
Messages
1,615
Reaction score
2,315
Credits
12,249
I don’t think you can log the commands inside a script. The history 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 like less, or more, or a terminal based browser like w3m, or lynx.
Or you could just cat 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.
 
OP
C

caijq

New Member
Joined
Nov 30, 2022
Messages
3
Reaction score
2
Credits
22
Here's one way to record the commands executed in a script to a logfile.

Create a script with the filename: helo
Code:
[[email protected] ~]$ 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:
[[email protected] ~]$ 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:
[[email protected] ~]$ sh -x helo 2> logfile
hello
how are you?
good day
hi

And the logfile records each execution:
Code:
[[email protected] ~]$ cat logfile
+ echo hello
+ echo how are you?
+ echo good day
+ echo hi
thank you
 
OP
C

caijq

New Member
Joined
Nov 30, 2022
Messages
3
Reaction score
2
Credits
22
I don’t think you can log the commands inside a script. The history 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 like less, or more, or a terminal based browser like w3m, or lynx.
Or you could just cat 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.
thank you
 

CrazedNerd

Well-Known Member
Joined
Mar 31, 2021
Messages
922
Reaction score
394
Credits
7,818
I don’t think you can log the commands inside a script.
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...
 

wizardfromoz

Administrator
Staff member
Gold Supporter
Joined
Apr 30, 2017
Messages
8,083
Reaction score
6,953
Credits
30,971
Moved to Command Line.

Wizard
 

JasKinasis

Well-Known Member
Joined
Apr 25, 2017
Messages
1,615
Reaction score
2,315
Credits
12,249
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 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.
 

CrazedNerd

Well-Known Member
Joined
Mar 31, 2021
Messages
922
Reaction score
394
Credits
7,818
A short-cut to keylogging is an inline usb like this: https://www.keelog.com/usb-keylogger/.
That is cool!

A funny story about how i got interested in this: when i was using windows more than linux and learning computer stuff as the beginning of my hobby, i read about them in that book "Dark Markets" as a way to steal credit card info. However, when i've been learning bash scripts, i was trying to think of how i would do that to myself just so i could look at what i've typed in a file.

So that is just designed to work with windows based on what i see on the site?
 

osprey

Well-Known Member
Joined
Apr 15, 2022
Messages
399
Reaction score
312
Credits
3,636
As I understand it the logging appears in a log.txt file which would be readable from a linux program. I haven't used one, however, I have spied one in a public library, discretely out of open view, but I can't say if it was for malicious purposes like logging passwords to bank accounts, or for the library's monitoring purposes.
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Linux.org Hosting Donations
Consider making a donation

Staff online


Top