2

I want to ask a user for an input such as:

Echo "Please enter name: "
read name 
read -r -p "Is this a costumer? (Y/N)" response;
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
then 
    echo "Please enter name: "
    read name
    AreYouDone
else
    "Please enter  name "
    read name2
    AreYouDone
fi

echo $name is a costumer  
echo $name2 is an employer

The idea is to keep asking for name and name2 and print them all at the end depending on the Y/N answer.

But how do I store them into different variables?**

There may be 20 names and some are costumer and some employer.

P.S.:

To clear any confusion, if there is any, AreYouDone is just a function that'll just exit out of the program when the costumer is done and implemented already.

Thanks.

2
  • the answer is here: stackoverflow.com/questions/1951506/… Commented May 12, 2015 at 23:09
  • 2
    Are you dealing with people who make or supply costumes (aka costumers) or with people who buy things (aka customers)? Commented May 13, 2015 at 17:32

2 Answers 2

2

It sounds like you want two arrays -- an array of customers, and an array of employers.

declare -a customers=( ) employers=( )
while ! AreYouDone; do
  echo "Please enter name: "
  read name 
  read -r -p "Is this a costumer? (Y/N)" response;
  if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]; then 
      customers+=( "$name" )
  else
      employers+=( "$name" )
  fi
done

Then, to print all names by type:

printf '%s is a customer\n' "${customers[@]}"
printf '%s is an employer\n' "${employers[@]}"

A fancier approach would be to use an associative array to store type information for each name.

declare -A names=( )
while ! AreYouDone; do
  read -r -p "Please enter name: " name
  read -r -p "Is this a customer? " type
  if [[ $response = [Yy][Ee][Ss] ]]; then
    names[$name]=customer
  else
    names[$name]=employer
  fi
done

for name in "${!names[@]}"; do
  echo "$name is a ${names[$name]}"
done

Aside: If you want more control over what happens after AreYouDone, it would be better to write it like so:

AreYouDone() {
  read -r -p 'Are you done?'
  case $REPLY in
    [Yy]*) return 0 ;;
    *)     return 1 ;;
  esac
}

...and let it return a true or false value depending on whether the user wants to exit, rather than having it exit itself.

Sign up to request clarification or add additional context in comments.

6 Comments

Good stuff; I suggest changing Echo to echo, because Echo would at least by default only work on case-insensitive filesystems (by invoking the utility form of echo instead of the shell builtin).
Heh! I assumed that the user had their own helper, but that is indeed the more likely explanation.
Im getting line 14: declare: -A: invalid option declare: usage: declare [-afFirtx] [-p] [name[=value] ...] Is that an error made on my end? (solved by making it -a).
I have also used your implementation of AreYouDone() but it's saying command not found. AreYouDone: command not found
@user3610137, re: AreYouDone, where exactly you put it and how you call it matters. In particular, it needs to be at the top.
|
2

Declare array/s.

Example:

declare -a names
for ((i=0;i<20;i++));do
  read -rp "Enter name: " 'names[i]'
  echo "${names[i]}"
done

Additionally (from comment): You can construct a full sentence with another for loop with the inputs you got:

for ((i=0;i<${#names[@]};i++));do
  fullsentence+="Name is ${names[$i]} "
done
echo "$fullsentence"

As names is an indexed array, you can access its value at a certain index with ${names[$i]}, where $i is the index. ${#names[@]} is the size of the array.

4 Comments

Thanks! But how would I do this if I wanted to output the data outside the loop? is that doable? Reason I'm asking is there are other info asked before this. Like I'm asking for age group ONCE in the beginning then at the end I'm doing an echo of all the info given in full sentences. Like: Age Group: 16-24 Name John is an employer Name Joe is a costumer Name Alex is a costumer
@user3610137, yes certainly that's doable, There are several methods you can apply, but if you intent to use multiple indexed arrays (age,name and such), an arithmetic for loop would be better choice. I have added an example, I hope you can use it to meet your specific needs
You forgot to escape the [ there. Should be read -rp "Enter name: " 'names[i]'.
Indeed. Test with nullglob enabled and the outcome from not quoting that is particularly obvious. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.