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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:35:18+00:00 2026-05-31T08:35:18+00:00

I had a bash file which prompted the user for some parameters and used

  • 0

I had a bash file which prompted the user for some parameters and used defaults if nothing was given. The script then went on to perform some other commands with the parameters.
This worked great – no problems until most recent addition.

In an attempt to read the NAMES parameter from a txt file, I’ve added a while-loop to take in the names in the file, but I would still like the remaining parameters prompted for.

But once I added the while loop, the output shows the printed prompt in get_ans() and never pauses for a read, thus all the defaults are selected.

I would like to read the first parameter from a file, then all subsequent files from prompting the user.

What did I break by adding the while-loop?

cat list.txt | 
while read line
do 
  get_ans "Name" "$line"
  read NAME < $tmp_file

  get_ans "Name" "$line"
  read NAME < $tmp_file
done

function get_ans
{
  if [ -f $tmp_file ]; then
    rm $tmp_file

  PROMPT=$1
  DEFAULT=$2

  echo -n "$PROMPT [$DEFAULT]: "
  read ans
  if [ -z "$ans" ]; then
    ans="$DEFAULT"
  fi
  echo "$ans" > $tmp_file
}

(NOTE: Code is not copy&paste so please excuse typos. Actual code has function defined before the main())

  • 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-31T08:35:19+00:00Added an answer on May 31, 2026 at 8:35 am

    You pipe data into your the while loops STDIN. So the read in get_ans is also taking data from that STDIN stream.

    You can pipe data into while on a different file descriptor to avoid the issue and stop bothering with temp files:

    while read -u 9 line; do
       NAME=$(get_ans Name "$line")
    done 9< list.txt
    
    get_ans() {
        local PROMPT=$1 DEFAULT=$2 ans
        read -p "$PROMPT [$DEFAULT]: " ans
        echo "${ans:-$DEFAULT}"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had used several ways to do some simple integer arithmetic in BASH (3.2).
I wrote a bash script in that i had a Value=09 and when I
I have a bash script (.sh file) and a Python script (.py) file in
I'm updating a bash script which serves as a program testing tool. Previously, I
i had made a small bash script in order to get the frequency of
Say I have a file at the URL http://mywebsite.example/myscript.txt that contains a script: #!/bin/bash
In a bash script, files with spaces show up as File\ with\ spaces.txt and
I've written a simple bash script that prompts for a file or directory path
Had an interesting discussion with some colleagues about the best scheduling strategies for realtime
I had used Server Explorer and related tools for graphical database development with Microsoft

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.