Ubuntu Shell file

Discussion of chess software programming and technical issues.

Moderators: bob, hgm, Harvey Williamson

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Post Reply
Look
Posts: 238
Joined: Thu Jun 05, 2014 12:14 pm
Location: Iran
Full name: Mehdi Amini
Contact:

Ubuntu Shell file

Post by Look » Tue Oct 29, 2019 1:29 pm

Hi,

I want to change directory in terminal by executing a shell (.sh) file in Ubuntu. Direct method of this works:

Code: Select all

:~$ cd 'Desktop/Folder/Folder 2/'
:~/Desktop/Folder/Folder 2$
But with the cd commands in .sh file , directory is not changed:

cd_test.sh

Code: Select all

cd ~ &&
cd 'Desktop/Folder/Folder 2/'
directory is:

Code: Select all

:~$
What to do ?

User avatar
hgm
Posts: 23725
Joined: Fri Mar 10, 2006 9:06 am
Location: Amsterdam
Full name: H G Muller
Contact:

Re: Ubuntu Shell file

Post by hgm » Tue Oct 29, 2019 2:11 pm

This is because commands you execute from the shell are first forked off as a new process. What you do in that new process does not affect your original shell. A 'cd' command is one of the exceptions to that; it is executed by the shell process itself (or it would not have any lasting effect).

When you execute a shell script, the shell forks off another shell to interpret that script. Any 'cd' commands in that script then only affect that latter shell, so their effect expires as soon as that shell terminates (at the end of the script).

Sven
Posts: 3826
Joined: Thu May 15, 2008 7:57 pm
Location: Berlin, Germany
Full name: Sven Schüle
Contact:

Re: Ubuntu Shell file

Post by Sven » Tue Oct 29, 2019 9:37 pm

Look wrote:
Tue Oct 29, 2019 1:29 pm
What to do ?

Code: Select all

:~$ source cd_test.sh
But why would you want to do that?
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)

Look
Posts: 238
Joined: Thu Jun 05, 2014 12:14 pm
Location: Iran
Full name: Mehdi Amini
Contact:

Re: Ubuntu Shell file

Post by Look » Wed Oct 30, 2019 12:44 pm

Sven wrote:
Tue Oct 29, 2019 9:37 pm
Look wrote:
Tue Oct 29, 2019 1:29 pm
What to do ?

Code: Select all

:~$ source cd_test.sh
But why would you want to do that?
Thanks , this worked for this file. BTW I want this to easily compile XBoard in case of using terminal and such a shell:

Code: Select all

cd 'path' &&
./configure &&
 make &&
sudo make install &&
read -p "Press enter to continue" #keep shell
 

User avatar
hgm
Posts: 23725
Joined: Fri Mar 10, 2006 9:06 am
Location: Amsterdam
Full name: H G Muller
Contact:

Re: Ubuntu Shell file

Post by hgm » Wed Oct 30, 2019 4:49 pm

A script

Code: Select all

cd 'path'
./configure
make
sudo make install
Should work without problems; the effect of the 'cd' would remain valid within the same script. The shell that had executed it would only terminate when it reaches the end of the script. After that the shell from which you invoked the script would take over again, so you won't find yourself in 'path' (presumably the XBoard source directory) anymore. But you would have no business there anyway; the make-install would already have moved the xboard binary to /usr/local/bin.

Look
Posts: 238
Joined: Thu Jun 05, 2014 12:14 pm
Location: Iran
Full name: Mehdi Amini
Contact:

Re: Ubuntu Shell file

Post by Look » Wed Oct 30, 2019 7:33 pm

hgm wrote:
Wed Oct 30, 2019 4:49 pm
A script

Code: Select all

cd 'path'
./configure
make
sudo make install
Should work without problems; the effect of the 'cd' would remain valid within the same script. The shell that had executed it would only terminate when it reaches the end of the script. After that the shell from which you invoked the script would take over again, so you won't find yourself in 'path' (presumably the XBoard source directory) anymore. But you would have no business there anyway; the make-install would already have moved the xboard binary to /usr/local/bin.
By the way in order not to confuse my builds with official ones , can I change the icon of XBoard ?

AlvaroBegue
Posts: 920
Joined: Tue Mar 09, 2010 2:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: Ubuntu Shell file

Post by AlvaroBegue » Thu Oct 31, 2019 6:03 pm

To answer the original question, you are probably using bash as your shell. Instead of writing a script, you can define a bash function (https://linuxize.com/post/bash-functions/), which doesn't spawn a new process, and would be able to change the working directory just fine.

bob
Posts: 20573
Joined: Mon Feb 27, 2006 6:30 pm
Location: Birmingham, AL

Re: Ubuntu Shell file

Post by bob » Thu Oct 31, 2019 8:27 pm

rather than executing the file that does the CD, simply type "source <filename>" and it will execute the cd command and whatever in the SAME (current) process, so that once you source the command and do a "pwd" you will find you are in the new directory.

This works for the c-shell as I use it all the time. Can't say about the other shells although I suspect they would also do this...

Post Reply