Derek Dub
Derek Dub

Reputation: 53

Unable to read in contents of hidden file (.file)

(My question relates to and is answered by the question Shell script read missing last line). Due to the fact that people may not know that their issue is that the file they are attempting to read in has no new line at the end, the wording of this post may help others come to that same question linked.

I'm writing a bash script that reads the contents of a hidden file, pertaining particularly to zscaler. This script is checking for the contents of a certain line in the .config.ini file inside the zscaler folder. After some debugging using print statements I realize that it seems the code isn't reading in the file at all. The script can see/find the file with a simple file check but when it comes to reading the file using BASH's read functionality, nothing echo's out with my logic. Below is my code.

#!/bin/bash
result=""
path="/Applications/Zscaler"
configfile="/Applications/Zscaler/.config.ini"
if [ -d $path ]
  then
    if [ -f "/Applications/Zscaler/.config.ini" ]
      then
        while IFS= read -r line; do
          echo "inside the while loop"
          echo "$line"
          if [[ "$line" =~ "regex check here" ]]
            then
              result="True"
            else
              result="False"
          fi
        done < $configfile
      else
        result="False"
    fi
  else
    result="Zscaler not installed"
fi
echo "<result>$result</result>"

I've thought of a possible work around which is to cat the contents of a file into a variable then parse through it that way as I've confirmed the cat command does successfully output the contents to a variable. I'm just curious as to why this isn't working. It's almost as if the hidden file is hidden to the script itself, but only when trying to use the read function; running a cat on the file correctly outputs the contents. The script is run as sudo so none of these files should be off limits to the script itself. In addition, I've run this script substituting an arbitrary text file that I created simply for testing, and this script reads in the contents fine and echo's each line. ***edit The issue never had anything to do with the file being a hidden .file.

Upvotes: 0

Views: 137

Answers (1)

Derek Dub
Derek Dub

Reputation: 53

The issue was the config file not containing a new line at the end of the file. A solution to this was posted on Shell script read missing last line

Upvotes: 0

Related Questions