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.

  • 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 5: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 5: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 6:28
  • Do you want to run this every single time you open a shell? – Kaz Wolfe Sep 23 '14 at 6:29
  • @Whaaaaaat yes. I want this to happen everytime I run the script.sh script. Thanks! – Tak Sep 23 '14 at 6:31
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.

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.