Reputation: 556
Here's the general command logging function I wrote:
function exec_cmd() {
{
echo "$(date '+%Y-%m-%d %H:%M:%S')" "$1"
$1
} >>./cmd.log
}
When I call this function with exec_cmd "modprobe -r toa || true"
, I got an error "modprobe: FATAL: Module || not found."
Running the same command without the wrapper exec_cmd
works as expected.
The whole script is running in strict mode. The ||
were included because only the error from this command can be ignored.
Upvotes: 2
Views: 939
Reputation: 309
Bash has builtin support for printing commands as they are executed, namely the -x
flag. From man bash
:
-x Print commands and their arguments as they are executed.
So you can revise your script to call set -x
as the first command, then every command after that is printed in the sequence they are called.
#!/bin/bin/env bash
set -x
modprobe -r toa || true
Upvotes: 1
Reputation: 26457
I would write the function like this :
#!/usr/bin/env bash
function exec_cmd() {
{
printf "$(date '+%Y-%m-%d %H:%M:%S') %s\n" "$*"
"$@"
} >> ./cmd.log
}
exec_cmd modprobe -r toa || true
Upvotes: 0