3

I am new to writing shell scripts and I am trying to write a script to CD to a folder where my programs are saved for one of my classes, then have a second terminal open that ssh's to a server used for testing the programs that I write. The problem is that I can not find a way to change the directory of the terminal shell to be where my programs are stored.

#!/bin/bash/
gnome-terminal -e "ssh user@foo.bar.edu";
cd /path/to/dir

the problem is that the working directory does not change after the script terminates (it stays as it was when the script was called). I tried calling it except with a '.' in front of the name of the script to get it to run in the current process but the second terminal never opened.

I tried to do the same thing as above except I replaced the third line with

gnome-terminal -e "cd /path/to/dir/";

so that way two terminals would open, but the terminal ment to CD to the path gave an error along the lines of "there was an error creating the child process for this terminal: failed to execute child process 'cd'"

can anyone help me figure out how to solve this problem?

3

Your first version first opens a gnome-terminal, waits until you close it and then changes to the new directory.

Your second version tries to run a command cd instead of a shell, however cd is not a real command but a shell builtin. (See type -a cd for that.)

The question is how gnome-terminal decides what directory to display. It will normally use the current working directory unless overridden by the --working-directory option.

Therefore you can use either:

cd /path/to/dir && gnome-terminal

or

gnome-terminal --working-directory=/path/to/dir

Have a look at man gnome-terminal for available options.

For the ssh part you have to decide whether you want to run you gnome-terminal on the local or remote side. To run it on the remote site you use:

ssh -X user@foo.bar.edu gnome-terminal --working-directory=/path/to/dir

for the local side you can use something like:

gnome-terminal -e "ssh -t user@foo.bar.edu bash -c 'cd /path/to/dir && bash -l'";
  • The problem with the first example is that when I CD the working directory stats the same after the script ends. – bs7280 Feb 25 '15 at 7:04
  • I think you misunderstand the question. I want to have two separate terminals. One where the working directory changes to a given folder, and another that is ssh'd into a remote server that is used to do testing – bs7280 Feb 25 '15 at 7:08
  • for two different gnome-terminals just run it twice. gnome-terminal --working-directory=/path/to/dir & gnome-terminal -e "ssh user@foo.bar.edu" the first one starts a gnome-terminal at the given path and the second one starts a terminal which connects to the remote host. – michas Feb 25 '15 at 7:12
  • Thank you, this should solve my problem when I write it out. But for the sake of learning: how could I change the directory of the terminal that the file was called from? – bs7280 Feb 25 '15 at 7:16
  • Actually I just tested it and the new terminal's working directory is just my default/home folder. – bs7280 Feb 25 '15 at 7:20
0

The working directory would stay changed if you ran the script with a dot (space) before the name of the script.

For instance, I use one named pj which moves me into my python directory for projects. It contains these two lines:

#!/bin/bash
cd /home/pi/python

I made it executable then copied it to /usr/sbin where it is on my $PATH.

To run it, I type:

. pj         (note the dot and the space)

And voila, it changes into that directory and leaves me there.
Couldn't be easier.

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.