将数据存储在数组中(bash脚本)

时间:2022-01-06 10:24:39

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.

我们的想法是不断询问名称和名称2,并根据答案的答案将其全部打印出来。

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

但是如何将它们存储到不同的变量中?**

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

可能有20个名字,有些是客户和一些雇主。

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.

为了清除任何混淆,如果有任何混淆,AreYouDone只是一个功能,当客户已经完成并实施时,它将退出程序。

Thanks.

2 个解决方案

#1


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之后发生的事情,那么最好这样写:

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.

...并根据用户是否要退出而不是让它自行退出,让它返回true或false值。

#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循环:

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.

由于names是一个索引数组,您可以使用$ {names [$ i]}在某个索引处访问其值,其中$ i是索引。 $ {#names [@]}是数组的大小。

#1


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之后发生的事情,那么最好这样写:

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.

...并根据用户是否要退出而不是让它自行退出,让它返回true或false值。

#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循环:

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.

由于names是一个索引数组,您可以使用$ {names [$ i]}在某个索引处访问其值,其中$ i是索引。 $ {#names [@]}是数组的大小。