How to Let a Bash Script User State the Number of Question Fields

Now that’s some title for a post. I Couldn’t think how to shorten it but it is shorter than the original title I wanted to use. Anyway, a couple of weeks ago I needed to figure out how to write a Bash script that let its users state how many fields or questions the script should request information for and what those field names should be. It took less than five minutes to solve the problem but I thought I’d post my solution here in hope that someone else finds it useful or knows a better solution.

Here’s what I came up with. It’s an input loop script that continues to request input for as many fields as the user specifies:

#! /bin/bash

# Part One: Ask the number of form fields or questions to be asked

while [[ ! $REPEAT =~ ^[0-9]+$ ]];
do
  echo -e $green"How many form fields need to be completed?"$remove
  echo -n -e ""
  read -e REPEAT
done

# Part Two: Ask for the field names or questions

clear
N=0
until [ $N == "$REPEAT" ];
do
  N=$((N+1))
  echo -e $green"Enter the Name of field $N:"$remove
  echo -n -e ""
  read -e FORM[$N]
done

# Part Three: Return the field names or answers

clear

echo "Here is the information you gave:"
echo ""
for (( X=1; X<="$REPEAT"; X++ ));
do
	echo "${FORM[$X]}"
done
echo ""

As it is, it does three things:

  1. Asks a user to specify the number of data fields that are to be used e.g how many questions should be asked by the script.
  2. Asks a user to specify the data for each data field e.g the questions to be asked.
  3. Prints each data field to the screen in order of entry.

It performs those actions with

  1. A question that requires a  numerical answer. The answer to this question sets a variable called REPEAT which is used to tell loops used in the script how many times to repeat.
  2. A primary loop that repeats as many times as there are fields to be created as specified by the REPEAT variable. This loop stores each answer into an array.
  3. A secondary loop that repeats until each field has been printed to the screen. Again, this loop uses the REPEAT variable as its upper count limit.

It is a simple script and performs a simple function – it lets a user state how many data fields need to be inputted then it displays those data fields to the screen immediately after the final field has been created.

With a little bit of editing the method can be adapted so that the script continues to ask for data until a defined phrase is entered at the prompt. For example, this next script will prompt for input until either the word enough or the word ENOUGH is entered into the prompt. In this case we have an input loop script that continues until the user tells it to quit:

#! /bin/bash

# Part One: Ask for the data until either "enough" or "ENOUGH" is entered into the prompt

clear
N=0
until [[ "${FORM[$N]}" =~ ^(enough|ENOUGH)$ ]];
do
  N=$((N+1))
  echo -e $green"Enter the Name of field $N:"$remove
  echo -n -e ""
  read -e FORM[$N]
done
unset FORM[$N] # Removes "enough" from the array
# Part Two: Return the data to screen

clear

echo "Here is the information you gave:"
echo ""
for (( X=1; X<="${#FORM[@]}"; X++ ));
do
	echo "${FORM[$X]}"
done
echo ""

exit 0

But what can be done with the inputted data?

I used it to create an autofill form script. The script was created to work with any web form. Because different web forms contain different quantities of fields, the script needed to be open about the number of fields it was used to auto complete so I wrote it to allow its user to tell it how many fields it needed to complete and to tell it the titles used for each of those fields. This script is available for viewing in the download here (look in the ScriptBox for auto-form-filler).

What else can it be used for?

It can be made into a function that takes arguments. This function can then be called whenever a series of data needs to be written to an array or file. It could be used to write a basic database. I suppose it has as many applications as we have imagination to apply it to.

Over to You

I am a novice Bash script writer. I dabble when I need to. I know there must be other ways to perform the same action. Do you know any? If so please let us know.

1
Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
newest oldest most voted
Notify of

haha, it’s hard to understand ur Chinese.
中文写得有点怪,没有看懂哦。

Free to your inbox

Join our mailing list to receive the latest news and updates from JournalXtra.

You have Successfully Subscribed!