The error message “No such file or directory” often occurs when the script is unable to locate the file due to one of the following reasons:
1. Relative Path Issue
Ensure the file is in the same directory as the script. If the script is executed from a different working directory, source ./utilities.sh
will look for the file relative to the current working directory, not the script’s location.
Fix: Use the script’s directory dynamically with $BASH_SOURCE
:
source "$(dirname "${BASH_SOURCE[0]}")/utilities.sh"
source "$(dirname "${BASH_SOURCE[0]}")/config.sh"
2. File Permission Issue
Even if the file is marked executable (chmod +x
), it must also be readable. Check the file permissions:
ls -l utilities.sh config.sh
Ensure the files have read (r
) permissions. Example output should include r
:
-rw-r--r-- 1 user group 1234 Dec 11 10:00 utilities.sh
If not, add read permissions:
chmod +r utilities.sh config.sh
3. File Name or Path Mismatch
- Check for Typos: Verify the file name and case sensitivity (
utilities.sh
vsUtilities.sh
). - Debugging Tip: Use
ls
to confirm the file’s presence:ls -l utilities.sh
- Absolute Path: If the relative path isn’t working, try using the absolute path:
source /full/path/to/utilities.sh
4. Execute in the Correct Directory
Ensure you are in the directory containing the script and its dependencies before executing:
cd /path/to/script-directory
./main_script.sh
Alternatively, specify the full path when running the script:
bash /path/to/script-directory/main_script.sh
5. Debugging Tips
- Add debug statements to confirm paths:
echo "Sourcing utilities.sh from $(pwd)"
- Run with verbose and debug mode:This prints each command as it is executed, helping identify the failure point.
bash -x main_script.sh