Bash Scripting: Escrevendo / adicionando texto a um arquivo existente

0

Portanto, estou tentando escrever um script que possa adicionar informações adicionais a um arquivo existente e também solicitar ao usuário que as informações já existam no arquivo e que elas possam gravar outra coisa ou sair.

#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

Aqui está o que eu tenho feito até agora

#!/bin/bash

file=database.dat

echo "Enter a username:"
read $username
grep -q $username $file && echo $?
if [ $? == 0 ] ; then
        echo "User already exists"
    
por user8379123 21.11.2016 / 18:23

1 resposta

0

Você terá que elaborar mais em sua pergunta se estiver procurando mais. Isso deve começar você.

#!/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

Aqui está a saída ...

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$
    
por bc2946088 21.11.2016 / 18:44