How can I programmatically access the bash command history in C?
The closest you can get is
alias cmd="history | cmd"
and read standard input from the application.
History command works in a terminal, but doesn't when written as a bash script
Looking at your history only makes sense in an interactive shell. Make that command a function instead of a standalone script. In your ~/.bashrc, put
popular_history() {
history | sort -k2 | uniq -c --skip-fields=1 | sort -r -g | head
}
Does bash shell script leave its command history?
No, it does not:
https://www.gnu.org/software/bash/manual/html_node/Bash-History-Facilities.html
9.1 Bash History Facilities
When the
-o history
option to the set builtin is enabled (see The Set Builtin), the shell provides access to the command history, the list of commands previously typed.
To get current settings, use set -o
command. In interactive shells (with -i
option of bash or in usual bashes from login or terminal) it prints history on
. When used in scripts (bash -c 'set -o'
or when you did ssh machine bash
without tty allocation) it is unset: history off
.
https://www.gnu.org/software/bash/manual/html_node/Interactive-Shell-Behavior.html
6.3.3 Interactive Shell Behavior - When the shell is running interactively, it changes its behavior in several ways.
- Command history (see Bash History Facilities) and history expansion (see History Interaction) are enabled by default. Bash will save the command history to the file named by $HISTFILE when a shell with history enabled exits.
History can be enabled in script with several commands: https://askubuntu.com/questions/546556/how-can-i-use-history-command-in-a-bash-script / https://unix.stackexchange.com/questions/5684/history-command-inside-bash-script - set HISTFILE and (optionally) HISTTIMEFORMAT, enable with set -o history
and then use history
command.
How to add a command to the bash history from python
There is no cross-shell, universally portable option: History is an interactive facility without a close POSIX specification as to how it's implemented.
That said, there are some fixes needed to make the general approach attempted above both functional and safe:
subprocess.Popen(['bash', '-ic', 'set -o history; history -s "$1"', '_', command_string])
- The HISTFILE variable is only set in interactive shells. Thus, you need to run bash with
-i
to have it set at all. set -o history
is similarly needed to turn history on.- Passing
command_string
out-of-band instead of substituting it into the argument following-c
avoids massive security bugs (where trying to append a line to history could execute parts of it instead).
Creating a history command for a shell program using the c language
One fix would be changing
char **cmdsHistory;
to
char *cmdsHistory[10]; //or any desire number/macro
But still your program leaks memory, by calling strdup
and resetting i as 0
after a cycle. Please fix it though.
Fix for leak would be like
if (cmdsHistory[cmdHisC]) {
free(cmdsHistory[cmdHisC]);
cmdsHistory[cmdHisC] = cmdHistory;
}
Make sure you initialize all pointers to NULL
on start up.
Related Topics
How to Change File Extension in Linux Shell Script
What Means "Atomic" System Call
Vagrant - How to Mount The Virtualbox Shared Folder? ("Vboxsf" Not Available)
How to Compare 3 Files Together (To See What Is in Common Between Them)
Logging Memory Access Footprint
Stty: Standard Input: Inappropriate Ioctl for Device
Replace in a CSV File Value of a Column
How to Simulate a Iret on Linux X86_64
Binding on a Port with Netpipes/Netcat
Linux Awk Comparing Two CSV Files and Creating a New File with a Flag
Replace Strings with Evaluated String Based on Matched Group (Elegant Way, Not Using for .. In)
Dreaming of Making My Own Os- What Should I Use? (Suggestions)
Where The Structure "Struct Page" Is Stored on The Linux Kernel
Program Life in Terms of Paged Segmentation Memory
How to Remove Everything Else in a Folder Except Filea
Are Debug Symbols Loaded into Memory on Linux
Cups Linux: Help Printing These Media Types: Ms Excel, Ms Word and HTML