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
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.
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.
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)
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:
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.
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