dotfiles/bin/rename_for_shell.sh

36 lines
681 B
Bash
Raw Normal View History

2023-12-23 20:14:04 -07:00
#!/usr/bin/env bash
print_help() {
cat <<HELP
Converts a given file name to something more sensible
USAGE: $(basename "$0") <file-name>
Removes characters that shouldn't be used in file names. Effectively leaves
/[0-9A-z-_.]/ with a few additional rules (like no leading hyphens)
HELP
}
if [[ -z "$1" ]]; then
print_help
exit 1
else
echo "$@" | awk '{
split($0, a, "")
for (i in a) {
c=a[i]
if (!length(name) && c == "-")
continue;
2023-12-23 20:14:04 -07:00
else if (c ~ /[0-9A-Za-z_.\-\/]/)
2023-12-23 20:14:04 -07:00
name = name c
else if (c == " ")
name = name "_"
else if (c == ":" || c == ";")
name = name "-"
}
}
END { print name }'
fi