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.

2
  • Replace every ; by &&. Now each command only runs when the previous has quit without an error. – s3lph Sep 23 '14 at 12:46
  • thanks for your comment. I've replaced it but the same thing is happening, no difference. – Tak Sep 23 '14 at 12:49
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.

4
  • thanks for your answer! I just tried it using the .desktop method but not working this is a screen shot of the result dropbox.com/s/ncq2rwgh2cpipxn/result.png?dl=0 where this message appearing appears from the varset.sh – Tak Sep 23 '14 at 13:12
  • 1
    @user1460166 please copy/paste my script directly. I have no idea where ## sourcing the variables comes from, it's not in my script. Also, because of the &&, the script will fail if any of the commands fail. First try running the script yourself from a terminal and then try the .desktop. – terdon Sep 23 '14 at 13:16
  • okay, just tried it on terminal using source foo.sh. Now seems the problem in the .desktop file. this is how it looks: #!/usr/bin/env xdg-open [Desktop Entry] Version=1.0 Type=Application Terminal=true Icon[en_AU]=/home/pics/pic1.png Name[en_AU]=auto_script Exec=/home/userA/Desktop/foo.sh Comment[en_AU]=comment Name=auto_script Comment=comment Icon=/home/pics/pic1.png – Tak Sep 23 '14 at 13:22
  • 1
    @user1460166 no, not source! Who said anything about source? Why are you adding #!/usr/bin/env xdg-open to the .desktop file? Why don't you just come into chat and @ ping me? – terdon Sep 23 '14 at 13:32

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.