-2
$ ls -l
bin/
$ ls bin/
startup.sh shutdown.sh

I wrote a script named restart.sh inside the bin/ looks like

#!/bin/sh
sh ./shutdown.sh
sh ./startup.sh

Now the bin/ directory looks like this.

$ ls -l bin/
startup.sh shutdown.sh restart.sh

When I execute the restart.sh inside the bin/ it works.

When I execute the restart.sh outside the bin/ it doesn't.

$ ls
bin/
$ ./bin/restart.sh
sh: ./shutdown.sh: No such file or directory
sh: ./startup.sh: No such file or directory
$ cd bin
$ ./restart.sh
I'm happy...

How can I make this work?

Why down-votes? Is this normal?

UPDATE

I appreciate both comments and answers. I already know what . means in system. My apology must be given for making people confused. I accepted the answer because that is what exactly the context to be understood by my question. I should've asked some like How can I make a shell script known to its path..?

I give my answer for someone else. There are plenty of search results for detecting the actual position in shell scripts. I found one at How a BASH script can find its own location.

#!/bin/sh
wd="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo $wd
sh $wd/shutdown.sh
sh $wd/startup.sh
Jin Kwon
  • 135
  • 8

1 Answers1

1

When you type ./scriptname.sh the ./ says "only look in this folder for scriptname.sh".

And you are doing that, in a folder where scriptname.sh does not exist, so the shell returns "No such file or directory".

The fact that your restart.sh script is calling ./shutdown.sh on your behalf makes no difference, you're still in a folder where shutdown.sh doesn't exist, so the shell doesn't find it.

You either need to add your directory to the $PATH (an environment variable containing a list of folders the shell always searches, e.g. export PATH=$PATH:/home/me/bin (won't survive a reboot)) or you need to change the script to call /home/me/bin/shutdown.sh or you need the script to cd /home/me/bin/ and call ./shutdown.sh.

TessellatingHeckler
  • 5,726
  • 3
  • 26
  • 44