0

I am calling a system call from my linux application.

/* Some file.c */ file.c is embedded in an executable called file.elf. this file.elf is present in directory /home/ubuntu/file.elf when i execute the file.elf the echo $BB_PATH prints the executable directory path. i am expecting the directory path to be the path where the script has been placed. i.e /home/ubuntu/Desktop/BIN/BB/Chk_File.sh

How can this be acheived ?

if(!(system("ls /home/ubuntu/Desktop/BIN/BB")))
    {
    /* Test Path : remove after testing */
    dw_flag = system("/home/ubuntu/Desktop/BIN/BB/Chk_File.sh");//Call to execute Script
    dw_flag = WEXITSTATUS(dw_flag); 
    }

this in turns call the file CHK_File.sh

ret_val=0

BB_PATH=$(pwd)

echo $BB_PATH

if [ ! -f ACTION_TAG.txt ]
then
  echo " ACTION_TAG NOT PRESENT "
else

ret_val=1       
fi

echo $ret_val
exit $ret_val
Cœur
  • 37,241
  • 25
  • 195
  • 267
user2598064
  • 157
  • 1
  • 13
  • As I think you've now found out, the current directory is unrelated to the directory where the shell script is found, in general. – Jonathan Leffler Apr 19 '14 at 06:27
  • possible duplicate of [Can a Bash script tell what directory it's stored in?](http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in) – Jonathan Leffler Apr 19 '14 at 06:28

2 Answers2

0

You can use dirname to get the directory of the shell script.

echo `dirname $0`
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • You have to quote ``$0``, you should also use ``$()`` instead of backticks. Here is the trick: ``a='one/two three/four' && dirname $a`` prints two lines: ``one`` and ``three``, while ``a='one/two three/four' && dirname "$a"`` prints ``one/two three`` correctly – Aleks-Daniel Jakimenko-A. Jul 02 '14 at 06:48
0

Adding this below lines in script helped me

if [ -L $0 ] ; then
    DIR=$(dirname $(readlink -f $0)) ;
else
    DIR=$(dirname $0) ;
fi ;


echo $DIR

Now we dont need to worry from which directory the script is called !! Great !!

user2598064
  • 157
  • 1
  • 13