Problem: I need to remove the EOL/^M/Ctrl-M which comes from writing a document in Windows/Dos and opening it in Unix.
Objective: I need to read a file and assign the read string to a variable, so that I can use the small string as part of a path name. I've read many posts about opening VIM, however, this needs to be done via a SOURCED BASH SCRIPT, will be run on multiple machines, and has many environment variables. All the code needs to be native, therefore, no dos2unix or something similar. I've also tried several lines of code for sed, perl, and awk; however, I haven't had much luck. Any help would be great.
Example of executing the sourced script from the terminal:
username#: . ./Do_something.sh some_string > ~/Do_something_log.txt
Let's pretend we have the following lines inside Do_something.sh
#!/bin/bash
some_string=$1
SOME_PATH="${ANOTHER_PATH}/Some/directory_name"
FILE="${SOME_PATH}/an/even/longer/path/a_file.cfg"
while read line
do
if [ ! -z "{line}" ] ; then
NEW_PATH_VARIABLE=${line}
TOTAL_PATH="${SOME_PATH}/${NEW_PATH_VARIABLE}.cfg"
if [ ! -f "${TOTAL_PATH}" ] ; then
echo "ERROR. FILE NOT FOUND!!!"
fi
if [ "${SOME_STRING}" = "${TOTAL_PATH}" ] ; then
break
fi
fi
done < ${ANOTHER_FILE}
After reading the file, I have an EOL inside my variable name, so it is unable to tag the .cfg extension on the end of the variable which is being assigned to TOTAL_PATH. I only noticed the ^M because I executed less from the terminal using
username#: less ~/Do_something_log.txt
I am looking for the most efficient way to remove remove these (EOL/^M/Ctrl-M)s from inside a script.
Thanks