Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if file exists in ksh

Tags:

ksh

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
like image 856
mibzer Avatar asked Feb 23 '12 21:02

mibzer


People also ask

How do I check to see if a file exists?

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 .

How do you check if a file does not exist in shell script?

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.

How do you check if the file exists in Unix?

[[ 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.


1 Answers

That should read

  if [[ -e "$DATAFILE" ]]
like image 190
glenn jackman Avatar answered Oct 19 '22 19:10

glenn jackman