1

I'm running a shell script.sh which is supposed to open terminal and then run some commands in this opened terminal. What happens is that the terminal starts but the following commands are not executed in this terminal. If anyone can please tell me how after openening the terminal through this script.sh to run in it some other commands. This is is my script.sh file:

#!/bin/bash
gksu -u userA /usr/bin/gnome-terminal PACKAGE_PATH=/home/userA/package1; cd /home/userA/scripts

so the first command gksu -u userA /usr/bin/gnome-terminal opens a terminal instance, I'm asking how to run the two following commands PACKAGE_PATH=/home/userA/package1 and cd /home/userA/scripts in this opened terminal instance using the script.sh

EDIT:

after applying muru's suggestion this is what I used: 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' but it is not run in the same sequence I put it.

The first line appears in the terminal is a message which is found in variables.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 this command in this sequence and to set the variables on the first command and variables in the varset.sh shell file.

Tak
  • 976
  • 3
  • 15
  • 34
  • I'd still suggest gnome-terminal -x sudo -u userA bash -c 'PACKAGE_PATH=/home/userA/package1; cd /home/userA/scripts; bash' instead of running the terminal itself as another user. – muru Sep 23 '14 at 05:47
  • Though i really don't understand why you're stopping at the cd. What is it you want? You say you have to run scripts, but it looks like you want a shell. – muru Sep 23 '14 at 05:52
  • @muru thank you very much for your comment and suggestion! It partially solved my problem. I've edited the question so if you could please have a look. – Tak Sep 23 '14 at 06:28
  • Do you want to run this every single time you open a shell? – Kaz Wolfe Sep 23 '14 at 06:29
  • @Whaaaaaat yes. I want this to happen everytime I run the script.sh script. Thanks! – Tak Sep 23 '14 at 06:31
  • Not what I meant. Whenever you open your shell, or just when running script.sh? – Kaz Wolfe Sep 23 '14 at 06:32
  • oh sorry for the misunderstanding! No, I just want the terminal shell to run like this and execute the scripts and commands in script.sh only when running script.sh. So if I manually opened a terminal then it should work normally without executing anything automatically. – Tak Sep 23 '14 at 06:38
  • Let me ask again, do you want a shell, or do you want to run scripts? For the first, consider exporting the variables. For example: gnome-terminal -x sudo -u userA bash -c ' PACKAGE_PATH=PACKAGE_PATH=/home/userA/package1:/home/userA/package2:$PACKAGE_PATH; cd /home/userA/scripts; source varset.sh', where any variables in varset.sh are also exported. – muru Sep 23 '14 at 07:26
  • @muru Sorry for the misunderstanding! I'll try to explain what I want to do in a better way. What I used to do is: 1- manually open a terminal instance. 2- run this PACKAGE_PATH=/home/userA/package1:/home/userA/package2:$PACKAGE_PAT‌​H script.sh, 3- runcd /home/userA/scripts;4- runvarset.sh5-runsource ff.sh $input $output6- runbb.shwhich requests me to enter a number. 7- runbc.sh 8`. – Tak Sep 23 '14 at 07:43
  • So I had to manually copy and paste these in the terminal before every run. And what I want to do now is to put all this in script file which 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. – Tak Sep 23 '14 at 07:44
  • @user1460166 the comment on exporting variables? Did it work for you? – muru Sep 23 '14 at 19:57

1 Answers1

1

I'd suggested the following as a way to keep the shell open:

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

However, the last bash won't be affected by the assignment of $PACKAGE_PATH or from source varset.sh if they aren't exported.

So:

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

with varset.sh exported should do the trick.

muru
  • 197,895
  • 55
  • 485
  • 740