Files
q-tools/files/split-q
2025-11-27 14:21:10 +02:00

52 lines
1.0 KiB
Bash
Executable File

#!/bin/bash
CMD="split -d -a 4"
_help() {
split --help
echo ""
echo "My default switches for split utility"
echo "$CMD -b 500M FILE FILE."
echo "if you want to split using stdin, use --stdin pattern"
}
_helpexit() {
_help
exit
}
for (( i=1; i<=$#; i++ )); do
[[ ${!i} = "-h" ]] && _helpexit
[[ ${!i} = "--help" ]] && _helpexit
done
args=( )
for (( i=1; i<=$#; i++ )); do
j=$(( i + 1 ))
test -f "${!i}" && {
file="${!i}"
# file found, remove from arguments
set -- "${@:1:i-1}" "${@:i+1}"
}
[[ "${!i}" = "-b" ]] && SIZE_SET=1
[[ "${!i}" = "--stdin" ]] && {
PATTERN="${!j}";
set -- "${@:1:i-1}" "${@:i+2}"
i=$(( i + 1 ));
if [ -z "$PATTERN" ]; then echo must give pattern; exit 1; fi;
}
done
[[ "$SIZE_SET" -eq 1 ]] || {
size="-b 500M"
}
if [[ -n "$PATTERN" ]]; then
set -x
cat - | pv | $CMD $size "$@" - "$PATTERN".
exit
else
test -f "$file" || { echo "No such file"; exit 1; }
set -x
pv "$file" | $CMD $size "$@" - "$file".
fi