144 lines
No EOL
3.9 KiB
Bash
144 lines
No EOL
3.9 KiB
Bash
#!/bin/bash
|
|
|
|
# dos2unix mimic script
|
|
# Converts DOS/Windows line endings (CRLF) to Unix line endings (LF)
|
|
|
|
# Function to display usage
|
|
usage() {
|
|
echo "Usage: $0 [OPTIONS] FILE..."
|
|
echo "Convert DOS/Windows line endings to Unix line endings"
|
|
echo ""
|
|
echo "Options:"
|
|
echo " -h, --help Show this help message"
|
|
echo " -n, --newfile Create new file with .unix extension"
|
|
echo " -b, --backup Create backup with .bak extension"
|
|
echo " -q, --quiet Suppress output messages"
|
|
echo ""
|
|
echo "Examples:"
|
|
echo " $0 file.txt"
|
|
echo " $0 -n file.txt"
|
|
echo " $0 -b file.txt"
|
|
echo " $0 *.txt"
|
|
exit 1
|
|
}
|
|
|
|
# Function to convert file
|
|
convert_file() {
|
|
local input_file="$1"
|
|
local output_file="$2"
|
|
local backup="$3"
|
|
local quiet="$4"
|
|
|
|
# Check if input file exists
|
|
if [ ! -f "$input_file" ]; then
|
|
echo "Error: File '$input_file' not found" >&2
|
|
return 1
|
|
fi
|
|
|
|
# Check if file has DOS line endings
|
|
if ! grep -q $'\r' "$input_file"; then
|
|
[ "$quiet" != "true" ] && echo "dos2unix: converting file $input_file to Unix format..."
|
|
[ "$quiet" != "true" ] && echo "dos2unix: $input_file: No conversion needed (already Unix format)"
|
|
return 0
|
|
fi
|
|
|
|
# Create backup if requested
|
|
if [ "$backup" = "true" ]; then
|
|
if ! cp "$input_file" "${input_file}.bak"; then
|
|
echo "Error: Cannot create backup file" >&2
|
|
return 1
|
|
fi
|
|
[ "$quiet" != "true" ] && echo "dos2unix: created backup: ${input_file}.bak"
|
|
fi
|
|
|
|
# Convert line endings using sed
|
|
if sed 's/\r$//' "$input_file" > "$output_file"; then
|
|
[ "$quiet" != "true" ] && echo "dos2unix: converting file $input_file to Unix format..."
|
|
|
|
# If output file is different from input (newfile mode), show message
|
|
if [ "$input_file" != "$output_file" ]; then
|
|
[ "$quiet" != "true" ] && echo "dos2unix: converted to $output_file"
|
|
fi
|
|
|
|
return 0
|
|
else
|
|
echo "Error: Failed to convert $input_file" >&2
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Parse command line arguments
|
|
newfile=false
|
|
backup=false
|
|
quiet=false
|
|
files=()
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
case $1 in
|
|
-h|--help)
|
|
usage
|
|
;;
|
|
-n|--newfile)
|
|
newfile=true
|
|
shift
|
|
;;
|
|
-b|--backup)
|
|
backup=true
|
|
shift
|
|
;;
|
|
-q|--quiet)
|
|
quiet=true
|
|
shift
|
|
;;
|
|
-*)
|
|
echo "Error: Unknown option $1" >&2
|
|
usage
|
|
;;
|
|
*)
|
|
files+=("$1")
|
|
shift
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Check if at least one file is provided
|
|
if [ ${#files[@]} -eq 0 ]; then
|
|
echo "Error: No files specified" >&2
|
|
usage
|
|
fi
|
|
|
|
# Process each file
|
|
exit_code=0
|
|
for file in "${files[@]}"; do
|
|
# Expand wildcards
|
|
for expanded_file in $file; do
|
|
if [ -f "$expanded_file" ]; then
|
|
if [ "$newfile" = "true" ]; then
|
|
# Create new file with .unix extension
|
|
output_file="${expanded_file}.unix"
|
|
else
|
|
# Overwrite original file
|
|
output_file="$expanded_file"
|
|
fi
|
|
|
|
if [ "$newfile" = "true" ]; then
|
|
# For newfile mode, we can directly convert
|
|
convert_file "$expanded_file" "$output_file" "$backup" "$quiet"
|
|
else
|
|
# For in-place conversion, use temporary file
|
|
temp_file=$(mktemp)
|
|
if convert_file "$expanded_file" "$temp_file" "$backup" "$quiet"; then
|
|
mv "$temp_file" "$expanded_file"
|
|
else
|
|
rm -f "$temp_file"
|
|
exit_code=1
|
|
fi
|
|
fi
|
|
else
|
|
echo "Error: File '$expanded_file' not found" >&2
|
|
exit_code=1
|
|
fi
|
|
done
|
|
done
|
|
|
|
exit $exit_code |