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

 
Thread Tools Search this Thread
Operating Systems Linux Red Hat Unable to get $PWD value from script in /etc/init.d
# 1  
Old 03-29-2012
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

Code:
service a.sh start

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

portion content of b.sh
Code:
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!
# 2  
Old 03-29-2012
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.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-30-2012
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.
# 4  
Old 04-30-2012
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:
Code:
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:
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

Code:
#
# 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

Code:
                                                                             
#
# 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:
Code:
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


Last edited by IKE0000; 04-30-2012 at 04:17 AM.. Reason: typos
# 5  
Old 04-30-2012
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:
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with pwd for script run by double click on script (MacOS High SIerra)

Hello, I have the following script that just archives and clears some log files. #!/bin/bash # script: archive_logs_and_clear # add date to logfile names and copy archive directory # clear logs # change to script directory cd ... (4 Replies)
Discussion started by: LMHmedchem
4 Replies

2. UNIX for Dummies Questions & Answers

Pwd script

when user types "pwd.sh joe" at command line, it generates pwd as abc123 and when user types "pwd.sh jane" at command line, it generates pwd as abc123. pwd will change every 90 days. my pwd file will be always abc123 until i change joe/jane/bob pwd manually. (3 Replies)
Discussion started by: lawsongeek
3 Replies

3. Shell Programming and Scripting

getting pwd of script

how can i get the absolute path of whatever directory a script and/or command is in when it is run? i want to know the directory. say for instance, if i were to run the "who" command, i want to know exaclty where the who command is located. if a user ran a script, i want to know where there... (2 Replies)
Discussion started by: SkySmart
2 Replies

4. Shell Programming and Scripting

script to change shell's pwd

Hi there, i was presented with a challenge that is beyond my current shell knowledge: how can you have a script that executed interactive will change your current working directory? Example (under MacOS): 1. start Terminal and my current working directory is my home folder 2. execute a... (3 Replies)
Discussion started by: gigagigosu
3 Replies

5. Shell Programming and Scripting

How to pass the pwd to an export command through script?

I have an export command which exports the file which I specify to the specified location. It asks for an pwd and again to confirm the pwd, can some one help me to pass the pwd thru the script. cd /opt/var/SecureTransport/bin xml_export /opt/SecureTransport/var/logs/accounts_log.xml... (3 Replies)
Discussion started by: srini0603
3 Replies

6. Shell Programming and Scripting

script for db user pwd

The script to get the db user password from LDAP does not work on AIX 5.3. It's using bash. Our current shell is /usr/bin/shell. How can i make changes to this script so that it can run on aix with current shell with out installing bash. We tried making but din't work. ... (0 Replies)
Discussion started by: noorm
0 Replies

7. Red Hat

init-script failing because of /etc/rc.d/init.d/functions

I encountered a problem on one of our database servers. OS: CentOS 5.5 final Kernel: 2.6.18-238.5.1.el5.028stab085.2 (OpenVZ kernel) We wrote some DB-Start/Stop-scripts ("/db2/admin/scripts_dba/start_services.ksh" and ".../stop_services.ksh") to start the database instances. (Database... (1 Reply)
Discussion started by: bakunin
1 Replies

8. AIX

sync samba pwd with aix5.3 pwd

currently, my samba login works just fine. i want my clients to use aix5.3 account to login to samba so they don't have to change samba pwd and aix pwd. i googled, and vi /usr/lib/smb.conf per some of knowledge base, but i could not get to work. aix5.3 and samba 3.0.24.0 thanks in advace..... (2 Replies)
Discussion started by: tjmannonline
2 Replies

9. Linux

How to I change init levels after typing init 1

Dear all, I typed in init 1 on my redhat box as root and according to wikipedia (http://en.wikipedia.org/wiki/Runlevel): 1 Single-User Mode Does not configure network interfaces, start daemons, or allow non-root logins So now I can't connect back to it. How do I change the init back to 3?... (8 Replies)
Discussion started by: z1dane
8 Replies

10. Shell Programming and Scripting

pwd & cd commands not working in shell script

Here is my script #!/bin/bash pwd cd /var/lib/pgsql Both "pwd" and "cd" are not executed is there any other way i can change the current working directory to /var/lib/pgsql pls help! (9 Replies)
Discussion started by: perk_bud
9 Replies
Login or Register to Ask a Question