Operating System - HP-UX
1833870 Members
1533 Online
110063 Solutions
New Discussion

Re: shell script: Add details in a file

 
summersnowy
New Member

shell script: Add details in a file

I'm writing a shell script to add some details into a file. I'm not sure how to save the details into a file. I've written the script that can accept the details but how do I save them into my file?

---------------------
#!/bin/sh
echo "Item: \c"
read a
echo "Quantity: \c"
read b

`??? >>itemFile`
#How do I append the details into my file?

----------------------
My itemFile:

Item Quantity
Printer 10
Monitor 120
Keyboard 120

How can I add the item and quantity details into my itemFile?
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor

Re: shell script: Add details in a file

Hi:

...
read a
echo $a >> itemfile
echo "Quantity: \c"
read b
echo $b >> itemfile

Regards!

...JRF...
Ian Dennison_1
Honored Contributor

Re: shell script: Add details in a file

Use cat and awk

cat itemfile |awk -v printer=$a '$1 ~ /Printer/ {print $1" "printer}' >/tmp/itemnew
mv /tmp/itemnew itemfile

This only works if there is only 1 entry called "Printer" per file.

If you want to treat flat files as a potential 'database', then store the data in the best possible format, and then reformat it when you want to display it,.... so this example if best stored on 1 line per record (a record being a group of related variables with a unique key - basic DB management principles)

eg. [User] [Printer] [Monitor] [Keyboard]
The above line format, delimited by spaces, would look like this,...

Bob 10 120 120

You would retrieve field 2 to get the Printer Count, and use a similar awk statement to the one above, but keyed on User.

Share and Enjoy! Ian
Building a dumber user
john korterman
Honored Contributor

Re: shell script: Add details in a file

Hi,
if you only want to append, try this:
#!/bin/sh
ITEMFILE=/tmp/items
echo "Item: \c"
read a
echo "Quantity: \c"
read b
echo $a $b >>$ITEMFILE

regards,
John K.
it would be nice if you always got a second chance
John Meissner
Esteemed Contributor

Re: shell script: Add details in a file

#!/bin/sh
echo "Item: \c"
read a
echo "Quantity: \c"
read b
echo $a >> file1
echo $b >> file2
paste file1 file2 >> file3


This will put the entries next to each other to easier reading
All paths lead to destiny
summersnowy
New Member

Re: shell script: Add details in a file

What if I don't want to use append, what else I can do to add my details into a file?
john korterman
Honored Contributor

Re: shell script: Add details in a file

Hi,
I guess you would like to test whether an item is already present in the itemfile or not. A small test for that could be made like this:

#!/bin/sh
ITEMFILE=/tmp/items
[ -r $ITEMFILE ] || touch $ITEMFILE
echo "Item: \c"
read a
echo "Quantity: \c"
read b

# Is item already present?
grep "^$a" $ITEMFILE
if [ "$?" = "0" ]
then
# yes, "overwrite"
OTHER_ITEMS=`grep -v "^$a" $ITEMFILE`
echo "$OTHER_ITEMS\n$a $b" >$ITEMFILE
else
# no, append
echo $a $b >>$ITEMFILE
fi

However, the original order of items is not kept.

regards,
John K
it would be nice if you always got a second chance
Reinhard Burger
Frequent Advisor

Re: shell script: Add details in a file

If you want to update informations inside the file then would take the solution provided by Ian. This will update the information stored in your file
keep it simple
John Meissner
Esteemed Contributor

Re: shell script: Add details in a file

of course if you are adding ammounts to existing ammounts you could do it like this

ptr=$(cat itemfile | grep Printer | awk '{print $2}')
mtr=$(cat itemfile | grep Monitor | awk '{print $2}')
kbd=$(cat itemfile | grep Keyboard | awk '{print $2}')

echo "Enter item:\c"
read a
echo "Enter quantity:\c"
read b
if [ $a = "Printer" ]
then
((c=$b+$ptr))
echo sed 's/$ptr/$c/g' itemfile > itemfile2" > itemswap.sh
chmod +x itemswap.sh
./itemswap.sh
mv itemfile2 itemfile
elif [ $a = "Montitor" ]
then
((c=$b+$mtr))
echo "sed 's/$mtr/$c/g' itemfile > itemfile2" > itemswap.sh
chmod +x itemswap.sh
./itemswap.sh
mv itemfile2 itemfile
# do the same for any instances of the item you want to update
fi
All paths lead to destiny