12

I want to be able to get a script (ran at startup) to open up a konsole terminal.
When it opens it is to do some persistent things (like change directory and source bashrc) and run a long running program.
If the program crashes or I come in and <ctrl+c> it, it is to start accepting commands from standard input (like 'up-enter' to try again, as if it was interactive the whole time).

I have tried so many things to get it working (I'm currently just trying to get it to ls and revert to interactive on completion);

konsole -e ls
konsole --hold ls
konsole -e /bin/bash -c ls
konsole --hold -e "/bin/bash -c ls"
konsole -e "/bin/bash -i -c ls"
konsole -e /bin/bash -i -c ls
konsole -e "echo ls > /tmp/konsolebash;/bin/bash -i --rcfile /tmp/konsolebash"

echo ls > /tmp/konsolebash
konsole -e "/bin/bash -i --rcfile /tmp/konsolebash"

Is it to do with the quotes? Should I not be using them, should I be escaping something?
Am I even meant to try and execute bash?
I'm running out of ideas but I hope it's even achievable (but hopefully not something embarrassingly simple that I missed).

I'll upvote answers that successfully use other terminal emulators if konsole in particular is the problem (but since the question is specifically about konsole I don't think I can give you the juicy tick)

Hashbrown
  • 3,338
  • 4
  • 39
  • 51

3 Answers3

9

Thanks to @n.st's comments I have made this one liner:

konsole -e /bin/bash --rcfile <(echo "cd /;ls;echo hi | less")

Which is just a shorter version without tmpfiles, using bash process substitution for the following;

echo "cd /;ls;echo hi | less" > /tmp/konsolebash;konsole -e /bin/bash --rcfile /tmp/konsolebash

Which will run some commands, have them display, change the environment, run a long running program (less) and when it ends (:q) will be interactive.
So replace cd /;ls;echo hi | less (the demonstration) with your script.

No history but at least you're in the correct directory now and have any environment variables you may have wanted set up.


Basically the same as my prior attempt;

konsole -e "echo ls > /tmp/konsolebash;/bin/bash -i --rcfile /tmp/konsolebash"

except the file write is outside the konsole execution, I've dropped the -i flag and the execution parameters are not in one quote block


Unfortunately the --rcfile switch causes your ~/.bashrc not to be loaded for those commands, so if you needed an alias or something you'll have to do this;

cat ~/.bashrc > /tmp/konsolebash; echo "commands" >> /tmp/konsolebash;konsole -e /bin/bash --rcfile /tmp/konsolebash

Which just copies your bashrc then appends your commands to the end of it

Hashbrown
  • 3,338
  • 4
  • 39
  • 51
1

A shorter & simpler solution:

konsole -e '$SHELL -c "ls; $SHELL"' &

This one doesn't have the issues with history or sourcing .bashrc either. If your $SHELL environment variable doesn't point to bash, you can specify it manually.

This does spawn a second (bash) shell inside the same terminal window, so some things from the first command might not be carried over.

Answer adapted from an askubuntu answer.

Carolus
  • 359
-1
#!/bin/bash

#
# If not run from konsole, start own one
#
if [ ! -t 0 ]
then
    export konsole_instance_created=1
    konsole -e $0 $* &
    exit
fi

#
# Keep own konsole open after script finished
#
if [ -n "$konsole_instance_created" ]
then
    unset konsole_instance_created
    trap $SHELL EXIT
fi

This code solves two tasks:

  • when the script is started from outside konsole, it is run in a new konsole instance which is not closed on script's finish,
  • when the script is started inside konsole, it behaves as usual.

This code can be placed into a separate file and included by . or source into all scripts that need such behavior.