Minzkraut
Minzkraut

Reputation: 2187

Bash - Read from file in the same directory as the script file

Simple task i can't get to work:

I have

checkCsv.sh

in my user folder with these contents:

#!/bin/bash
DATEFILE="filedate.txt"
echo $DATEFILE
logDate="$(<$DATEFILE)"
echo $logDate

in the same directory is the file

filedate.txt

The problem is this error:
checkCsv.sh: line 5: filedate.txt: No such file or directory

But the file exists!

EDIT: Im running GNU Bash version 4.3.30(1)


Would appreciate any help

Upvotes: 0

Views: 2212

Answers (1)

drvtiny
drvtiny

Reputation: 715

  • Please, insert this code after the first line of your script:

    TARGET_DIR=$HOME

    # Replace $HOME with the path to filedate.txt if it is not located at $HOME...

    cd "$TARGET_DIR"

  • For debug, IMHO, cat "$DATEFILE" will be more helpful than echo "$DATEFILE"

Upvotes: 1

Related Questions