Reading a value from the configuration file

Hi,
I have prepared a config file in which I am declaring the value for a country such as: COUNTRY=USA
Now I am trying to read the country from the config file and print a message based on the same. I have written the following code in a script and when executing the script I getting an error.

Code:

cd /root/IMAGE_SAMPLE/
source ./country.conf
if [ "$COUNTRY" != "" ]
then
echo "Installtion in progress for $COUNTRY"
fi

Result:

[root@SNRBBY34844 IMAGE_SAMPLE]# chmod 777 ./Country_Sample.sh
[root@SNRBBY34844 IMAGE_SAMPLE]# ./Country_Sample.sh
: No such file or directory: cd: /root/IMAGE_SAMPLE/
: No such file or directory: ./country.conf
./Country_Sample.sh: line 8: syntax error: unexpected end of file

Can you please help?? Thanks in advance....:slight_smile:

There's your problem. Make sure that the directory you want to change to really exists. Also, reading and (at least) trying to understand error messages can go a long way when debugging a script.

Thanks for your response....The directory does exist, its the correct path and also the file country.conf

Can u pls help??

Did you use a dos/windows editor?

Regards

Are you absolutely sure? Change into that dir on the console, issue a 'pwd', and compare that output with the value set in your script.

Yes I am sure that I am issuing the command in the same directory...

I am using a windows editor

Remove the CR's with:

tr -d '\r' < file > temp
mv temp file

Regards

I did not get you...CR's?? Sorry I am new to Unix...

Dos/windows editors place a line feed and a carriage return character at the end of each line of a text file, but Unix uses only a line feed character.
The code is to get rid of the carriage return.

Regards

Got your point but I am not able to get where and how to use it in my code... :confused:

You don't have to use it in your code but on the UNIX command line. Alternatively, tell your editor to save the file in UNIX encoding (almost all Non-MS editors can do that)

Thanks alot for your help but sorry I am still confused and not getting it :confused:

The lines in text files (including any script language) are separated with a line terminator (called EOL [End Of Line] in C++ for example). But not every OS uses the same kind. DOS/Windows uses 2 characters '\r' (Control character "Carriage Return") and '\n' (Control character "Line Feed"). UNIX uses just the '\n' (Line Feed) character. MacOS uses '\r'.
Since you wrote and saved your script on Windows, the default setting of your editor probably placed the two characters '\r' and '\n' at the end of every line, instead of only '\n', which UNIX would expect. In order to get your file into a format that UNIX will understand properly you have two options:

  1. Upload your file to the UNIX server, and on the command line enter
    text # tr -d '\r' < Country_Sample.sh > Country_Sample_new.sh
    and try running the new file.
  2. Dig through your editors settings and see if it's possible to change the default line terminator from Windows style to UNIX style. (This isn't possible with Notepad or Write)

Note: The two characters Carriage Return and Line Feed come from a time long long ago, when output was still done on needle printers instead of monitors. Carriage Return told the printer to move the printer head to it's leftmost position, Line Feed told it to advance the paper feed by one line

Thanks alot...It works....