hippy
Posts: 5091
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

[SOLVED] Shell script weirdness

Fri May 24, 2019 10:37 pm

I have a shell script "./work-sb" with execute set, a single "cd" command in it, but when I run it nothing happens. It's like it's just ignored. I have other one-liners, no shebangs, and they all work. The directory exists because I can type the same "cd" in at the command line ..

Code: Select all

pi@Pi3B:~/extensions $ ls -l work-sb
-rwxr-xr-x 1 pi pi 43 May 24 23:25 work-sb

Code: Select all

pi@Pi3B:~/extensions $ cat work-sb
cd /home/pi/sb-dev-cleanup/extensions/gpio

Code: Select all

pi@Pi3B:~/extensions $ ./work-sb              <-- HERE !
pi@Pi3B:~/extensions $

Code: Select all

pi@Pi3B:~/extensions $ cd /home/pi/sb-dev-cleanup/extensions/gpio
pi@Pi3B:~/sb-dev-cleanup/extensions/gpio $
Any ideas what's going on ? What I've done wrong ?
Last edited by hippy on Sat May 25, 2019 12:10 am, edited 1 time in total.

pcmanbob
Posts: 5869
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Shell script weirdness

Fri May 24, 2019 11:00 pm

Don't forget when you run a script you are not operating at the command line.

So your script is doing exactly what you told it to and changed the directory that it was working in, but you did not tell it to do anything else.

When you run the same cd command at the command line then you see the command line change the directory it's working in.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

epoch1970
Posts: 2756
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Shell script weirdness

Fri May 24, 2019 11:03 pm

Do you expect that process to actually change the current directory of your terminal?
A process can change dir, but only for itself. Your code goes to a directory, and exits. It works, add an "ls" and see.
https://stackoverflow.com/questions/185 ... mmatically

If you want to create a shortcut command, you can define an alias (usually in your .profile), eg

Code: Select all

me@lab:/tmp$ ls
tmp.iJWu9cp14U
me@lab:/tmp$ alias foo="mkdir $PWD/foo; cd foo"
me@lab:/tmp$ foo
me@lab:/tmp/foo$ 
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

User avatar
scruss
Posts: 2221
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Shell script weirdness

Fri May 24, 2019 11:52 pm

Or do this:

Code: Select all

. ./work-sb
‘.’ (a synonym for source) executes commands from a file in the current shell.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

hippy
Posts: 5091
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Shell script weirdness

Sat May 25, 2019 12:05 am

epoch1970 wrote:
Fri May 24, 2019 11:03 pm
Do you expect that process to actually change the current directory of your terminal?
I did! Too much switching between Windows and Linux too late at night .

I've gone with an "exec $SHELL" after the "cd" and an echoed reminder to type "exit" when done. I'm having enough trouble remembering which slashes, which command name, to have to remember it's dot-space for this particular script file.

I'd also been misled by an example I'd followed earlier which claimed it needed a "cd -" at the end to go back to where I was. Of course it doesn't.

Thanks for the help.

klricks
Posts: 6356
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Shell script weirdness

Sat May 25, 2019 12:11 am

You can use $SHELL

Code: Select all

cd /home/pi/sb-dev-cleanup/extensions/gpio;$SHELL

Or to open a new terminal at a specified directory:

Code: Select all

lxterminal --working-directory=/home/pi/sb-dev-cleanup/extensions/gpio;$SHELL
Unless specified otherwise my response is based on the latest and fully updated Raspbian Stretch w/ Desktop OS.

Return to “Beginners”