maxlen.bash: suppress progress output if not on a terminal

This commit is contained in:
Jakob Unterwurzacher 2021-06-25 14:00:20 +02:00
parent 84e702126a
commit 389aba6a6b

View File

@ -13,6 +13,13 @@ if [[ $# -ne 1 || $1 == -* ]]; then
exit 1 exit 1
fi fi
# Only show live progress if connected to a termial
# https://stackoverflow.com/a/911213/1380267
INTERACTIVE=0
if [[ -t 1 ]] ; then
INTERACTIVE=1
fi
cd "$1" cd "$1"
echo "Testing $PWD" echo "Testing $PWD"
@ -22,7 +29,7 @@ NAME=""
while true ; do while true ; do
NEXT="${NAME}x" NEXT="${NAME}x"
if [[ -e $NEXT ]]; then if [[ -e $NEXT ]]; then
echo "error: file $NEXT already exists" echo "error: file $PWD/$NEXT already exists"
exit 1 exit 1
fi fi
touch $NEXT 2> /dev/null || break touch $NEXT 2> /dev/null || break
@ -45,6 +52,9 @@ echo "${#NAME}"
for CHARS_PER_SUBDIR in 1 10 100 $MAX_DIRNAME ; do for CHARS_PER_SUBDIR in 1 10 100 $MAX_DIRNAME ; do
echo -n " Maximum path length with $(printf %3d $CHARS_PER_SUBDIR) chars per subdir: " echo -n " Maximum path length with $(printf %3d $CHARS_PER_SUBDIR) chars per subdir: "
if [[ $INTERACTIVE -eq 1 ]] ; then
echo -n " "
fi
# Trick from https://stackoverflow.com/a/5349842/1380267 # Trick from https://stackoverflow.com/a/5349842/1380267
SUBDIR=$(printf 'x%.0s' $(seq 1 $CHARS_PER_SUBDIR)) SUBDIR=$(printf 'x%.0s' $(seq 1 $CHARS_PER_SUBDIR))
mkdir "$SUBDIR" mkdir "$SUBDIR"
@ -54,8 +64,10 @@ for CHARS_PER_SUBDIR in 1 10 100 $MAX_DIRNAME ; do
NEXT="$P/$SUBDIR" NEXT="$P/$SUBDIR"
mkdir "$NEXT" 2> /dev/null || break mkdir "$NEXT" 2> /dev/null || break
P=$NEXT P=$NEXT
if [[ $INTERACTIVE -eq 1 ]] ; then
echo -n -e "\b\b\b\b" echo -n -e "\b\b\b\b"
printf %4d ${#P} printf %4d ${#P}
fi
done done
# Then add one character at a time until we hit an error # Then add one character at a time until we hit an error
NAME="" NAME=""
@ -67,7 +79,9 @@ for CHARS_PER_SUBDIR in 1 10 100 $MAX_DIRNAME ; do
if [[ $NAME != "" ]] ; then if [[ $NAME != "" ]] ; then
P=$P/$NAME P=$P/$NAME
fi fi
if [[ $INTERACTIVE -eq 1 ]] ; then
echo -n -e "\b\b\b\b" echo -n -e "\b\b\b\b"
fi
printf %4d ${#P} printf %4d ${#P}
echo echo
rm -R "$SUBDIR" rm -R "$SUBDIR"