需要一个用于旋转光标的 shell 脚本

发布于 2025-01-11 06:09:51 字数 390 浏览 0 评论 0原文

我需要一个用于旋转光标的 shell 脚本,我可以将其与“复制”功能一起使用。我尝试了下面的程序并且它有效,但我遇到的唯一问题是微调器显示在文本下方。

#!/bin/bash
spinner=('\' '/' '-' '\')

copy(){
echo "copying files..."
spin &
pid=$!
for i in `seq 1 10`
do 
sleep 1
done
kill $pid
echo ""
}

spin(){
while [ 1 ]
do 
for i in "${spinner[@]}"
do
echo -ne "\r$i"
sleep 0.2
done
done
}
copy

预期输出:正在复制文件...\

I need a shell script for spinning cursor which I can use with "copying" function. I tried below program and it works but the only problem I have here is that the spinner is showing below the text.

#!/bin/bash
spinner=('\' '/' '-' '\')

copy(){
echo "copying files..."
spin &
pid=$!
for i in `seq 1 10`
do 
sleep 1
done
kill $pid
echo ""
}

spin(){
while [ 1 ]
do 
for i in "${spinner[@]}"
do
echo -ne "\r$i"
sleep 0.2
done
done
}
copy

Expected Output: Copying files...\

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

反话 2025-01-18 06:09:51
#!/bin/bash

spinner () {
    local chars=('|' / - '\')

    # hide the cursor
    tput civis
    trap 'printf "\010"; tput cvvis; return' INT TERM

    printf %s "$*"

    while :; do
        for i in {0..3}; do
            printf %s "${chars[i]}"
            sleep 0.3
            printf '\010'
        done
    done
}

copy ()
{
    local pid return

    spinner 'Copying 5 files... ' & pid=$!

    # Slow copy command here
    sleep 4

    return=$?

   # kill spinner, and wait for the trap commands to complete
    kill "$pid"
    wait "$pid"

    if [[ "$return" -eq 0 ]]; then
        echo done
    else
        echo ERROR
    fi
}

copy

根据您使用此功能的方式,您可能希望先隐藏光标,然后再显示它。而不是为多次 copyspinner 调用打开和关闭它。例如:

#!/bin/bash

trap 'tput cvvis' EXIT
tput civis

copy
copy
# other stuff

tput civis 运行时,光标隐藏,当脚本退出时(正常情况下,或由于中断等),光标取消隐藏(tput cvvis)。如果您这样做,请从函数中删除相应的 tput 命令(但保留其他陷阱命令)。

隐藏光标的原因是它可能会扰乱旋转动画。

#!/bin/bash

spinner () {
    local chars=('|' / - '\')

    # hide the cursor
    tput civis
    trap 'printf "\010"; tput cvvis; return' INT TERM

    printf %s "$*"

    while :; do
        for i in {0..3}; do
            printf %s "${chars[i]}"
            sleep 0.3
            printf '\010'
        done
    done
}

copy ()
{
    local pid return

    spinner 'Copying 5 files... ' & pid=$!

    # Slow copy command here
    sleep 4

    return=$?

   # kill spinner, and wait for the trap commands to complete
    kill "$pid"
    wait "$pid"

    if [[ "$return" -eq 0 ]]; then
        echo done
    else
        echo ERROR
    fi
}

copy

Depending on how you use this, you probably want to hide the cursor earlier, and show it later. Instead of turning it on and off for multiple copy or spinner invocations. Eg:

#!/bin/bash

trap 'tput cvvis' EXIT
tput civis

copy
copy
# other stuff

The cursor is hidden when tput civis runs, and un-hidden (tput cvvis) when the script exits (normally, or due to interrupt etc). If you do it like this, remove the corresponding tput commands from the function (but keep the other trap commands).

The reason for hiding the cursor is that it can mess with the spinner animation.

予囚 2025-01-18 06:09:51

如果您不需要换行符,请打印文本 Copying files... 且不带尾随换行符:

echo -n Copying files...

Print the text Copying files... without trailing newline, if you don't want one:

echo -n Copying files...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文