Ниже приведен полный сценарий, основанный на приведенных выше ответах наряду с проверкой работоспособности, который работает в Mac OS X и должен работать и в других системах Linux / Unix (хотя это не было проверено).
#!/bin/bash
# /7281767/preobrazovanie-formatirovaniya-novoi-stroki-iz-mac-v-windows
# =============================================================================
# =
# = FIXTEXT.SH by ECJB
# =
# = USAGE: SCRIPT [ MODE ] FILENAME
# =
# = MODE is one of unix2dos, dos2unix, tounix, todos, tomac
# = FILENAME is modified in-place
# = If SCRIPT is one of the modes (with or without .sh extension), then MODE
# = can be omitted - it is inferred from the script name.
# = The script does use the file command to test if it is a text file or not,
# = but this is not a guarantee.
# =
# =============================================================================
clear
script="$0"
modes="unix2dos dos2unix todos tounix tomac"
usage() {
echo "USAGE: $script [ mode ] filename"
echo
echo "MODE is one of:"
echo $modes
echo "NOTE: The tomac mode is intended for old Mac OS versions and should not be"
echo "used without good reason."
echo
echo "The file is modified in-place so there is no output filename."
echo "USE AT YOUR OWN RISK."
echo
echo "The script does try to check if it's a binary or text file for sanity, but"
echo "this is not guaranteed."
echo
echo "Symbolic links to this script may use the above names and be recognized as"
echo "mode operators."
echo
echo "Press RETURN to exit."
read answer
exit
}
# -- Look for the mode as the scriptname
mode="`basename "$0" .sh`"
fname="$1"
# -- If 2 arguments use as mode and filename
if [ ! -z "$2" ] ; then mode="$1"; fname="$2"; fi
# -- Check there are 1 or 2 arguments or print usage.
if [ ! -z "$3" -o -z "$1" ] ; then usage; fi
# -- Check if the mode found is valid.
validmode=no
for checkmode in $modes; do if [ $mode = $checkmode ] ; then validmode=yes; fi; done
# -- If not a valid mode, abort.
if [ $validmode = no ] ; then echo Invalid mode $mode...aborting.; echo; usage; fi
# -- If the file doesn't exist, abort.
if [ ! -e "$fname" ] ; then echo Input file $fname does not exist...aborting.; echo; usage; fi
# -- If the OS thinks it's a binary file, abort, displaying file information.
if [ -z "`file "$fname" | grep text`" ] ; then echo Input file $fname may be a binary file...aborting.; echo; file "$fname"; echo; usage; fi
# -- Do the in-place conversion.
case "$mode" in
# unix2dos ) # sed does not behave on Mac - replace w/ "todos" and "tounix"
# # Plus, these variants are more universal and assume less.
# sed -e 's/$/\r/' -i '' "$fname" # UNIX to DOS (adding CRs)
# ;;
# dos2unix )
# sed -e 's/\r$//' -i '' "$fname" # DOS to UNIX (removing CRs)
# ;;
"unix2dos" | "todos" )
perl -pi -e 's/\r\n|\n|\r/\r\n/g' "$fname" # Convert to DOS
;;
"dos2unix" | "tounix" )
perl -pi -e 's/\r\n|\n|\r/\n/g' "$fname" # Convert to UNIX
;;
"tomac" )
perl -pi -e 's/\r\n|\n|\r/\r/g' "$fname" # Convert to old Mac
;;
* ) # -- Not strictly needed since mode is checked first.
echo Invalid mode $mode...aborting.; echo; usage
;;
esac
# -- Display result.
if [ "$?" = "0" ] ; then echo "File $fname updated with mode $mode."; else echo "Conversion failed return code $?."; echo; usage; fi