acme/bin/t2s

43 lines
755 B
Text
Raw Normal View History

#!/usr/bin/env bash
2017-01-26 19:29:57 +03:00
n_spaces="$1"
2017-01-26 19:29:57 +03:00
if [ ! "$n_spaces" ]; then
n_spaces=4
fi
2017-01-26 19:29:57 +03:00
if [ $n_spaces -lt 1 ]; then
echo 'Sorry. Number of spaces should be a positive number'
exit 1
fi
2017-01-26 19:29:57 +03:00
space=''
i=0
while [ true ]; do
space="$space "
i=$(( $i + 1 ))
if [ $i -eq $n_spaces ]; then
break
fi
done
2017-01-26 19:29:57 +03:00
while IFS='' read -r line || [[ -n "$line" ]]; do
if [[ ${line:0:1} != ' ' ]]; then
echo "$line"
continue
fi
# Line length
line_len=${#line}
# Get leading tabs and their length
tabs=$(echo "$line" | sed -n 's/^\( *\)\(.*\)$/\1/p')
tabs_len=${#tabs}
line_without_tabs_len=$(( $line_len - $tabs_len ))
line_without_tabs=${line:$tabs_len:$line_without_tabs_len}
spaces=$(echo "$tabs" | sed "s/ /$space/g")
echo "$spaces$line_without_tabs"
done