小编gel*_*gel的帖子

输出一些东西(在循环中)直到按下一个键

我正在尝试制作秒表,当用户按下 QI 想要退出时。

我找到了两个脚本,一个在按下 ctrl + z 之前显示时钟。还有一个脚本,如果按下“q”就会退出。

我试图将它们结合起来,但“阅读”似乎把这一切都搞砸了。

我想要实现这一点的原因是,如果用户按 Q,经过的时间将被保存到一个文件中。

跑表:

BEGIN=$(date +%s)

echo Starting Stopwatch...

while true; do
   NOW=$(date +%s)
   let DIFF=$(($NOW - $BEGIN))
   let MINS=$(($DIFF / 60))
   let SECS=$(($DIFF % 60))
   let HOURS=$(($DIFF / 3600))
   let DAYS=$(($DIFF / 86400))

   # \r  is a "carriage return" - returns cursor to start of line
   printf "\r%3d Days, %02d:%02d:%02d" $DAYS $HOURS $MINS $SECS
   sleep 0.25
done
Run Code Online (Sandbox Code Playgroud)

在 q 上退出:

while true; do
    echo -en "Press Q to exit \t\t: " …
Run Code Online (Sandbox Code Playgroud)

shell-script

7
推荐指数
1
解决办法
2万
查看次数

终端与 .txt 文档中的不同格式

我有一个跟踪时间的脚本。

当我在控制台中打印输出时,我得到以下信息:

0 Days, 00:00:33
Run Code Online (Sandbox Code Playgroud)

当我将它保存到 txt 文件时,我得到了这个:

^[[2K
  0 Days, 00:00:33
Run Code Online (Sandbox Code Playgroud)

代码:

now=$(date +%s)
diff=$(($now - $begin))
mins=$(($diff / 60))
secs=$(($diff % 60))
hours=$(($diff / 3600))
days=$(($diff / 86400))

printf "\33[2K\r%3d Days, %02d:%02d:%02d" $days $hours $mins $secs
printf "\33[2K\r%3d Days, %02d:%02d:%02d" $days $hours $mins $secs >> test.txt
Run Code Online (Sandbox Code Playgroud)

哪里^[[2K来的?我猜它与 printf 中的格式有关。

我在这里阅读了一些关于 printf 的内容:http : //wiki.bash-hackers.org/commands/builtin/printf。但我没有变得更聪明......

shell-script date printf

6
推荐指数
2
解决办法
689
查看次数

Add text to each value while looping thru and printing them in a array?

I'm trying to add a text to every value in array while I loop thru them.

I tried this:

for value in "${array[@]}"
do
    echo "--" "$value"
done
Run Code Online (Sandbox Code Playgroud)

It will only add "--" one time. And that's in the beginning of the output.

I've tried printf also, like this:

printf "%s--" "${array[@]}"
Run Code Online (Sandbox Code Playgroud)

Same result.

I add my values like this:

array+="1"
array+="2"
array+="3"
Run Code Online (Sandbox Code Playgroud)

I declare my array like this:

array=()
Run Code Online (Sandbox Code Playgroud)

Also tried:

declare -a array
Run Code Online (Sandbox Code Playgroud)

Is this the correct behaviour, …

bash array

3
推荐指数
1
解决办法
171
查看次数

标签 统计

shell-script ×2

array ×1

bash ×1

date ×1

printf ×1