Tuesday, June 7, 2022

[SOLVED] exec throws "no such file or directory" but folder exists and so does the file

Issue

I'm using the following exec command:

MainFolder is the main folder inside it mainScript.sh and settings folder

mainScript.sh (Located in MainFolder) :

....
...
...
exec "../settings/helper.sh"

The file helper.sh is located one folder before the current directory , in settings folder but exec throws no such file or directory.

Any idea how to fix this ?


Solution

You have to declare the variable MAINFOLDER in your script mainScript.sh

MAINFOLDER=$(dirname $(readlink -f $0))

and call any script relative to $MAINFOLDER

if settings is in $MAINFOLDER

exec "${MAINFOLDER}/settings/helper.sh"

if settings is located one folder before

exec "${MAINFOLDER}/../settings/helper.sh"


Answered By - ufopilot
Answer Checked By - Marie Seifert (WPSolving Admin)