0

I am starter in shell scripting. I am trying to open 4 terminal using a script. Here's the code

for i in 1 2 3 4
do
    echo $i
    if [ $i -eq 1 ]; then
        # xterm -e 
        xterm -e "source /home/akshay/openapps.sh; bash"
        opensublime
    elif [ $i -eq 2 ]; then
        xterm -e "source /home/akshay/openapps.sh; bash"
        openspark
    elif [ $i -eq 3 ]; then
        xterm -e "source /home/akshay/openapps.sh; bash"
        openpycharm
    elif [ $i -eq 4 ]; then
        xterm -e "source /home/akshay/openapps.sh; bash"
        runproject 8000
    fi

done

The problem is it is opening a new terminal but for second terminal I need to close first one. How I can avoid closing of previous terminal to open next one.

Complete Code

dpkg -s xterm  &> /dev/null
echo "Checking for Xterm"
if [ $? -eq 0 ]; then
    echo "Package  is installed!"
else
    echo "Package is not installed. Now installing package"
    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install xterm
    echo "Package  is NOW installed!"
fi

for i in 1 2 3 4
    do
        echo $i
        if [ $i -eq 1 ]; then
            xterm -e "source /home/akshay/openapps.sh; bash" &
            opensublime
        elif [ $i -eq 2 ]; then
            xterm -e "source /home/akshay/openapps.sh; bash" &
            openspark
        elif [ $i -eq 3 ]; then
            xterm -e "source /home/akshay/openapps.sh; bash" &
            openpycharm
        elif [ $i -eq 4 ]; then
            xterm -e "source /home/akshay/openapps.sh; bash" &
            runproject 8000
        fi
    done

openapps.sh

function opensublime() {
  cd "/opt/Sublime Text 2"
  eval "./sublime_text"
  cd 
}
function openpycharm() {
  cd "/home/akshay/Documents/pycharm-community-2018.1.4/bin"
  eval "./pycharm.sh"
  cd 
}
function openspark(){
  cd "/opt/Spark/"
  eval "./Spark"
  cd 
}
function runproject(){
  cd "wr_workspace/corporate_admin/"
  eval "source ../env/bin/activate"
  echo "starting server on port $1"
  eval  "python manage.py runserver 127.0.0.1:$1"
  deactivate
  cd
}
  • Should the openspark etc. commands be run inside the terminals? – Kusalananda Jul 12 '18 at 13:29
  • @Kusalananda yes – 108 Jul 12 '18 at 13:38
  • The xterm presence check in the complete version is not actually checking the result of the dpkg command, but of the echo command. And running apt-get upgrade means potentially a full system upgrade: running it in a script that is just supposed to ensure xterm is available is a bit excessive in my opinion... – telcoM Jul 12 '18 at 13:38
1

If you want your script to not wait for the execution of a command to end before proceeding to the next command, add & to the end of the command line.

Also, you don't need a loop if you want to do a different thing each time around the loop. Just list the things you wish to do in sequence.

After a bit of back&forth in the comments, I understand you want the xterm to run a bash shell which should first source your openapps.sh script and then execute a function defined in the script. So something like this should work:

xterm -e bash -c "source /home/akshay/openapps.sh; opensublime; bash" &
xterm -e bash -c "source /home/akshay/openapps.sh; openspark; bash" &
xterm -e bash -c "source /home/akshay/openapps.sh; openpycharm; bash" &
xterm -e bash -c "source /home/akshay/openapps.sh; runproject 8000; bash" &

I'm not certain whether or not the ...; bash part is actually necessary, but I kept it just in case.

Your original commands had a few problems:

xterm -e "source /home/akshay/openapps.sh; bash" 
opensublime

This tells xterm to execute the command line source /home/akshay/openapps.sh; bash. But xterm is not a shell: it can only execute things that are actual executable files on disk. source is an internal command in Bourne-style shells, and so it does not exist as an executable file.

The openapps.sh script is sourced by the instance of bash within the xterm, but the commands opensublime/openspark etc. are attempting to call the functions of openapps.sh in a different shell instance, that does not necessarily have sourced openapps.sh, and so these commands may fail.

So, you'll need xterm to run a shell within the new xterm window, with a specified command line. xterm -e bash -c "<commands>"does that.

If you want to use your opensublime and other functions, you'll need to run those in the same shell that sourced your openapps.sh, otherwise they are likely to just produce command not found error messages. And that's why those commands needed to be moved inside the double quotes.

ulimit settings and exported environment variables can be propagated from a parent process through a non-shell child (xterm in this case) to a grandchild process (bash running within the xterm), but shell functions won't be passed this way; they'll need to be reloaded.

  • Added '&'. Now it is opening 4 terminals but saying 'opensublime: command not found' and same for openspark, openpycharm and for runproject. If i omit that '&' part then it opens the first terminal and also runs the command. any idea why this is happening? – 108 Jul 12 '18 at 13:14
  • In your original script, are the opensublime/openspark/etc. commands actually on the same line as the xterm ... commands? – telcoM Jul 12 '18 at 13:16
  • yes. all commands are on same line of xterm – 108 Jul 12 '18 at 13:20
  • I have updated the complete code – 108 Jul 12 '18 at 13:25
  • What is the /home/akshay/openapps.sh script supposed to do? Is it necessary to run it before opensublime etc.? Are opensublime, openspark, openpycharm and runproject binary applications, scripts, or shell aliases or functions? – telcoM Jul 12 '18 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.