我正在bash中运行此脚本,以将用户列表添加到数组,然后将其写入文件。这是脚本:
echo "Insert the first user of the list"
read -r user_name
user_list=()
echo "User $user_name inserted!"
user_list+=($user_name)
echo "Do you want to insert another user?(yes or no)"
read -r answ
while [ $answ == "yes" ]; do
echo "Enter a new user to insert"
read -r new_user
user_list+=($new_user)
echo "Users list contains:"
echo "$user_list"
echo "Do you want to add another user?(yes or no)"
read -r answ
done
echo "$user_list" > user_list.txt;
除了数组只包含第一个元素外,其他所有东西都工作正常。我不明白为什么不将$ new_user添加到数组中。
答案 0 :(得分:1)
最后一行可能是
printf "%s\n" "${user_list[@]}" >user_list.txt