- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: shell script: Add details in a file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2002 05:17 AM
10-31-2002 05:17 AM
shell script: Add details in a 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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2002 05:29 AM
10-31-2002 05:29 AM
Re: shell script: Add details in a file
...
read a
echo $a >> itemfile
echo "Quantity: \c"
read b
echo $b >> itemfile
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2002 05:30 AM
10-31-2002 05:30 AM
Re: shell script: Add details in a file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2002 05:35 AM
10-31-2002 05:35 AM
Re: shell script: Add details in a file
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2002 06:04 AM
10-31-2002 06:04 AM
Re: shell script: Add details in a file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2002 05:53 PM
10-31-2002 05:53 PM
Re: shell script: Add details in a file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2002 12:16 AM
11-01-2002 12:16 AM
Re: shell script: Add details in a file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2002 01:56 AM
11-01-2002 01:56 AM
Re: shell script: Add details in a file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2002 06:36 AM
11-01-2002 06:36 AM
Re: shell script: Add details in a file
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