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: 240
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: 23772
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: 3830
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: 240
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: 23772
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: 240
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: 922
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: 20640
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...

BeyondCritics
Posts: 353
Joined: Sat May 05, 2012 12:48 pm
Location: Bergheim

Re: Ubuntu Shell file

Post by BeyondCritics » Wed Dec 04, 2019 10:01 am

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.

Post Reply