我正在尝试在以下 unix/linux 中对库存清单进行排序。我成功地按名称和 ID 进行排序,并能够调出使用菜单,但无法按价格排序,即使使用“awk”函数也是如此。
我使用 调用代码./invOpt -p
,输出继续告诉我这是一个invalid option: -p
然后拉出下面的使用菜单。我很困惑,因为它拒绝对价格所在的第 4 个字段进行排序。我不知道 $ 和/或逗号符号是否扰乱了排序
"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
当前代码:
#!/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 < "$tempFile"
# Format the total inventory value with commas
formattedTotalInventoryValue=$(echo $totalInventoryValue | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta')
# Print the total inventory value
printf "\nTotal Inventory Value: $%-14s\n" "$formattedTotalInventoryValue"
# Clean up the temporary file
rm -f "$tempFile"
你遇到了这种行为(从 的输出来看
help getopts
):当你写:
您缺少一个要求参数,因为您已经在调用中定义了该
-p
选项:p:
getopts
因此,当您编写时
-p
没有以下参数,opt
则设置为:
。由于您没有针对这种情况的显式处理程序,因此它最终会达到您的默认情况:这就是您收到“无效选项”错误的原因。您的代码似乎实际上并不期望该选项有一个值,因此最简单的解决方案是像这样
-p
重写您的调用:getopts