2

After sudo su -, I want to have both the username and timestamp in the k-shell history list. I have the command line:

trap 'who am i|cut -d" " -f1 |tr "\n" " " && date|read -s' debug

With this command, I expect something like:

UserName Tue Oct 13 15:37:06 CDT 2015 in the history list. However it shows only Tue Oct 13 15:37:06 CDT 2015

This seems to be executing the second action date only, and ignores or overrides the first action.

How can I have the username and timestamp in the history list?

Noting that the reason why the user name is needed is that multiple users have the same impersonation right to the same service user.

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Heinz
  • 913
  • 4
  • 12
  • 22

3 Answers3

1

This worked for me:

$ trap 'echo this; echo that' INT
$ sleep 10
^Cthis
that
$ 

If you need complicated commands, the usual way is to use ksh -c 'compound | pipe | command' but it can get pretty tricky with quoting.

(At least in bash, the subshell is capable of invoking the parent shell's functions, so you could define a function as the commenter suggested. Not tested this with ksh).

UPDATE

This works for me:

trap 'echo $(whoami|cut -d" " -f1 |tr "\n" " " && date)' INT

Consider doing

trap 'echo "$(id -un) $(date)"' INT

Or even

trap 'date "+$(id -un) %DT%T"' INT

Etc.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • if I run`who am i|cut -d" " -f1 |tr "\n" " " && date`, it does give me what I want. The problem appears to be with the trap, or the read. Also how can I do what you suggested? the ^Cthis is a control-C, or it is a ^ and a C? – Heinz Oct 13 '15 at 21:18
  • @Heinz added a few tested updates. `man date` should help getting the format desired – sehe Oct 13 '15 at 21:23
  • sehe, as I said, your examples work when you run it at the command line. I need the user's name and timestamp (the format is not important here) in the history list so we can trace who did what at what time. – Heinz Oct 13 '15 at 21:31
  • sehe, my company's security blocks the access to your URL. – Heinz Oct 13 '15 at 21:43
  • It "just works". Let me upload a recording: http://stackoverflow-sehe.s3.amazonaws.com/8747946f-9e9b-4cdc-95a8-ad48b619b803/kshtest.mp4 – sehe Oct 13 '15 at 21:43
  • I watched you recording, and did not see the user and date in the history. – Heinz Oct 13 '15 at 21:53
  • Oh. Wait. I clearly saw both the username and the date in my recording, so you must be referring to some other history location. Why did you expect it there? Use [`script(1)`](https://en.wikipedia.org/wiki/Script_(Unix)) or similar? – sehe Oct 13 '15 at 21:59
1

I figured out a simple solution:

  1. Create a file trapme in the service user's home directory with one line:

    who am i | cut -d" " -f1 |tr "\n" " " && date

  2. Add one line in .profile:

    trap '~/trapme | read -s' debug

this will create a line after a command with the real user login and the timestamp.

Heinz
  • 913
  • 4
  • 12
  • 22
0

Put all the commands whose output you want to pass to read into a group:

trap '{ who am i|cut -d" " -f1 |tr "\n" " "; date; }|read -s' DEBUG
Mark Plotnick
  • 9,598
  • 1
  • 24
  • 40