Shortcut Alt-c (bash)
With bash, using the readline utility, we can define a key sequence to place the word calc
at the start and enclose the text written so far into double quotes:
bind '"\ec": "\C-acalc \"\e[F\""'
Having executed that, you type 23 + 46 * 89
for example, then Alt-c to get:
calc "23 + 46 * 89"
Just press enter and the math will be executed by the function defined as calc, which could be as simple as, or a lot more complex:
calc () { <<<"$*" bc -l; }
a (+) Alias
We can define an alias:
alias +='calc #'
Which will comment the whole command line typed so far. You type:
+ (56 * 23 + 26) / 17
When you press enter, the line will be converted to calc #(56 * 23 + 26) / 17
and the command calc
will be called. If calc is this function:
bash
calc(){ s=$(HISTTIMEFORMAT='' history 1); # recover last command line.
s=${s#*[ ]}; # remove initial spaces.
s=${s#*[0-9]}; # remove history line number.
s=${s#*[ ]+}; # remove more spaces.
eval 'bc -l <<<"'"$s"'"'; # calculate the line.
}
ksh
calc(){ s=$(history -1 | # last command(s)
sed '$!d;s/^[ \t]*[0-9]*[ \t]*+ //'); # clean it up
# (assume one line commads)
eval 'bc -l <<<"'"$s"'"'; # Do the math.
}
zsh zsh doesn't allow neither a +
alias nor a #
character.
The value will be printed as:
$ + (56 * 23 + 26) / 17
77.29411764705882352941
Only a +
is required, String is quoted (no globs), shell variables accepted:
$ a=23
$ + (56 * 23 + $a) / 17
77.11764705882352941176
a (+) Function
With some limitations, this is the closest I got to your request with a function (in bash):
+() { bc -l <<< "$*"; }
Which will work like this:
$ + 25+68+8/24
93.33333333333333333333
The problem is that the shell parsing isn't avoided and a *
(for example) could get expanded to the list of files in the pwd.
If you write the command line without (white) spaces you will probably be ok.
Beware of writing things like $(...)
because they will get expanded.
The safe solution is to quote the string to be evaluated:
$ + '45 + (58+3 * l(23))/7'
54.62949752111249272462
$ + '4 * a(1) * 2'
6.28318530717958647688
Which is only two characters shorter that your _bc "6/2"
, but a +
seems more intuitive to me.
Best Answer
This is not as comprehensive as real accounting, and it can be undone easily by a user, but assuming it doesn't have to be a real accounting system and that both BASH and rsyslog are in use, edit the system-wide BASH RC file:
Append to the end of that file:
Above,
logger
records the time, user, command, and command return value. Set up logging for "local6" with a new file:And the contents...
Restart rsyslog:
Log out. Log in. Log rotation:
There is a list of log files to rotate the same way...
Add the new bash-commands log file in that list:
Save and restart/reload rsyslog. (The rotation will eventually overwrite log files, so more thought or configuration may be needed.)
To see all the files that a package has installed:
So you could execute something like that when you install software.