0

Want to execute the following commands from any single script. How to do this?

Whenever I'm starting my putty session, on my unix machine, bash shell is coming, then I need to enter the following commands to setup my workspace.

Below is the exact procedure which I'm doing.

    -bash-4.2$ bash
    [userName@SystemName ~]$ su

getting admin rights
    [root@SystemName userName]#

setting up environment variable for python project
    [root@SystemName userName]# source xxxx.env

setting the path for my local workspace
    [root@SystemName userName]# setxxxx /home/userName/SourceCode/

Now, I want all these commands to get executed from single script. Thus, I had putted all these commands into a single shell script and tried to execute but only first instruction got executed. Why?

start.sh

    #!/bin/bash
    bash
    su
    source ssdt.env
    setssdt /home/userName/projectName/
akD
  • 1,137
  • 1
  • 10
  • 15
  • 3
    because su creates new shell, try to change first line of script to `#!/bin/su -c /bin/bash` and remove `su` from body of script – zb' Mar 16 '16 at 05:13
  • 1
    Why don't you just run `su -c 'source ssdt.env && setssdt /home/adubey/Test_Tip/'`? – heemayl Mar 16 '16 at 05:13
  • If you _must_ encapsulate the `su` in the script rather than invoke it externally, consider prefixing each command with `sudo` instead. And, as others say remove `bash` and `su` from the script, as both of them will invoke a new shell. – paddy Mar 16 '16 at 05:14
  • 1
    @paddy `sudo` on each line will have the same "new session" problems. – Etan Reisner Mar 16 '16 at 05:15
  • Why are you running all that as `root`? Why not as your normal user? – Etan Reisner Mar 16 '16 at 05:15
  • 1
    I would add this in the beginning of the script `[ $UID != 0 ] && exec sudo "$0" "$@"` – anishsane Mar 16 '16 at 05:32

1 Answers1

0

The first command in your script is bash, which opens a new shell that will run until you exit from it, before it will continue executing the other commands in your script. If you're just trying to make the script execute with bash, that's what the #! line at the top does. The su command will also open a subshell that will have to exit before the other commands will be run with normal privileges.

kyle
  • 446
  • 4
  • 10