Run script from the directory it is in


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Run script from the directory it is in
# 1  
Old 02-13-2009
Run script from the directory it is in

I have a script at /java/custom/build.sh

I want to force the user to execute the script from the /java/custom directory ONLY. In other words if the user is in /java I want to exit if they execute custom/build.sh

The directory structure may change from the env to env, so I can't use variables (say $SCRIPT_PATH=/java/custom).

Thanks for the help in advance
# 2  
Old 02-13-2009
I am not sure how to tell what directory the script is currently residing in, but PWD will tell you the directory that the person who is running it is in. er, hope you understand that. ^_^

Code:
#!/bin/bash

if test $PWD != '/java/custom'; then
        echo "You can not run the script from any directory except /java/custom";
        exit 1;
fi

# 3  
Old 02-13-2009
something along these lines:
Code:
#!/bin/ksh

iam="$(whence $0)"
iamDir="${iam%/*}"
iamDir="${iamDir%/.}"

if [ "${iamDir}" != "${PWD}" ]; then
  echo "you cannot run [${0}] from anywhere BUT [${iamDir}]"
  exit 1
fi

echo "I'm happy"

# 4  
Old 02-14-2009
Wish it was that easy, Like I said the directory structure may change from env to env so I can't use $PWD against a predefined path. It has to be generic.
# 5  
Old 02-14-2009
Thanks, vgersh99

That's exactly what I was looking for.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script run in a case statement call to run a php file, also Perl

Linux System having all Perl, Python, PHP (and Ruby) installed From a Shell script, can call a Perl, Python, PHP (or Ruby ?) file eg eg a Shell script run in a case statement call to run a php file, also Perl or/and Python file??? Like #!/usr/bin/bash .... .... case $INPUT_STRING... (1 Reply)
Discussion started by: hoyanet
1 Replies

2. Shell Programming and Scripting

How to run a script/command on all the directories in a directory tree?

How to run a script/command on all the directories in a directory tree? The below script is just for the files in a single directory, how to run it on all the directories in a directory tree? #!/bin/sh for audio_files in *.mp3 do outfile="${audio_files%.*}.aiff" sox "$audio_files"... (2 Replies)
Discussion started by: temp-usr
2 Replies

3. Shell Programming and Scripting

Include libexec directory for script when run

Hi everyone! This is my first post, so bear with me. I have this script, vpm, which make use of the libexec directory for commands intended to be run by the bin/vpm binary. I want to add the libexec directory to $PATH when running the vpm binary. I don't want the script itself to try figure... (2 Replies)
Discussion started by: KevinSjoberg
2 Replies

4. Shell Programming and Scripting

How to run the Shell Script from external directory using java?

Hi, I have created a Shell Script and invoke through java using Process Builder It's working fine, if (Shell script file ) in the same directory as java file. By Problem: How to run the Shell Script file( resides in external directory) using java. What configuration i have... (1 Reply)
Discussion started by: nanthagopal
1 Replies

5. Shell Programming and Scripting

Shell script to run all the python scripts from particular directory

I have N number of python scripts which i am receiving from REST web server and i am saving them in /home/mandar/ . Now i want to write a script to run all the python scripts from this directory and monitor them every minute...if any process is dead or hung it should be restarted by this script. ... (0 Replies)
Discussion started by: Mandar Nandale
0 Replies

6. Shell Programming and Scripting

how to run an already made script run against a list of ip addresses solaris 8 question

how to run an already developed script run against a list of ip addresses solaris 8 question. the script goes away and check traffic information, for example check_GE-VLANStats-P3 1.1.1.1 and returns the results ok. how do I run this against an ip list? i.e a list of 30 ip addresses (26 Replies)
Discussion started by: llcooljatt
26 Replies

7. UNIX for Dummies Questions & Answers

How do I run a .bin file in another directory in a script?

The way this works from the command prompt is: ___________________________________________ cd /data/local/bin chmod 0755 file.bin ./file.bin _______________________________________________ How do I make this happen in a script. The file must be run in its directory but I can not get the... (2 Replies)
Discussion started by: chrstdvd
2 Replies

8. Shell Programming and Scripting

script that reads all the scripts in the directory and run them within that script

Hi I am trying to write a shell script that is reading all the scripts in the current directory (currently 5) and is allowing me to run the scripts that is in the directory. I want that this scripts asks te user to execute 1 of the listed scripts. I have 4 sample scripts in the directory:... (8 Replies)
Discussion started by: I-1
8 Replies

9. Shell Programming and Scripting

How to make a script to run everytime a new file is copied into a directory??

Hi folks I have a unix script script1 in a directory folder1 and also I have few input log files in this directory. My input log files will be copied into this directory folder1 from the portable thumb drive. Now what I want is I need to run this script1 whenever any new file is copied... (2 Replies)
Discussion started by: ks_reddy
2 Replies
Login or Register to Ask a Question