如何让 bash 变量根据时间在单数和复数结尾之间更改单词?
How can I make a bash variable change a word between singular and plural endings based on a count of time?
我还不是一个好的 bash 脚本编写者。
在下面的示例脚本中,我试图通过步骤移动,检查自步骤开始以来经过的时间量,并使用正确的单数或复数结尾来描述已经过去的时间量。
如果经过的时间四舍五入为 1 秒,我希望脚本告诉我完成该步骤需要 1 "second",如果除了 1 秒之外还有其他任何时间,我想让它告诉我它花了 x 数量 "seconds"
#!\bin\bash
STEP=0
STEPS=4
RUN=1
while [ $RUN -eq 1 ]
do
if (( RUNTIME == 1 ))
then
SEC="second"
else
SEC="seconds"
fi
STEP=$(( STEP + 1 ))
printf "\nStep $STEP/$STEPS\n"
printf "Hi!\n"
TIME=$SECONDS
RUNTIME=$(( TIME - START_TIME ))
printf "That took $RUNTIME $SEC\n\n"
STEP=$(( STEP + 1 ))
printf "Step $STEP/$STEPS\n"
printf "Hi!\n"
sleep 1s
TIME1=$SECONDS
RUNTIME=$(( TIME1 - TIME ))
printf "That took $RUNTIME $SEC\n\n"
STEP=$(( STEP + 1 ))
printf "Step $STEP/$STEPS\n"
printf "Hi!\n"
sleep 2s
TIME2=$SECONDS
RUNTIME=$(( TIME2 - TIME1 ))
printf "That took $RUNTIME $SEC\n\n"
STEP=$(( STEP + 1 ))
printf "Step $STEP/$STEPS\n"
printf "Hi!\n"
sleep 3s
TIME3=$SECONDS
RUNTIME=$(( TIME3 - TIME2 ))
printf "That took $RUNTIME $SEC\n\n"
END_TIME=$SECONDS
RUNTIME=$(( END_TIME - START_TIME ))
printf "The script took $RUNTIME $SEC to complete.\n\n"
RUN=0
done
David,如果您还没有找到问题,那更多的是 chicken-or-egg 问题。这意味着您要根据循环 开始 处 RUNTIME
的值设置 SEC
,而不是紧接循环之前 RUNTIME
的当前值输出。一个更好的处理方法,而不是在循环的顶部计算 SEC
,只是编写一个小函数,根据 [=] 的值输出 "second"
或 "seconds"
13=] 并将其合并到您的 printf
中(如果您不在 WSL 上,则不应在 格式字符串 中包含双 \
- 尽管从您的使用其中,看起来你是)
你可以做一些简单的事情,比如:
#!/bin/bash
STEP=0
STEPS=4
RUN=1
secorsecs () { ## simple sec or secs function based on $RUNTIME
local seconds="second" ## your base output is "second"
if (( RUNTIME != 1 )) ## if RUNTIME != 1
then
seconds+="s" ## append an "s" to the end
fi
printf "%s" "$seconds" ## output result
}
while [ $RUN -eq 1 ]
do
STEP=$(( STEP + 1 ))
printf "\nStep $STEP/$STEPS\n"
printf "Hi!\n"
TIME=$SECONDS
RUNTIME=$(( TIME - START_TIME ))
printf "That took $RUNTIME $(secorsecs)\n\n"
STEP=$(( STEP + 1 ))
printf "Step $STEP/$STEPS\n"
printf "Hi!\n"
sleep 1s
TIME1=$SECONDS
RUNTIME=$(( TIME1 - TIME ))
printf "That took $RUNTIME $(secorsecs)\n\n"
STEP=$(( STEP + 1 ))
printf "Step $STEP/$STEPS\n"
printf "Hi!\n"
sleep 2s
TIME2=$SECONDS
RUNTIME=$(( TIME2 - TIME1 ))
printf "That took $RUNTIME $(secorsecs)\n\n"
STEP=$(( STEP + 1 ))
printf "Step $STEP/$STEPS\n"
printf "Hi!\n"
sleep 3s
TIME3=$SECONDS
RUNTIME=$(( TIME3 - TIME2 ))
printf "That took $RUNTIME $(secorsecs)\n\n"
END_TIME=$SECONDS
RUNTIME=$(( END_TIME - START_TIME ))
printf "The script took $RUNTIME $(secorsecs) to complete.\n\n"
RUN=0
done
例子Use/Output
$ bash seconds.sh
Step 1/4
Hi!
That took 0 seconds
Step 2/4
Hi!
That took 1 second
Step 3/4
Hi!
That took 2 seconds
Step 4/4
Hi!
That took 3 seconds
The script took 6 seconds to complete.
检查一下,如果您还有其他问题,请告诉我。
我还不是一个好的 bash 脚本编写者。
在下面的示例脚本中,我试图通过步骤移动,检查自步骤开始以来经过的时间量,并使用正确的单数或复数结尾来描述已经过去的时间量。
如果经过的时间四舍五入为 1 秒,我希望脚本告诉我完成该步骤需要 1 "second",如果除了 1 秒之外还有其他任何时间,我想让它告诉我它花了 x 数量 "seconds"
#!\bin\bash
STEP=0
STEPS=4
RUN=1
while [ $RUN -eq 1 ]
do
if (( RUNTIME == 1 ))
then
SEC="second"
else
SEC="seconds"
fi
STEP=$(( STEP + 1 ))
printf "\nStep $STEP/$STEPS\n"
printf "Hi!\n"
TIME=$SECONDS
RUNTIME=$(( TIME - START_TIME ))
printf "That took $RUNTIME $SEC\n\n"
STEP=$(( STEP + 1 ))
printf "Step $STEP/$STEPS\n"
printf "Hi!\n"
sleep 1s
TIME1=$SECONDS
RUNTIME=$(( TIME1 - TIME ))
printf "That took $RUNTIME $SEC\n\n"
STEP=$(( STEP + 1 ))
printf "Step $STEP/$STEPS\n"
printf "Hi!\n"
sleep 2s
TIME2=$SECONDS
RUNTIME=$(( TIME2 - TIME1 ))
printf "That took $RUNTIME $SEC\n\n"
STEP=$(( STEP + 1 ))
printf "Step $STEP/$STEPS\n"
printf "Hi!\n"
sleep 3s
TIME3=$SECONDS
RUNTIME=$(( TIME3 - TIME2 ))
printf "That took $RUNTIME $SEC\n\n"
END_TIME=$SECONDS
RUNTIME=$(( END_TIME - START_TIME ))
printf "The script took $RUNTIME $SEC to complete.\n\n"
RUN=0
done
David,如果您还没有找到问题,那更多的是 chicken-or-egg 问题。这意味着您要根据循环 开始 处 RUNTIME
的值设置 SEC
,而不是紧接循环之前 RUNTIME
的当前值输出。一个更好的处理方法,而不是在循环的顶部计算 SEC
,只是编写一个小函数,根据 [=] 的值输出 "second"
或 "seconds"
13=] 并将其合并到您的 printf
中(如果您不在 WSL 上,则不应在 格式字符串 中包含双 \
- 尽管从您的使用其中,看起来你是)
你可以做一些简单的事情,比如:
#!/bin/bash
STEP=0
STEPS=4
RUN=1
secorsecs () { ## simple sec or secs function based on $RUNTIME
local seconds="second" ## your base output is "second"
if (( RUNTIME != 1 )) ## if RUNTIME != 1
then
seconds+="s" ## append an "s" to the end
fi
printf "%s" "$seconds" ## output result
}
while [ $RUN -eq 1 ]
do
STEP=$(( STEP + 1 ))
printf "\nStep $STEP/$STEPS\n"
printf "Hi!\n"
TIME=$SECONDS
RUNTIME=$(( TIME - START_TIME ))
printf "That took $RUNTIME $(secorsecs)\n\n"
STEP=$(( STEP + 1 ))
printf "Step $STEP/$STEPS\n"
printf "Hi!\n"
sleep 1s
TIME1=$SECONDS
RUNTIME=$(( TIME1 - TIME ))
printf "That took $RUNTIME $(secorsecs)\n\n"
STEP=$(( STEP + 1 ))
printf "Step $STEP/$STEPS\n"
printf "Hi!\n"
sleep 2s
TIME2=$SECONDS
RUNTIME=$(( TIME2 - TIME1 ))
printf "That took $RUNTIME $(secorsecs)\n\n"
STEP=$(( STEP + 1 ))
printf "Step $STEP/$STEPS\n"
printf "Hi!\n"
sleep 3s
TIME3=$SECONDS
RUNTIME=$(( TIME3 - TIME2 ))
printf "That took $RUNTIME $(secorsecs)\n\n"
END_TIME=$SECONDS
RUNTIME=$(( END_TIME - START_TIME ))
printf "The script took $RUNTIME $(secorsecs) to complete.\n\n"
RUN=0
done
例子Use/Output
$ bash seconds.sh
Step 1/4
Hi!
That took 0 seconds
Step 2/4
Hi!
That took 1 second
Step 3/4
Hi!
That took 2 seconds
Step 4/4
Hi!
That took 3 seconds
The script took 6 seconds to complete.
检查一下,如果您还有其他问题,请告诉我。