Reputation: 3
I'm writing a simple bash script but the commands within the script do not anything. However it works when I copy/paste the commands directly to the command line.
#!/bin/bash
today=$(date +%B-%A-%d-%Y)
expr='clear|ls|cd'
history | grep -v -E $expr > histor$today.txt
history -c
Suggestions?
Another question: is possible to call and execute any command of the command line?
NOTE: I would like to make a script that saves my command history except the commands "clear, ls, cd" and then use crontab to save it periodically.
Upvotes: 0
Views: 329
Reputation: 780949
By default, the history facility is only enabled in interactive shells, not shells running scripts. You can put
set -o history
at the beginning of your script to enable history.
Also, each shell invocation has its own history, it's not shared between shells. So enabling history in the script will not give it access to the history of your interactive shell. So the only history that your script would find are the commands that were executed in the script after set -o history
.
What you should do instead is define a function in your .bashrc
.
gethist() {
history | grep -v -E 'clear|ls|cd' > $(date +'histor%B-%A-%d-%Y.txt')
}
Upvotes: 2