0

I am new to Bash and was wondering if someone could give me some insight on how I can make this program work more accurately.

Goal: To write a bash shell script that presents a menu to the user with the options of add a contact, remove a contact, find a contact, list all contacts, and exit the program.

This is my code so far:

#!/bin/bash
touch contacts.dat
echo "Select one of the following options:"
echo "-------------------------------------"
echo "1. Add a contact"
echo "2. Remove a contact"
echo "3. Find a contact"
echo "4. List all contacts"
echo "5. Exit the program"
read -p "Enter a choice (1-5): " choice
echo
#
case "$choice" in
    1)
    read -p "Enter a name: " name
    read -p "Enter an email address: " email
    echo "$name , $email" >> contacts.dat
    ;;
2)
    read -p "Enter a name: " name
    if (grep -q name contacts.dat) then
        grep -v name contacts.dat > deletedNames.dat
        echo "$name was removed from the file."
    else
        echo "$name does not exist in the file."
    fi
    ;;
3)
    read -p "Enter a name: " name
    if (grep -q name contacts.dat) then
        echo "$name , $email"
    else
        echo "The $name was not found."
    fi
    ;;
4)
    sort -k 1 contacts.dat 
    ;; 
5)
    echo "Thank you for using this program!"
    # break?
    exit 1
    ;;
*)
    echo "Please enter a valid choice (1-5)."
    ;;
esac

The program seems to work with options 1, 4, and 5. However, not with 2 and 3.

How can I get 2 and 3 to remove the contact and find the contact (respectfully)? Thank you in advance for any help you may be able to offer.

M.Z
  • 13
  • 1
  • 1
  • 4
  • 2
    `[[` is not part of `if` syntax, and it should only be used if you're trying to invoke the *extended test command*. And, like any other shell keyword, `[[foo` has no more meaning that `lsfoo` does -- you need to have spaces: `ls foo`, `[[ foo`. – Charles Duffy Sep 23 '17 at 21:47
  • That said -- as a whole, this question isn't well-asked right now. Please see the documentation on building a [mcve]: Code should be the **smallest possible example** that's tested to generate the error you're inquiring about. – Charles Duffy Sep 23 '17 at 21:49
  • 2
    Also, in the future, consider running your code through http://shellcheck.net/ and fixing what it finds before asking questions here. – Charles Duffy Sep 23 '17 at 21:50
  • BTW, while `if grep -q name contacts.dat; then` will branch on whether the name exists anywhere in your file, it'll match on substrings -- meaning that you can tell it to search for "Jill" and it'll find a match where the actual line is "Jillian". You probably want to be a bit more selective than that. – Charles Duffy Sep 23 '17 at 22:05
  • As another aside, `if (grep -q name contacts.dat) then` is less efficient than `if grep -q name contacts.dat; then` -- the parens tell the shell to spawn off a subprocess and run the code contained within it. If you want command grouping without that side effect, then the syntax is `if { grep -q name contacts.dat; }; then` -- though there's no point to it in this context. – Charles Duffy Sep 24 '17 at 00:00
  • This is now a *completely different question* than what you first asked -- and still far afield of complying with the [mcve] definition, or of being scoped to a single problem. ("How can I add a line to a file?" is one distinct question, "how can I remove a line from a file?" another -- and *both of those* already have answers in the knowledgebase; asking a question scoped to contain both just puts it into the territory of "too broad"). – Charles Duffy Sep 24 '17 at 00:04
  • ...that said, I've added some additional dupes topical for the question as-amended. – Charles Duffy Sep 24 '17 at 00:45

0 Answers0