6

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
  • 2,782
  • 4
  • 32
  • 47
  • The last two lines look promising. Have you tried them without escaping the quotes around `source ... ls`? – n.st Dec 02 '13 at 01:06
  • sorry that was a copy paste error from writing the question – Hashbrown Dec 02 '13 at 01:09
  • And that didn't work? Strange... What exactly *did* it do? – n.st Dec 02 '13 at 01:09
  • yeah it just brings up a normal interactive shell (no command is visibly ran, just prompt is displayed). It seems to either accept arguments, or be interactive, never both. – Hashbrown Dec 02 '13 at 01:10
  • If nothing else works, you could use `konsole -e "bash -c 'ls'; bash"` -- which is really ugly, but should leave you with an interactive shell when `ls` terminates. – n.st Dec 02 '13 at 01:20
  • but that would forget any `cd` and command history (break `up-enter`). Might as well not not make it interactive and open two shells – Hashbrown Dec 02 '13 at 01:27
  • Since we're already doing ugly things: `ls; echo 'ls' >> $HOME/.bash_history; bash` – n.st Dec 02 '13 at 01:29
  • 2
    I still think however that using `--rcfile` should have done exactly what you need... Edit: In fact, I just checked and `bash --rcfile /tmp/foo` does work for me, while `bash` refuses to start with `-i --rcfile /tmp/foo`. So try omitting the `-i` and see if that helps. – n.st Dec 02 '13 at 01:31
  • Sorry, I overlooked that commands in the rcfile are not added to the history, so `--rcfile` is useless for your application. Looks like we'll need the `echo 'ls' >> .bash_history` hack after all... – n.st Dec 02 '13 at 01:41
  • And here's yet another idea, suggested by ZyX in a comment on [this answer](http://stackoverflow.com/a/6219428/1114687): invoke a `screen` session, then have it run `ls` in the newly created session. – n.st Dec 02 '13 at 01:55
  • thanks for all this, I didn't know it'd be this hard. The comment I upvoted worked (without history though). Even if a `cd` is in the file. You should make an answer so I can upvote it/so others can see it and get help from it too – Hashbrown Dec 02 '13 at 02:45

2 Answers2

7

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
  • 2,782
  • 4
  • 32
  • 47
  • There is no need to copy your bashrc, just use `source ~/.bashrc`. – n.st Dec 02 '13 at 11:21
  • I actually tried that, it doesn't work for the rest of the commands in the list. I.e if `.bashrc` has alias `BOB`, the argument `source ~/.bashrc;BOB;cd /` will have `BOB` fail. I have no idea why, but this works – Hashbrown Dec 02 '13 at 12:10
0

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
  • 329
  • 4
  • 8