-1

So, in a directory /home/pi/remoteinfo/temp/code is have a C program called a.out and a shell file called tempstart.sh.

Inside the tempstart.sh file is the following:

#!/bin/bash
./a.out

Now, when I am in /home/pi/remoteinfo/temp/code I can type ./tempstart.sh and my program will begin execution.

But when I try to run tempstart.h from anywhere outside its directory I get line 3: ./a.out: No such file or directory.

What is happening and how can I fix it?

I'm using a Raspberry Pi 3 with the default OS

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Adam G
  • 3
  • 1

2 Answers2

2

What is happening is that the script will run the command ./a.out in the present working directory, as that's what's written into the script; if you run the script from somewhere else, your binary is not in the present working directory, causing the shell to throw the error you're seeing.

If you want to use the script to invoke the binary regardless of the directory from which it is invoked, you have at least four options:

  • Put a.out into a directory that is already in your PATH and stop specifying the present working directory as the location of a.out (e. g. move a.out to $HOME/bin/a.out, and change the script to simply run a.out rather than ./a.out).
  • Have the script explicitly call the full path to the binary (e. g. rather than ./a.out, instead /path/to/the/location/of/a.out)
  • Have the script add to its PATH the location of the script, and don't specify the current directory as the location of the binary (e. g. PATH="$PATH:/path/to/the/binary"; a.out
  • Have the script set the working directory to the location of the script before trying to run it (e. g. cd /path/to/the/binary; ./a.out)
DopeGhoti
  • 76,081
0

./script.sh would assume that you're running it in the same directory.

Say a.out is in /var/run/a.out, you could change tempstart.sh to contain that full filepath.

At which point you could run the script from anywhere (provided a.out itself doesn't require you to be in a certain directory when running it).

RobotJohnny
  • 1,039
  • 8
  • 18