1

What I used to do is:

  1. Manually open a terminal instance to run the following commands and shell files

  2. Run

    PACKAGE_PATH=/home/userA/package1:/home/userA/package2:$PACKAGE_PAT‌​H
    
  3. run:

    cd /home/userA/scripts
    
  4. run:

    varset.sh
    

    which exports and sets some variables.

  5. run:

    source ff.sh $input $output
    
  6. run:

    bb.sh
    

    which asks me to enter a number during its execution.

  7. run

    bc.sh 8
    

So I had to manually copy and paste these in the terminal before every run.

What I want to do now is to put all this in script file which will automatically open the terminal and run them in sequence so that the next command only runs when the first one is finished, so that I don't have to do this manually anymore.

After applying muru's suggestion found here I used the below script just for steps 1 to 4 but it's not working properly:

gnome-terminal -x sudo -u userA bash -c 'PPACKAGE_PATH=/home/userA/package1:/home/userA/package2:$PACKAGE_PATH; cd /home/userA/scripts; source varset.sh; bash'` 

It is not run in the same sequence I put it.

This successfully opens the terminal but the first line appears in the terminal is a message which is found in varset.sh (although this should be the third command to run) and the other thing is that none of the variables that should be set using this varset.sh is set, for example when I use echo $var1 (which is found in variables.sh) is display nothing which means the variables is not set the only thing that works in variables.sh is the echo message displayed.

The second line that appears in the the directory is the terminal working directory which is set to the /home/userA/scripts.

The third thing is that this command PACKAGE_PATH=PACKAGE_PATH=/home/userA/package1:/home/userA/package2:$PACKAGE_PATH which sets the $PACKAGE_PATH variable is not working.

So if anyone could please advise how to run these commands and shell files in the requested sequence.

Tak
  • 976
  • 3
  • 16
  • 35

1 Answers1

2

Just write a script:

#!/usr/bin/env bash

PACKAGE_PATH=/home/userA/package1:/home/userA/package2:$PACKAGE_PAT‌​H

cd /home/userA/scripts
## You need to source this script if it defines variables. Otherwise
## the variables won't be available to your main script. '.' is the same
## as 'source' but more portable to different shells.
. varset.sh &&


. ff.sh $input $output &&
bb.sh && 
bc.sh 8 &&

The && afater each command ensures that the next one will only be run if the first one exits successfully.

Now you can save the script as ~/scripts/foo.sh, make it executable (chmod 744 ~/scripts/foo.sh) and run it from a terminal:

~/scripts/foo.sh

Alternatively, you can create a .desktop file for it and run it by double clicking as explained here. Just change the Terminal=false line to Terminal=true.

terdon
  • 104,119