Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm new to bash and have written this script to collect data then save it to an automated file. It runs fine til here but as I try to rerun the script to add another details it gives me errors.

#!/bin/bash

#This program helps the user to collect contact details.
clear

options="Add_User End_session"

echo "1. Add another user"
echo "2. End session"
echo -n "Enter Selection:"
     read selection
     echo ""

select opt in $options; do

if ["$opt" = "End Session" ]; then
echo done
exit

elif ["opt" = "Add another user"]; then

echo "Dear, user . This script will help you to collect data from people."

echo "Type the name:"
read name
echo "Type the age:"
read age
echo "Type the address:"
read address
echo "Type the gender:"
read gender
echo "Type the phone number:"
read phone number
echo "Type the email:"
read email

echo "Full Details"

echo "$name"
echo "$age"
echo "$address"
echo "$gender"
echo "$phone"
echo "$email"

echo "Name: $name" >> Datacollection.txt
echo "Age: $age" >> Datacollection.txt
echo "Address: $address" >> Datacollection.txt
echo "Gende: $gender" >> Datacollection.txt
echo "Phone Number: $phonenumber" >> Datacollection.txt
echo "E-mail Address: $email" >> Datacollection.txt

else
clear
echo bad option
fi
done
share|improve this question
1  
Note that the [ in an if statement is actually a command (type help '[' in a terminal), not just syntax. Like any other shell command, there needs to be a space between the command name and the first argument: if [ "$opt" ... –  glenn jackman Dec 3 '12 at 2:57

1 Answer 1

You didn't specify the nature of the errors. You should always specify the exact error message(s) when asking for help. Otherwise, it's like you're expecing others to do all your debugging for you. Take a second or two to provide the most basic and obvious of information, and you'll probably get a lot more help.

Nevertheless, here are a few notes:

  1. When reading the phone number, you've got a space, meaning that you're reading into two separate variables. When you try to put it into the file, you're calling the variable by a different name.

    echo "Type the phone number:"
    read phone number
    #...
    echo "Phone Number: $phonenumber" >> Datacollection.txt
    
  2. You need spaces between square brackets ([]) and their contents. Thus, this line is wrong:

    elif ["opt" = "Add another user"]; then
    
  3. In addition to the preceeding, be careful what you're comparing. You have no dollar sign in front of "opt", so you're comparing the literal string, not its value.
  4. Your option selection routine is totally broken. You'll need to throw it away and rewrite it, using a different approach. Hint: Whatever the user types will be stored in the variable you pass to read. You'll be testing that value, so consider carefully what the user needs to type and how you can communicate that. Hint 2: Type help select to see the documentation for a good way to approach this.

  5. Finally, a really valuable tool for debugging your script is set -x. Put it at the top of your script. Then you'll be able to see everything in your script as the shell interprets it. If something looks wrong there, you've found a bug.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.