1

I run a script with different parameters and so on. When I run the script it std-outputs a header: the header must contain the command by which it was run. How can I have the running command in the header?

Goal

$ head ~/dominances_0_0.25_0.5_0.75_1.txt 

-----------------------------------------
SYSTEM TESTING FILE for 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 for BEPO" 
echo "TIMESTAMP: " `date +"%Y%m%d%H%M"`
echo "PWD: " `pwd`
echo "COMMAND: " SOME_COMMAND_HERE_TO_TELL_THE_RUN_COMMAND?!?!
echo "-----------------------------------------"
hhh
  • 50,788
  • 62
  • 179
  • 282

2 Answers2

2

The name of the executed file and the arguments are typically stored in $0 and $*. You can use a here document to simplify the script:

#! /bin/bash
cat << EOF
-------
SYSTEM TESTING FILE for BEPO
TIMESTAMP $( date +"%Y%m%d%H%M" )
PWD: $( pwd )
COMMAND: $0 $*
-------
EOF

Note that the redirection is not part of $*. It's a bit tricky to get this information. You can start with the following example and improve your script according to the requirements from there:

COMMAND: $0 $* > $( readlink -mn "/proc/$$/fd/1" )
nosid
  • 48,932
  • 13
  • 112
  • 139
  • Sorry there is one case when this does not work `"./bin/diffExpectedActual.sh 2324 24 > ../notCaptured.txt"`: now this command does not store the stdout command to the file `notCaputed.txt`. It stores only this part `./bin/diffExpectedActual.sh 2324`, not the `> ../notCaptured.txt` part. Why? – hhh May 04 '13 at 14:39
  • I cannot understand the `$0 $* > $( readlink -mn "/proc/$$/fd/1" )`, what did you want to demo with it? I get errs such as `readlink: illegal option -- m`. You are doing something with filedesciptors, thinking... – hhh May 04 '13 at 14:43
  • @hhh: Try it without `-m` if the readlink version does not support this option. `/proc/$$/fd/1` contains some information about the standard output of the bash script. For example try `ls -l /proc/self/fd/2 2> /dev/null`. – nosid May 04 '13 at 14:52
  • `$0 $* > $( readlink -n "/proc/$$/fd/1" )` fired `-bash: $( readlink -n "/proc/$$/fd/1" ): ambiguous redirect`? – hhh May 04 '13 at 16:53
1

You can do it easily, but it is an 3 step process. First, you must setup your bash for executing precmd. Copying form this question, and from here you should do:

1st step - save the next into any 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  # do 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 it into the current bash

source makelastcomm.sh

3rd step - you script should start as (in my examples it is called hhhqst)

#!/bin/bash
temp=$(tty)
cat << EOF
---------------------------
SYSTEM TESTING FILE for BEPO
TIMESTAMP $( date +"%Y%m%d%H%M" )
PWD: $( pwd )
COMMAND: $(cat /tmp/lastcommand.${temp:5})
---------------------------
EOF

#your main script here
echo "running the the main script for example the date command"
LC_ALL=C date

The result. When will run hhhqst as

bash hhhqst

will get

---------------------------
SYSTEM TESTING FILE for BEPO
TIMESTAMP 201305041939
PWD: /Users/jm/tmp
COMMAND: bash hhhqst
---------------------------
running the the main script for example the date command
Sat May  4 19:39:13 CEST 2013

when will run with a redirect for example as

./hhhqst >/tmp/hhh.out

the /tmp/hhh.out will contain

---------------------------
SYSTEM TESTING FILE for BEPO
TIMESTAMP 201305041940
PWD: /Users/jm/tmp
COMMAND: ./hhhqst >/tmp/hhh.out
---------------------------
running the the main script for example the date command
Sat May  4 19:40:39 CEST 2013

done.

The principe. Hooking bash debug trap we put into the /tmp/lastcomm.your_terminal the last command from the history. So, when you run the script the script only read the content from the above file.

Community
  • 1
  • 1
clt60
  • 62,119
  • 17
  • 107
  • 194