I wonder why the below tiny script is now working. I created dfFile under /data directory but script does not print the expressions in if statement.
#!/bin/ksh
DATAFILE="/data/dfFile"
echo $DATAFILE
#df -h>/data/dfFile
if [[ -e DATAFILE ]]
then
echo "sa"
echo $DATAFILE
df -h > $DATAFILE
fi
To check if a file exists, you pass the file path to the exists() function from the os. path standard library. If the file exists, the exists() function returns True . Otherwise, it returns False .
In order to check if a file does not exist using Bash, you have to use the “!” symbol followed by the “-f” option and the file that you want to check. Similarly, you can use shorter forms if you want to quickly check if a file does not exist directly in your terminal.
[[ parameter FILE ]] -f : Return true value if file exists and regular file. -r : Return true value if file exists and is readable. -w : Return true value if file exists and is writable. -x : Return true value if file exists and is executable.
That should read
if [[ -e "$DATAFILE" ]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With