0

I have the following source.conf which I am reading in a shell script.

    confFileName:"desktop.conf"
    region:"us-ashburn-1"

The shell script is as follows

#!/bin/bash

file="config/source.conf"

while IFS=: read -r key value unused; do
    echo "$value"
done <$file

When I run this script I am only able to print the first line "desktop.conf". Why is this happening?

  • Was the `config/source.conf` created on Windows or copied from a website such that it can have hidden Unicode or DOS carriage returns or be UTF-16 encoded? Run `dos2unix config/source.conf` or post the output of `file config/source.conf` or `hexdump -Cv config/source.conf` – David C. Rankin Nov 02 '22 at 04:45
  • hexdump command gives the following output 00000000 63 6f 6e 66 46 69 6c 65 4e 61 6d 65 3a 22 64 65 |confFileName:"de| 00000010 73 6b 74 6f 70 2e 63 6f 6e 66 22 0a 72 65 67 69 |sktop.conf".regi| 00000020 6f 6e 3a 22 75 73 2d 61 73 68 62 75 72 6e 2d 31 |on:"us-ashburn-1| 00000030 22 |"| 00000031 – Shagufta Oliveyu Methwani Nov 02 '22 at 04:52
  • I tried creating a new source file and manually typing the contents but it still didnt work. I dont understand bash very well but is it possible that desktop.conf that is the value of conffilename is executed and control never comes back? – Shagufta Oliveyu Methwani Nov 02 '22 at 04:55
  • 3
    The hexdump shows that the source.conf file is missing the newline terminator at the end of the second line, so `read` considers that the line is incomplete and returns an error status, causing the loop to exit. You should check how the file is created to see why it's creating malformed files. You can also use `while IFS=: read ... || [ -n "$key" ]; do` to process the last line despite the error. See ["Shell script read missing last line"](https://stackoverflow.com/questions/12916352/shell-script-read-missing-last-line). – Gordon Davisson Nov 02 '22 at 05:33

0 Answers0