One-click: Open terminal, run commands, keep using the terminal

5,584

Solution 1

You need to specify what the terminal to do after executing the command source bin/activate. You want an active bash session so you need to run bash. In addition there is a mes with the quotes. So the script could be:

#!/usr/bin/env bash
konsole --noclose --workdir /my/work/dir/ -e 'bash -c "source bin/activate; exec bash"' &
  • & at the end is added in order to keep the main terminal usable, in case you are executing that script from other terminal window.

  • the exec command could be omitted, and you can use only bash.

Here are few answers of similar questions, dedicated to gnome-terminal:

Solution 2

I am not able to comment, but from what I understand I may suggest

#!/bin/bash
cd /my/work/dir/
source bin/activate
konsole
Share:
5,584

Related videos on Youtube

Douglas James Bock
Author by

Douglas James Bock

Undergrad majoring in Physics and Mathematics. Constantly attempting to cube my spherical cow.

Updated on September 18, 2022

Comments

  • Douglas James Bock
    Douglas James Bock almost 2 years

    I need a bash script that does the following:

    1. open a new terminal
    2. change to a specific directory
    3. run a command in that directory
    4. keep the terminal open for further use

    Specifically I want to:

    1. open the konsole terminal
    2. change to /my/work/dir/
    3. inside /my/work/dir/, run source bin/activate
    4. after that I need to run further commands inside /my/work/dir/, e.g. ls

    A very similar question was given the following answer for the script (adapted to my requirements):

    #!/usr/bin/env bash
    konsole --noclose --workdir /my/work/dir/ -e 'bash -c 'source bin/activate'' 
    

    This does open a new terminal inside /my/work/dir/, but the terminal is not interactive. The

    user@userMachine: /my/work/dir$

    is missing that allows me to run further commands and anything I type (e.g. pwd) returns nothing, just new lines.

  • Douglas James Bock
    Douglas James Bock over 4 years
    Ok This shows some strange behaviour now: The terminal opens in the correct directory. Also the virtualenv seems to be running, since pip freeze gives only the packages installed in the virtualenv. But when I then try to $ deactivate i get deactivate: command not found. I then activate again (again pip freeze gives correct output), then deactivate, and now pip freeze STILL gives the environment packages, not the system wide packages as expected...
  • pa4080
    pa4080 over 4 years
    Hello, @DouglasJamesBock, I really do not have an idea how your environment is setup and what bin/activate actually does. Try to change the shebang of the script to #!/bin/bash and/or source also the $HOME/.bashrc file probably some paths are loaded by this file.
  • Douglas James Bock
    Douglas James Bock over 4 years
    the source bin/activate activates the python virtual environment I previously created in my/work/dir. I have also tried your other suggestions without improvements. I can not even exit the terminal, I guess due to the --noclose. I'm starting to think that Konsole might be to limited for this.
  • pa4080
    pa4080 over 4 years
    @DouglasJamesBock: Please tray with some other terminal emulator, gnomen-terminal I would say - check the references given in the answer.
  • Cinderhaze
    Cinderhaze over 4 years
    This would make the most sense to me! Set up the environment, then launch konsole with the environment set.
  • Douglas James Bock
    Douglas James Bock over 4 years
    This gives the same issues as my first comment under pa4080's answer.