Skip to content

Instantly share code, notes, and snippets.

@rafzei
Last active October 16, 2017 09:47
Show Gist options
  • Save rafzei/b94d81b707b08b2748a0ad6db416b4f1 to your computer and use it in GitHub Desktop.
Save rafzei/b94d81b707b08b2748a0ad6db416b4f1 to your computer and use it in GitHub Desktop.
WINDOWS -> UNIX EOL bash script
#!/bin/bash
#Functiond used for ensuring that file passed as argument has UNIX EOL
ensure_unix_eol(){
local file=$1
if [[ $# -eq 0 ]]; then
echo -e "\nERROR: No arguments passed. Script cannot continue. Exiting...\n"
exit 1
elif [[ $# -gt 1 ]]; then
echo -e "\nERROR: This function takes only one argument. Script cannot continue. Exiting...\n"
exit 1
fi
if [[ -f $file ]]; then
echo -e "\nINFO: '$file' file found.\n"
file $file | grep -q CRLF
if [[ $? -eq 0 ]]; then
echo -e "\nINFO: Windows EOL (CRLF) detected in the '$file' file. Converting it to UNIX EOL (LF)...\n"
sed -i 's/\r$//g' $file
file $file | grep -q CRLF
if [[ $? -eq 1 ]]; then
echo -e "\nSUCCESS: Windows EOL (CRLF) -> UNIX EOL (LF) conversion on '$file' file successful. Continuuing...\n"
else
echo -e "\nINFO: Windows EOL (CRLF) still present in the '$file'. Interrupting installation...\n"
exit 1
fi
else
echo -e "\nINFO: UNIX EOL (LF) detected in the '$file' file. Continuuing...\n"
fi
else
echo -e "\nERROR: Could not find '$file'. Installation script cannot continue. Interrupting installation...\n"
exit 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment