Ubuntu Shell file

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
Look
Posts: 364
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

Ubuntu Shell file

Post by Look »

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 ?
Farewell.
User avatar
hgm
Posts: 27788
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Ubuntu Shell file

Post by hgm »

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: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Ubuntu Shell file

Post by Sven »

Look wrote: Tue Oct 29, 2019 2: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)
User avatar
Look
Posts: 364
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

Re: Ubuntu Shell file

Post by Look »

Sven wrote: Tue Oct 29, 2019 10:37 pm
Look wrote: Tue Oct 29, 2019 2: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
 
Farewell.
User avatar
hgm
Posts: 27788
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Ubuntu Shell file

Post by hgm »

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.
User avatar
Look
Posts: 364
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

Re: Ubuntu Shell file

Post by Look »

hgm wrote: Wed Oct 30, 2019 5: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 ?
Farewell.
AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: Ubuntu Shell file

Post by AlvaroBegue »

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: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Ubuntu Shell file

Post by bob »

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...
BeyondCritics
Posts: 396
Joined: Sat May 05, 2012 2:48 pm
Full name: Oliver Roese

Re: Ubuntu Shell file

Post by BeyondCritics »

Depending on what you want, an alias might help, e.g. in your ~/.bashrc:

Code: Select all

alias cdw='cd ~/my_working_dir'
Keep in mind, that bash is always picky about quoting and whitespace.
Then do logout/login or type bash if you want to test things and then try cdw on the commandline.
Type help alias for short help within bash or man bash and then /alias to search system docs.

An even better solution is to install "autojump", see https://github.com/gsamokovarov/jump
As Ubuntu sudoer type

Code: Select all

sudo apt -y install autojump
In your ~/.bashrc add the line

Code: Select all

. /usr/share/autojump/autojump.sh
and reload bash.
Autojump will start to remember your visited directories. Aftera directory is learned, you can "jump" to it with a few keystrokes.
If ~/my_working_dir is learned, the command

Code: Select all

j my
might work.