Bash: the run-command to the result header -
i run script different parameters , on. when run script std-outputs header: header must contain command run. how can have running command in header?
goal
$ head ~/dominances_0_0.25_0.5_0.75_1.txt ----------------------------------------- system testing file bepo timestamp: 201305041511 pwd: /users/abc/abc/systemtestfiles run-command: ./bin/diffexpectedactual.sh > ~/dominances_0_0.25_0.5_0.75_1.txt -----------------------------------------
failure
$ ./bin/diffexpectedactual.sh > ~/dominances_0_0.25_0.5_0.75_1.txt $ head bin/diffexpectedactual.sh #!/bin/bash echo "-----------------------------------------" echo "system testing file bepo" echo "timestamp: " `date +"%y%m%d%h%m"` echo "pwd: " `pwd` echo "command: " some_command_here_to_tell_the_run_command?!?! echo "-----------------------------------------"
you can easily, 3 step process. first, must setup bash executing precmd
. copying form question, , from here should do:
1st step - save next file e.g. makelastcomm.sh
set -o functrace > /dev/null 2>&1 shopt -s extdebug > /dev/null 2>&1 preexec () { temp=$(tty); echo "$1" >/tmp/lastcommand.${temp:5} } preexec_invoke_exec () { [ -n "$comp_line" ] && return # nothing if completing local this_command=`history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g"`; preexec "$this_command" } trap 'preexec_invoke_exec' debug
2nd step - source current bash
source makelastcomm.sh
3rd step - script should start (in examples called hhhqst
)
#!/bin/bash temp=$(tty) cat << eof --------------------------- system testing file bepo timestamp $( date +"%y%m%d%h%m" ) pwd: $( pwd ) command: $(cat /tmp/lastcommand.${temp:5}) --------------------------- eof #your main script here echo "running the main script example date command" lc_all=c date
the result. when run hhhqst
as
bash hhhqst
will get
--------------------------- system testing file bepo timestamp 201305041939 pwd: /users/jm/tmp command: bash hhhqst --------------------------- running the main script example date command sat may 4 19:39:13 cest 2013
when run redirect example as
./hhhqst >/tmp/hhh.out
the /tmp/hhh.out
contain
--------------------------- system testing file bepo timestamp 201305041940 pwd: /users/jm/tmp command: ./hhhqst >/tmp/hhh.out --------------------------- running the main script example date command sat may 4 19:40:39 cest 2013
done.
the principe. hooking bash debug trap put /tmp/lastcomm.your_terminal
last command history. so, when run script script read content above file.
Comments
Post a Comment