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.

Jahid
  • 21,542
  • 10
  • 90
  • 108
user3610137
  • 283
  • 1
  • 5
  • 14

2 Answers2

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.

Jahid
  • 21,542
  • 10
  • 90
  • 108
  • 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 May 13 '15 at 03:11
  • @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 – Jahid May 13 '15 at 16:27
  • 2
    You forgot to escape the `[` there. Should be `read -rp "Enter name: " 'names[i]'`. – geirha May 13 '15 at 20:43
  • Indeed. Test with `nullglob` enabled and the outcome from not quoting that is particularly obvious. :) – Charles Duffy May 15 '15 at 20:32
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.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 1
    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). – mklement0 May 13 '15 at 17:42
  • 1
    Heh! I assumed that the user had their own helper, but that is indeed the more likely explanation. – Charles Duffy May 13 '15 at 19:51
  • 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). – user3610137 May 15 '15 at 20:23
  • I have also used your implementation of AreYouDone() but it's saying command not found. `AreYouDone: command not found` – user3610137 May 15 '15 at 20:25
  • @user3610137, re: AreYouDone, where exactly you put it and how you call it matters. In particular, it needs to be at the top. – Charles Duffy May 15 '15 at 20:29
  • @user3610137, re: `declare -A`, that was intentional, and using `declare -a` instead won't work the same way. Associative arrays are a bash 4.x feature; it sounds like you're using an ancient 3.x shell, and need to use the first (two-array) version, which doesn't use that feature. – Charles Duffy May 15 '15 at 20:30