Updating a database


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Updating a database
# 1  
Old 12-06-2009
Updating a database

i've got a database setup that references user inputs to see if they are already inputted. What I am wanting to do is allow the user to update the database by typing
"update: name" where name is someone already in the database.

Code:
echo "Enter your name."
read NAME

location=`find . -name tes.txt`

if
        grep -qi "$NAME" $location
        then
        echo "User alreay inputted"
        cut -d: -f 1-3,5 $location | sed -e 's/:/,/gi' > temp.txt
else
 echo "User not found - name will be added to database."
        echo "$NAME" >> tes.txt
fi

access=`cut -d: -f5 $location`
case $access in
"A") echo "Administrator Access" >> temp.txt;;
"B") echo "Business Access" >> temp.txt;;
"S") echo "Student Access" >> temp.txt;;
"G") echo "General Access" >> temp.txt;;
*) echo "General Access" >> tempt.txt;;
esac

echo "To update files type 'update: name'"
read UPDATE

if
cut -d: -f 1 $UPDATE > updatetemp.txt
grep -qi "$NAME" updatetemp.txt
.
.
.

I want to cut out the name part and then cross-reference with the current database to see if the name is already inputted. I was wanting to have the cut outside of the if statement, but it wasn't working.

Am I close to getting it right here?
# 2  
Old 12-07-2009
you're saying "database" when you mean "file". these are quite different.

To make things easier on yourself, I would not put all those commands inside an "if" block. For example, in the 2nd if block, you really don't care if "cut" fails. You care if "grep" fails.

Instead, do something like this:
Code:
cut -d: -f 1 $UPDATE > updatetemp.txt
grep -qi "$NAME" updatetemp.txt

if [ $? -eq 0 ]; then
  print success
else
  print fail
fi

Other things I noticed:
Why are you doing this: ( good place for a comment. )
Code:
cut -d: -f 1-3,5 $location | sed -e 's/:/,/gi' > temp.txt

Also: location could just be hard-coded. What's with putting a "find" command there? We don't know where our "database" is?

Also: later, you're blindly echoing the name onto the tes.txt file. Shouldn't that be $location?

Also: don't need double quotes within the case options. nor in those echo commands.

This looks like a mess though. It's a lot of coding to get around the occasional simple editting of a file.
# 3  
Old 12-07-2009
Quote:
Originally Posted by quirkasaurus
you're saying "database" when you mean "file". these are quite different.
you are correct - it is a file - my mistake.

Quote:
Originally Posted by quirkasaurus
To make things easier on yourself, I would not put all those commands inside an "if" block. For example, in the 2nd if block, you really don't care if "cut" fails. You care if "grep" fails.

Instead, do something like this:
Code:
cut -d: -f 1 $UPDATE > updatetemp.txt
grep -qi "$NAME" updatetemp.txt

if [ $? -eq 0 ]; then
  print success
else
  print fail
fi

That was what I was wanting to do, but as soon as I move the cut command outside of the if statement, it doesn't work and i get this error:

Code:
cut: name: No such file or directory

but when I put the exact same line within the if statement, it works fine.


Quote:
Originally Posted by quirkasaurus
Other things I noticed:
Why are you doing this: ( good place for a comment. )
Code:
cut -d: -f 1-3,5 $location | sed -e 's/:/,/gi' > temp.txt

Basically I'm cutting out columns 1,2,3,and from $location and swapping the : with , and sending it to a new file called temp.txt (no real reason other than to practice and understand how to use the commands).

Quote:
Originally Posted by quirkasaurus
Also: location could just be hard-coded. What's with putting a "find" command there? We don't know where our "database" is?
Correct, again just making use I''m understanding how to use the commands (I'm new to unix).

Quote:
Originally Posted by quirkasaurus
Also: later, you're blindly echoing the name onto the tes.txt file. Shouldn't that be $location?
Yes you are right - I didn't catch that, thanks.

Quote:
Originally Posted by quirkasaurus
Also: don't need double quotes within the case options. nor in those echo commands.
Didn't know that one either.. thanks again!

Quote:
Originally Posted by quirkasaurus
This looks like a mess though. It's a lot of coding to get around the occasional simple editting of a file.
Thanks for the feedback as it's good to know that what I have thus far is messy and there are ways to clean it up. Once I get it going, I'm going to tackle cleaning it up.

Thanks again for the help.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Ubuntu

16.04 not updating

Just installed the latest version on my hp stream, was tired of windows. Of the few times I've used Linux, this is the first time it won't update. Im connected to the net the updater recognizes that the OS needs 21 updates. But it won't download/install the updates. It also won't load the... (1 Reply)
Discussion started by: DabblingMadman
1 Replies

2. Shell Programming and Scripting

CRON Job to copy database and replace existing database

I have a reseller account with hostgator, which means i have WHM and Cpanel. I have set up a staging environment for one of my wordpress installations (client website), which is essentially sitting at staging.domain.com (live site is at domain.com). The staging website is a complete copy of the... (1 Reply)
Discussion started by: nzrobert
1 Replies

3. Web Development

Updating Records in Mysql Database

so when i issue a command like the below: # mysql --pager=/usr/bin/less -u cactiman -p -e 'select * from data_input' cacti Enter password: ... (2 Replies)
Discussion started by: SkySmart
2 Replies

4. AIX

Updating AIX OS

Hello I have a server whose os level reads 5300-01-00-0000. It has to be upgraded to the latest level which is 5300-12-04-1119. What would I need to do go get the server to this point? Would I need to go through each patch level until I get it to current? If so, would anyone happen to know... (1 Reply)
Discussion started by: stayfuzzy
1 Replies

5. Solaris

Can't create database after Oracle Database installation

I installed Oracle 10 software on Solaris 11 Express, everything was fine execpt I can't create database using dbca.rsp file. I populated file with following options. OPERATION_TYPE = "createDatabase" GDBNAME = "solaris_user.domain.com" SID = "solaris_user" TEMPLATENAME = "General... (0 Replies)
Discussion started by: solaris_user
0 Replies

6. Solaris

redirect solaris database from linux database..

hi.. i have a need .. my php runs on my linux redhat box with mysql.. i want my php code to refer another mysql database which is in solaris 10 x86... can u tell me the procedure .. how it can be done through php .. sorry am new to php... is it possible to redirect from linux mysql to... (7 Replies)
Discussion started by: senkerth
7 Replies

7. Homework & Coursework Questions

UNIX- Database creating/viewing/updating assignment

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have posted the assignment below, I am new to unix programming and am having a very hard time knowing where... (5 Replies)
Discussion started by: student1989
5 Replies

8. AIX

.sh_profile not updating

Dear Gurus i have read a lot written on this topic but i just wanna ask that the /etc/profile file is present but the .sh_profile in the users $HOME directory is not being updated, any guesses why??? Also that i need to get the information about the date, time, IP and the user name from which... (7 Replies)
Discussion started by: masquerer
7 Replies

9. Shell Programming and Scripting

awk updating one file with another, comparing, updating

Hello, I read and search through this wonderful forum and tried different approaches but it seems I lack some knowledge and neurones ^^ Here is what I'm trying to achieve : file1: test filea 3495; test fileb 4578; test filec 7689; test filey 9978; test filez 12300; file2: test filea... (11 Replies)
Discussion started by: mecano
11 Replies
Login or Register to Ask a Question