The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
altering numbers in files bigboizvince Shell Programming and Scripting 4 03-26-2008 07:34 AM
Unix Arithmatic operation issue , datatype issue thambi Shell Programming and Scripting 23 02-19-2008 04:19 AM
how to verify that copied data to remote system is identical with local data. ynilesh Shell Programming and Scripting 3 01-31-2008 05:55 AM
Howto capture data from rs232port andpull data into oracle database-9i automatically boss UNIX for Dummies Questions & Answers 1 09-22-2007 11:35 PM
Data Extraction issue. irehman Shell Programming and Scripting 5 04-06-2006 05:28 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-10-2006
stlitguru's Avatar
Registered User
 

Join Date: Apr 2006
Posts: 3
Question Issue altering end data

I have an inventory program that I would like to have the ability to go and change or alter the field data based on the item number as a key. I have the menu option set but at the end of the script process it just appends the changed data to the database rather than what I would like; which is to replace the current item number and fields that were changed in the menu.

Any advice would be appreciated.

The inventory change script is below.

#!/bin/bash
#===================================================
# Script Name: invadd
# By: jhaynes
# Date: 4-4-2006
# Purpose: Script loop to add inventory to the HC
# computing inventory list, while preventing
# duplicate item#.
# Command Line:invadd
#===================================================
trap "rm ~/tmp/* 2> /dev/null ; exit" 0 1 2 3
invfile=~/final/hc_inventory
looptest=y
while [ $looptest = "y" ]
do
clear
tput cup 25 4; ./appendinv;
tput cup 1 4; echo "HC Inventory Change"
tput cup 2 4; echo "================================"
tput cup 4 4; echo "Item Number: "
tput cup 5 4; echo "Description: "
tput cup 6 4; echo "Dept : "
tput cup 7 4; echo "Cost : "
tput cup 8 4; echo "Qty : "
tput cup 10 4; echo "Add Another? (y)es or (q)uit: "
tput cup 11 4; echo "Please enter an item number or q to quit "
tput cup 4 16; read itemnum
if [ "$itemnum" = 'q' ]
then clear ; exit
fi
#Check to make sure the Item number field is not blank
if [ "$itemnum" = "" ]
then
tput cup 11 2; echo "***Item number can not be blank*** "
tput cup 13 2; echo "******Hit ENTER to continue******"
tput cup 4 16 ; echo " "
tput cup 4 16 ; read itemnum
continue
fi
tput cup 5 16; read descr
while [ "$descr" = '-' ]
do
tput cup 4 16 ; echo " "
tput cup 4 16 ; read itemnum
if [ "$itemnum" = 'q' ]
then
clear ; exit
fi
tput cup 5 16 ; read descr
done
tput cup 6 16; read dept
while [ "$dept" = '-' ]
do
tput cup 5 16 ; echo " "
tput cup 5 16 ; read descr
if [ "$descr" = 'q' ]
then
clear ; exit
fi
tput cup 6 16 ; read dept
done
tput cup 7 16 ; read cost
while [ "$cost" = '-' ]
do
tput cup 6 16 ; echo " "
tput cup 6 16 ; read dept
if [ "$dept" = 'q' ]
then
clear ; exit
fi
tput cup 7 16 ; read cost
done
tput cup 8 16 ; read qty
while [ "$qty" = '-' ]
do
tput cup 7 16 ; echo " "
tput cup 7 16 ; read cost
if [ "$cost" = 'q' ]
then
clear ; exit
fi
tput cup 8 16 ; read qty
done
#Check to make sure fields are not blank before you write the data4
if [ "$itemnum:$descr:$dept:$cost:$qty" > " " ]
then
echo "$itemnum:$descr:$dept:$cost:$qty" >> $invfile
fi
tput cup 10 33; read looptest
if [ "$looptest" = 'q' ]
then
clear ; exit
fi
done
Reply With Quote
Forum Sponsor
  #2  
Old 05-10-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 988
Editing a line in the middle of a file is a LOT harder than adding a line to the end of a file. Adding to the end just means opening in append mode, but selectively editing a line in the middle of it means reading in the file completely, modifying the contents, then writing back the edit and everything after it.

The reason it's so hard is, what if the length of the line changes? You can't just replace the line or stuff like this happens:
Code:
line1
line2
line3
Replace line2 with line99 and it becomes:
Code:
line1
line99line3
Because line99 is one character longer, 9 overwrites the newline character. To do this properly you have to insert more characters into the middle. Or, if the new line is shorter, delete characters from the middle. This makes it a real pain.

If you use fixed record sizes, you'll be able to just seek to and overwrite arbitrary records. See 'man dd'. bash may not be the best language for this sort of work, however.
Reply With Quote
  #3  
Old 05-10-2006
stlitguru's Avatar
Registered User
 

Join Date: Apr 2006
Posts: 3
Thanks Corona688. I understand what you are saying. I am useing fixed-length records. I will look to the man dd pages. To see if there is a way to update the veriables based on the item number being the key identifier to the line.
Reply With Quote
  #4  
Old 05-10-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 988
You are? Neat. I didn't catch that since I'm not familiar with all the commands you're using in your script.

dd doesn't give much consideration to what data it's copying or writing. You can't tell it what to overwrite, only where. Overwriting a specific record would be something like
Code:
echo "${DATA}" | dd bs=recordsize seek=recordnum of=recordfile
and reading a specific record would be like
Code:
DATA=`dd bs=recordsize seek=recordnum if=recordfile`
So you'll have to use other means to find out the record number.

Be aware that dd is not a bash builtin, running dd means creating a process.

Last edited by Corona688; 05-10-2006 at 08:02 AM.
Reply With Quote
  #5  
Old 05-10-2006
stlitguru's Avatar
Registered User
 

Join Date: Apr 2006
Posts: 3
I like to think of myself as neat also
Here is an example of the inventory list

HC Inventory List

Dept Item# Description Qty Cost
==== ===== ============================== ===== ======
0100 10001 Intel Celeron - 1.8GHz 7 $65.00
0100 10002 Intel Pentium 4 - 2.4GHz 10 $125.00
0200 10003 AMD Athlon XP 2800 12 $108.00
0100 10004 Intel Mobile Pentium 4 1.7GHz 6 $170.00
0100 10005 Intel Xeon 1.8GHz 8 $183.00
0100 10006 Intel Pentium 4 - 3.2GHz 4 $285.00
0300 10007 Kingston 256MB PC133 20 $50.39
0300 10008 Kingston 512MB RDRAM Kit 6 $240.00

So as an example, lets say I wanted to change the qty of item # 10007
other than dd, are there any other tools that might better achive what I am looking to do.
Reply With Quote
  #6  
Old 05-11-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 988
I forsee problems no matter what method you use. What you seem to be trying to build is a rudimentary relational database, to let you do queries like
Code:
UPDATE INVENTORY SET B="value1", C="value2", D="value3" WHERE A="12345";
but BASH is not a good language to build a relational database in. Bash The Database is a BASH extension that seems closer to what you want but is designed for connecting to remote databases, not local file-based ones. The BASH shell is just not designed for record-based file access, and neither are most scripting languages. Can you use other languages or do you absolutely need to use BASH for some reason?

Another thing I notice is that, unless you keep all the records in your database sorted by item number, there is no way to find a specific number except checking every single row. This will be very slow when your database grows larger. Keeping them ordered will let you find the record you want with a binary search.

Last edited by Corona688; 05-11-2006 at 07:42 AM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:53 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0