Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6112263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:43:31+00:00 2026-05-23T14:43:31+00:00

I wrote a script that takes a bunch of options ( -d , -v

  • 0

I wrote a script that takes a bunch of options (-d, -v, -l, as well as --version, --leader, etc…) and then the rest of the text ($*) can be anything. The script processes the text and spits it out reformatted. It’s quite long, so here’s a condensed version:

## –––––––––––––––––––––––––[ myScript.sh ]–––––––––––––––––––––––––– ##

# (1) Set up default values

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) Parse user input
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) Now here, get text
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) Process the text
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) ##

Part 3 works for accepting text as entered after options and when piped, but hangs waiting for input if no text is entered and doesn’t see text passed as process substitution… eg:

Examples:

./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' )

> 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
> <B>Error:</B> No text entered...

./myScript.sh -L10 --width=20 <<<'This is a test'

> 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

How do I get the script to not hang on cat or read, waiting for input? (withoout using timeout or read -t as that just slows things down)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:43:31+00:00Added an answer on May 23, 2026 at 2:43 pm

    I think what you want here is to avoid reading from a terminal:

    if test -t 0; then
        echo "Not reading from terminal.  Pipe through cat if you really want to do this" >&2
        exit 1
    fi
    

    It is not possible to detect the general case where no source of input has been provided specifically to the program; all redirection is done by the shell, and your program cannot tell if its standard input was given specifically to it or inherited from the shell.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a Python script that does some task to generate, and then keep
I would like to write a python script that takes a bunch of swf
I'm trying to write a bash script that takes a few variables and then
Can someone point me to or write a script/program that takes X days and
I am trying to write a python script that takes record data like this
I wrote a script that uses xcodebuild to generate an AdHoc build of an
This is a crude korn shell script that someone else wrote. I don't know
I have a Perl script I wrote for my own personal use that fetches
I wrote a short bash script to complete a task that involves creating a
I have a Python script I recently wrote that I call using the command

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.