How to effectively use "konsole"

Where I work they use apple computers but for my personal use I have Leap for developing from home. I’m porting a script over from the work computer to my Leap setup.

On the apple computer I use the following:

osascript<<EOF
    tell application "Terminal"
        do script "$1 $2 $3 $4"
    end tell
EOF

and it will open up a new bash window and execute whatever I want. When it does, the new window inherits quite a few things and even

who am I

will return the user I sudo’ed from.

The equivalent in KDE as far as I can tell, from root user, is:

xauth add $(xauth -f /home/$USER_USER/.Xauthority list|tail -1)
export DISPLAY=:0.0
export XDG_RUNTIME_DIR='/tmp/runtime-root'
konsole --noclose -e "$1 $2 $3 $4"

however doing this loses

who am I

's ability to know who the user that was originally sudo’ed from.

While it’s possible I could pass this information in parameters I was wondering there is a way for the new konsole window bash script to figure out the original sudoer that started the whole process without passing the information as a parameter?

It seems to be more than just

who am i/sudoer

information that gets lost. All my variables that were setup don’t get passed either. Is there way to have a new terminal window inherit all the variables you setup in the terminal window that launched it?

Linux ‘sudo’ is normally set up to use the real and effective user and group IDs of the target user:


 > sudo whoami
root's password:
root
 > 

If you need to use a different behaviour then, the security policy has to be changed.

I’m pretty sure that “who am i” is just reporting the owner of the terminal device that you are using.

When you use “konsole”, a new pseudo-tty device is assigned, and set to be owned by whoever is running the KDE session. So that’s what the answer to “who am i” will show.

There is appparently a Python wrapper for osascript so you can run those scripts on Linux

https://code.activestate.com/pypm/osascript/

Otherwise…
I’d recommend you simply look up the functionality of each of the special shell extensions you want to implement and see if a shell framework already supports…
I’d also recommend you modify Xterm instead of Konsole…
Konsole is already modified by openSUSE and I don’t know if you should tinker with it, there might be unexpected consequences…
Xterm on the other hand is basic and default so not likely to conflict with whatever you want to do to it.
(or you can install another shell).

Examples of shell extenders…

BASH-it
https://github.com/Bash-it/bash-it

Awesome-shell
https://github.com/alebcay/awesome-shell

Terminals-are-sexy
https://terminalsare.sexy/

So ultimately I ended up doing it like this:

File: spawn.sh

#!/bin/bash

TX_FILE_FOR_BG_PROCESSES="$LOG_DIR/varsToTx.sh"
function save_vars_and_join_display() {
  xauth add $(xauth -f /home/$USER_USER/.Xauthority list|tail -1)
  export DISPLAY=:0.0
  export XDG_RUNTIME_DIR='/tmp/runtime-root'
  # The information to derive USER_GROUP & USER_USER gets lost when spawning
  # which we get from "who am i" in bash.
  set |grep -P '^(USER_(USER|GROUP)|PROJECT_NAME_QUERY)=' > "$TX_FILE_FOR_BG_PROCESSES"
  chmod 775 "$TX_FILE_FOR_BG_PROCESSES"
  chown $USER_USER:$USER_GROUP "$TX_FILE_FOR_BG_PROCESSES" # Just to make it easier to delete if we want.
}

case "$(uname -s)" in
    Linux*)
      case $(echo "$XDG_DATA_DIRS" | grep -Po 'gnome|kde|xfce' | head -n1) in
        gnome) 
          save_vars_and_join_display
          gnome-terminal -e "./includes/spawned.sh $1 $2 $3 $4 $5 $6 $7 $8 $9" & # Not yet tested
        ;;
        kde)
          save_vars_and_join_display
          konsole --noclose -e "./includes/spawned.sh $1 $2 $3 $4 $5 $6 $7 $8 $9" &
        ;;
        xfce) echo 'Not implemented.' ;;
      esac
    ;;

    Darwin*)
osascript<<EOF
  tell application "Terminal"
    do script "cd \"`pwd`\"; $1 $2 $3 $4 $5 $6 $7 $8 $9"
  end tell
EOF
    ;;
#    CYGWIN*) ;;
#    MINGW*)  ;;
#    *)       ;;
esac

File: spawned.sh

#!/bin/bash

TX_FILE_FOR_BG_PROCESSES="logs/varsToTx.sh" # This we have to hard code since this is a spawned process and all variables have been lost.
if  -e "$TX_FILE_FOR_BG_PROCESSES" ]; then
  . "./$TX_FILE_FOR_BG_PROCESSES"
fi
. $1 $2 $3 $4 $5 $6 $7 $8 $9