Unable to get $PWD value from script in /etc/init.d

Hi ppl hope to have your advice, i am run out of idea...

I have 3 scripts: a.sh, b.sh, and c.sh

a.sh resided in /etc/init.d

b.sh and c.sh /opt/xSystem

I intend to start my system with "service" command which will trigger my a.sh

service a.sh start

then.
a.sh will trigger b.sh and b.sh will trigger c.sh.

portion content of b.sh

current_dir=$PWD
$current_dir/c.sh start

But i really have no idea with below scenario:

I can't get my system started with service command.
I tried to echo the value of "current_dir" in b.sh script but it seems like UNABLE to get the PWD value as i got message saying "
//c.sh is not valid" returned. It seems like the PWD doesn't returned the working directory which i was expecting "/opt/xSystem"

But ironically, i can get "/opt/xSystem" returned IF i trigger the b.sh at /opt/xSystem itself!

Why it is so???

Please advice...

Many thanks!

PWD is the directory the program was run from -- not the same thing as "whatever directory my program is in". You can do cd / ; /absolute/path/to/my/program.sh to run a program inside /absolute/path/to/my/ even though you're in /.

So it's not refusing to give it to you. It really is in /, not bothering to CD into your folder before running your program.

Try checking the value of $0. It might be /path/to/program.sh, from which you can extract the path.

1 Like

Hi Corona688,

Thank you for your reply!

I at manage to get the thing solved with checking the $0 value using dirname...

thanks alot for your advise here...

else i really no idea how to fix this.

Just like Corona688 has said,
"PWD is the directory the program was run from".

So in order to get the directory in which shell b.sh is located, you should first get access to that directory and then run PWD command:

DIR=$(cd $(dirname "$0"); pwd) 

For your purpose, I will recommend you to define the service path in the parent shell which is a.sh and export the service path variable to the sub shell.

Here are the sample code:

# 
# a.sh
#

# Define the service_path variable and export it, so that $service_path 
# variable will be visible in the sub shell b.sh and c.sh
service_path="/opt/xSystem"
export service_path

cmd="$service_path/b.sh"
case "$1" in
start)echo "a.sh is starting"sh $cmd start;;stop)echo "a.sh is stopping"sh $cmd stop;;esac
#
# b.sh
#

cmd="$service_path/c.sh"
case "$1" in
start)echo "b.sh is starting from $service_path"sh $cmd start;;stop)echo "b.sh is stopping from $service_path"sh $cmd stop;;esac
                                                                             
#
# c.sh
#
case "$1" in
start)
echo "c.sh is starting from $service_path"
;;
stop)
echo "c.sh is stopping from $service_path"
;;
esac

Test:

service a.sh start
a.sh is starting
b.sh is starting from /opt/xSystem
c.sh is starting from /opt/xSystem

service a.sh stop
a.sh is stopping
b.sh is stopping from /opt/xSystem
c.sh is stopping from /opt/xSystem

Also,

It has been done, but it is generally a VERY BAD IDEA to change to a directory and run an arbitrary command. You should either use absolute paths, or check to make sure the previous command completed successfully, or if pedantic enough, do both.

Here's the reason why:

You have /some/directory/full/of/junk.

You run your script out of /usr/local/importanscripts

cd /some/directory/full/of/junk

it fails
rm -rf *

your scripts are now toast.