How to ensure a file gets read by a bash script without specifying its absolute path?

10 Views Asked by At

There are these two files in the directory:

$ ls -l
script.sh
sources.txt

The script.sh does the following:

#!/bin/bash
sed -i 's/\r//g' ./sources.txt

while read -r source
do
    echo "$source"
done < ./sources.txt

Everything works fine when the script gets executed from the directory containing it, so:

$ sh ./script.sh

But when I tried to test absolute path before putting it into crontab, I got the following error:

$ sh /var/tmp/scripts/script.sh
sed: ./sources.txt: No such file or directory
/var/tmp/scripts/script.sh: line 4: can't open ./sources.txt: no such file

The script will always be coupled with the sources.txt file. How to ensure the script can import the source file without specifying absolute path?

0

There are 0 best solutions below