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 7961605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:05:23+00:00 2026-06-04T05:05:23+00:00

I have an old shell script which needs to be moved to bash. This

  • 0

I have an old shell script which needs to be moved to bash. This script prints progress of some activity and waits for user’s commands. If no action is taken by user for 15 seconds screen is redrawn with new progress and timer starts again. Here’s my problem:

I am trying to use read -t 15 myVar – this way after 15 seconds of waiting loop will be restarted. There is however a scenario which brings me a problem:

  • screen redrawn and script waits for input (prints ‘Enter command:‘)
  • user enters foo but doesn’t press enter
  • after 15 seconds screen is again redrawn and script waits for input – note, that foo is not displayed anywhere on the screen (prints ‘Enter command:‘)
  • user enters bar and presses enter

At this moment variable $myVar holds ‘foobar‘.

What do I need? I am looking for a way to find the first string typed by user, so I could redisplay it after refreshing status. This way user will see:
Enter command: foo

On Solaris I could use stty -pendin to save input into some sort of a buffer, and after refresh run stty pendin to get this input from buffer and print it on a screen.

Is there a Linux equivalent to stty pendin feature? Or maybe you know some bash solution to my problem?

  • 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-06-04T05:05:24+00:00Added an answer on June 4, 2026 at 5:05 am

    OK, I think I have the solution. I took nhed’s proposition and worked a bit on it 🙂

    The main code prints some status and waits for input:

    while :
    do
        # print the progress on the screen
        echo -n "Enter command: "
        tput sc # save the cursor position
        echo -n "$tmpBuffer" # this is buffer which holds typed text (no 'ENTER' key yet)
        waitForUserInput
        read arguments <<< $(echo $mainBuffer) # this buffer is set when user presses 'ENTER'
        mainBuffer="" # don't forget to clear it after reading
        # now do the action requested in $arguments
    done
    

    Function waitForUserInput waits 10 seconds for a keypress. If nothing typed – exits, but already entered keys are saved in a buffer. If key is pressed, it is parsed (added to buffer, or removed from buffer in case of backspace). On Enter buffer is saved to another buffer, from which it is read for further processing:

    function waitForUserInput {
        saveIFS=$IFS # save current IFS
        IFS="" # change IFS to empty string, so 'ENTER' key can be read
    
        while :
        do
            read -t10 -n1 char
            if (( $? == 0 ))
            then
                # user pressed something, so parse it
                case $char in
                    $'\b')
                        # remove last char from string with sed or perl
                        # move cursor to saved position with 'tput rc'
                        echo -n "$tmpBuffer"
                        ;;
                    "")
                        # empty string is 'ENTER'
                        # so copy tmpBuffer to mainBuffer
                        # clear tmpBuffer and return to main loop
                        IFS=$saveIFS
                        return 0
                        ;;
                     *)
                        # any other char - add it to buffer
                        tmpBuffer=$tmpBuffer$char
                        ;;
                esac
            else
                # timeout occured, so return to main function
                IFS=$saveIFS
                return 1
            fi
        done
    }
    

    Thanks to all of you for your help!

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

Sidebar

Related Questions

Looking for some help with my bash script. I am trying to write this
I have an old project at our company that uses shell scripting a lot.
I have old code that uses size_t which IIRC comes from cstring.h. On OS
Most of the software company have old software which continue maintain, and they also
I have an old J2EE application (J2EE 1.3), which packages into an EAR, and
I have some old branches in my git repository that are no longer under
I have found a useful shell script that shows all files in a directory
I have a shell script that periodically checks the ADSL external IP address and
I have a custom shell program in which I have included signal.h , unistd.h
The scenario I have access to this old linux server, that no longer receives

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.