Я написал скрипт, который принимает несколько опций (-d, -v, -l, а также --version, --leader и т. Д.), А затем остальную часть текста ($ *)может быть что угодно.Скрипт обрабатывает текст и выплевывает его переформатированный.Это довольно долго, поэтому вот сокращенная версия:
## ––––––––––––––––––– [myScript.sh] –––––––––––––––––––––––––– ##
(1) Настройка по умолчаниюзначения:
declare -- v='1.0' FS=$':\n\r\v\f\t' Application='Finder' Files s='s'
declare -i errors=0 element=0 counter=0 n L=2
(2) Анализ пользовательского ввода:
until [[ -z "$1" || "$1" == '--' || "${1:0:1}" != '-' ]]; do
[ "$Input" ] && unset Input
if [[ "$1" =~ ^(-[Ww]|--[Ww]idth=)([0-9]+)? ]]; then
Input=$(echo "$1" | gsed -re 's|--?W(idth=)?||I' | grep -Eoe '^(0|[1-9][0-9]*)$')
[ -z "$Input" ] && echo "$2" | grep -Eoe '^(0|[1-9][0-9]*)$' && Input="$2" && shift 1
(( Input >= 0 )) && Width="$Input" || unset Width
elif [[ "$1" =~ ^((-[LlIi]|--(([Ll]ead(er|ing)?)?([Ii]n(dent)?)|[Ll]ead(er|ing)?)=)([0-9]+)?)$ ]]; then
Input=$(echo "$1" | gsed -re 's|--?[a-z]+=?||I' | grep -Eoe '^(0|[1-9][0-9]*)$')
[ -z "$Input" ] && echo "$2" | grep -Eoe '^(0|[1-9][0-9]*)$' && Input="$2" && shift 1
(( Input >= 0 )) && L="$Input" || unset L
...
else printf "$(Bold 'Error:') Unrecognized option: '$(Tbrown "$1")'\n\n" >&2
exit 2
fi
shift 1
done
(3) Теперь получите текст:
IFS=''
[ -n "${*}" ] && declare Text="${*}" || Text="$(cat)" ## could also use read instead of cat ##
[ -z "$Text" ] && printf "$(Bold 'Error:') No text entered...\n\n" >&2 && exit 2
(4) Обработка текста:
Text="$(echo "$Text" | gsed -rne '1h;1!H;$g;s|[\x0A-\x0D]+| |g;$p' | expand -t4 )"
echo "$Text" ## (temporary) ##
exit 0 ## (temporary) ##
... ## (process text) ##
... ## (process more) ##
Часть 3 работает для принятия текста, введенного после параметров и при передаче по трубопроводу, но зависает в ожидании ввода, если текст не введен, и не видит текст, переданный в качестве замены процесса ... например:
Примеры:
./myScript.sh -L10 --width=20 'This is a test'
> This is a test
echo 'This is a test' | ./myScript.sh -L10 --width=20
> This is a test
./myScript.sh -L10 --width=20 < <( echo 'This is a test' )</CODE>
> This is a test
./myScript.sh -L10 --width=20
##* Want to stop this *##
> (No output)... hangs waiting on cat (or read) for a ^D
echo 'This is a test' >( ./myScript.sh )
> This is a test /dev/fd/63
> Error: No text entered...
./myScript.sh -L10 --width=20 <<<'This is a test'</CODE>
> This is a test
echo "This is a test" | tee >( ./myScript.sh -L10 --width=20 ) >( ./myScript.sh )
> This is a test
> This is a test
> This is a test
Как заставить скрипт не висеть на коте или читать, ожидая ввода?(без использования тайм-аута или чтения -t, поскольку это только замедляет процесс)?