因此,我尝试编写一个脚本,它可以向现有文件添加附加信息,同时提示用户文件中已存在信息,并且他们可以写入其他内容或退出。
#Enter the new info here
#Check to see if this info already exists
#If it does exit tell user to enter something else or give the option to exit back to main menu
#if doesn't, proceed input/add to the file
以下是我目前想到的
#!/bin/bash
file=database.dat
echo "Enter a username:"
read $username
grep -q $username $file && echo $?
if [ $? == 0 ] ; then
echo "User already exists"
答案1
如果您想了解更多信息,则必须在问题中详细说明。这应该可以帮助您入门。
#!/bin/bash
file=database.dat
echo "Enter a username:"
read username
grep -i "$username" "$file"
if [ $? == 0 ] ; then echo "User already exists"
exit 1
else
echo "$username" >> "$file"
fi
这是输出...
ubuntu@ubuntu-xenial:~/t2$ ./test.sh
Enter a username:
myname
ubuntu@ubuntu-xenial:~/t2$ ./test.sh
Enter a username:
myname
myname
User already exists
ubuntu@ubuntu-xenial:~/t2$