【发布时间】:2020-12-04 00:03:25
【问题描述】:
我正在学习 bash 脚本编写,并且我试图编写脚本来解决问题但没有成功。
示例测试用例:
输入:
StoreId,Name,Type,Revenue,StoreExpenses (this line is not provided as cmd line argument)
1,RockDeptStore,stationary,100,50
2,WembleyStore,departmental,85,81
3,HealthyStore,grocery,95,97
4,Ministore,medical,60,55
输出:
1|RockDeptStore|stationary|100|50|50
4|Ministore|medical|60|55|5
2|WembleyStore|departmental|85|81|4
script.sh:
#!/bin/bash
#inputs
for record in "$@"
do
revenue=$(cut -d ',' -f 4 <<< $record)
expenses=$(cut -d ',' -f 5 <<< $record)
((profit=revenue-expenses))
if [[ profit -gt 0 ]]
then
# how to update this record with '|' and where to store this record so that I can access it later in my script for sorting.
fi
done
我需要编写一个shell脚本script.sh,它将每个商店详情的输入作为命令行参数。
我需要使用附加字段 profit = Revenue - StoreExpenses 打印所有商店,并且需要将分隔符从“,”更改为“|”。
并仅打印具有profit > 0 的商店,它们各自的profit 的降序,如上面的示例输出 中给出的。
我们将script.sh 运行为:
./script.sh 1,RockDeptStore,stationary,100,50 2,WembleyStore,departmental,85,81 3,HealthyStore,grocery,95,97 4,Ministore,medical,60,55
【问题讨论】:
-
“正在尝试编写脚本”。请展示您的尝试,描述您遇到的问题,并提出一个有助于您推进尝试的具体问题。不要只要求完整的代码。
-
好的,我已经包含了我正在编写的脚本
script.sh,但是卡在了中间。
标签: linux bash shell scripting sh