Trying to sort with getopts with different commands and cannot sort by price
0
votes
1
answer
29
views
I am trying to sort an inventory list in unix/linux below. I am successful in sorting by name and ID and able to pull up the usage menu but cannot sort by price, even with an "awk" function.
I call the code with
./invOpt -p
and the output continues to tell me it's an invalid option: -p
then pulls up the usage menu below. I'm confused because it refuses to sort the 4th field where the price is. I don't know if the $ and/or the comma sign is messing up the sorting
"Usage: invOpts -i | -n | -p | -h
" -i sort by product ID"
" -n sort by product name"
" -p sort by price"
" -h display usage"
INVENTORY MENU:
Product ID Product Name Quantity Price Total Value
----------- ------------------------------ -------- ----- -----------
P101 Apple MacBook Pro 25 $2,399.99 $59,999.75
P102 Samsung Galaxy S23 40 $799.99 $31,999.60
P103 Apple iPhone 15 60 $999.99 $59,999.40
P104 Google Pixel 8 35 $899.99 $31,499.65
P105 Microsoft Surface Pro 9 18 $1,299.99 $23,399.82
P106 Dell XPS 13 50 $1,099.99 $54,999.50
P107 Apple iPad Air 75 $599.99 $44,999.25
P108 Fitbit Charge 5 100 $179.95 $17,995.00
P109 Amazon Echo Dot 5th Gen 150 $49.99 $7,498.50
P110 Sonos One SL 80 $199.99 $15,999.20
P111 Logitech MX Master 3 120 $99.99 $11,998.80
P112 HP Spectre x360 25 $1,499.99 $37,499.75
P113 GoPro Hero 11 60 $399.99 $23,999.40
P114 Nintendo Switch OLED 45 $349.99 $15,749.55
P115 Canon EOS R6 15 $2,499.99 $37,499.85
P116 Seagate 2TB External Hard Drive 200 $69.99 $13,998.00
P117 Apple AirPods Pro 2nd Gen 130 $249.99 $32,498.70
P118 MSI GeForce RTX 4070 40 $599.99 $23,999.60
P119 Lenovo ThinkPad X1 Carbon 20 $1,799.99 $35,999.80
P120 Anker PowerCore 26800 180 $59.99 $10,798.20
CURRENT CODE:
#!/bin/bash
# Function to display usage information
usage() {
echo "Usage: invOpts -i | -n | -p | -h"
echo " -i sort by product ID"
echo " -n sort by product name"
echo " -p sort by price"
echo " -h display usage"
}
# Process command-line options using getopts
while getopts ":inp:h" opt; do
case $opt in
i)
# Sort by product ID (Field 1) and save to a temp file
tempFile="/tmp/inventory_sorted_by_id_$$.txt"
sort -t: -k1,1 ~/A09/inventory > "$tempFile"
;;
n)
# Sort by product name (Field 2) and save to a temp file
tempFile="/tmp/inventory_sorted_by_name_$$.txt"
sort -t: -k2,2 ~/A09/inventory > "$tempFile"
;;
p)
# Sort by price (Field 4) and save to a temp file
tempFile="/tmp/inventory_sorted_by_price_$$.txt"
sort -t: -k4,4n ~/A09/inventory > "$tempFile"
;;
h)
# Display usage and exit
usage
exit 0
;;
?)
# Handle invalid options
echo "Invalid option: -$OPTARG"
usage
exit 1
;;
esac
done
# If no option is provided, default to original file
if [[ -z $tempFile ]]; then
tempFile=~/A09/inventory
fi
# Source the myFunctions script to use the chkFile function
source ~/Homework9/myFunctions
# Store the inventory file name
filename=~/A09/inventory
# Check if the file exists using chkFile
chkFile $filename
if [[ $? -ne 0 ]]; then
echo "Error: Inventory file does not exist."
exit 1
fi
# Print the header
printf "%-12s %-30s %-10s %-15s %-15s\n" "Product ID" "Product Name" "Quantity" "Price" "Total Value"
printf "%-12s %-30s %-10s %-15s %-15s\n" "-----------" "------------------------------" "--------" "-----" "-----------"
# Initialize total inventory value
totalInventoryValue=0
# Read the sorted file line by line (either sorted or original)
while IFS=: read -r productID productName quantity price; do
# Calculate the total value: Quantity * Price
totalValue=$(echo "$quantity * $price" | bc -l)
# Format the price and total value with commas
formattedPrice=$(echo $price | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta')
formattedTotalValue=$(echo $totalValue | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta')
# Print the formatted data
printf "%-12s %-30s %-10s $%-14s $%-14s\n" "$productID" "$productName" "$quantity" "$formattedPrice" "$formattedTotalValue"
# Add total value to the overall inventory value
totalInventoryValue=$(echo "$totalInventoryValue + $totalValue" | bc -l)
done /,&/;ta')
# Print the total inventory value
printf "\nTotal Inventory Value: $%-14s\n" "$formattedTotalInventoryValue"
# Clean up the temporary file
rm -f "$tempFile"
Asked by Natasha Shorrock
(5 rep)
Dec 10, 2024, 12:24 AM
Last activity: Jan 13, 2025, 09:59 AM
Last activity: Jan 13, 2025, 09:59 AM