0

Can anyone help me with the below. I do not understand what is wrong, Not getting any output. My requirement is to read a file and check if it's not empty and to print the content line by line.

   #!/bin/ksh
   echo " enter file name "
   read $file
   if [ -f "$file" ] && [ -s "$file" ]
    then 
   echo " file does not exist, or is empty "
      else
   while IFS='' read -r line || [[ -n "$file" ]];do
      echo "$line"
   done
   fi
0

1 Answer 1

1

read $file should be read file

Your comparison logic is backwards. The comparison if [ -f "$file" ] && [ -s "$file" ] is 'if the file is a regular file and not empty go into error case'. You want 'if the file is not regular or the file is empty go into error case' if [ -f "$file" ] -eq 0 || [ -s "$file" ] -eq 0.

Per ksh file read should be

 while IFS='' read -r line 
   do
      echo "$line"
   done < "$file"

Further Reading On ksh redirection

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.