如何在txt文件中存储用户输入?

时间:2017-11-28 07:53:59

标签: shell

我必须将用户输入存储在两个文件中。如果用户输入芒果然后它应该自动存储在杂货店文件中或者如果用户输入衬衫那么它应该存储在衣服文件中并最终将两个文件合并在一个文件中。 我到目前为止所做的工作如下。

#!/bin/bash
clear
touch grocry.txt clothes.txt other.txt

echo -n "enter a item: "
read item
if 
  grep -Fxq "$item" item1.txt
then
  echo -n "$item" >> grocry.txt
elif -Fxq "$item" item2.txt
then
  echo -n "$item" >> clothes.txt
else
  echo -n "$item" >> other.txt
fi

1 个答案:

答案 0 :(得分:0)

我认为你会选择这样的事情:

#!/bin/bash
clear
touch grocery.txt clothes.txt other.txt

read -rp "Enter an item: " item

if grep -wq "$item" item1.txt; then
    echo "$item" >> grocery.txt
elif grep -wq "$item" item2.txt; then
    echo "$item" >> clothes.txt
else
    echo "$item" >> other.txt
fi

read有一个内置的提示功能,你错过了其中一个条件行的grep。

相关问题