shell腳本動畫小工具


shell腳本動畫小工具

看gif圖:

shell腳本版

腳本內容如下:

#!/usr/bin/env bash

## ------------------------------------------------------------
## Author:博客園——駿馬金龍
## shell scripts:http://www.cnblogs.com/f-ck-need-u/p/7048359.html
## ------------------------------------------------------------

## Usage:$0 "COMMAND"
## you must enclosing the COMMAND by double-quotes
## example1: $0 "sleep 3;echo haha"
## example2: $0 "service mysql start"

killmyself="pkill -13 -f `basename $0`"

trap "$killmyself" sigint

while true;do
    for i in '-' "\\" '|' '/';do
        printf "\r%s" $i
        sleep 0.2
    done
done &

bgpid=$!

tmp="`bash -c \"$@\"`"
kill $bgpid
printf "\r%s\n" "$tmp"
$killmyself

必須將待運行的命令放進引號中包圍,並作為腳本的參數。

## example1: $0 "sleep 3;echo haha"
## example2: $0 "service mysql start"
## example3: $0 "service mysql stop"

perl版

下面是用perl寫的,作用完全一樣。將內容保存到一個文件中,賦予可執行權限即可。同樣,待執行的命令需要使用雙引號包圍。

#!/usr/bin/env perl
use strict;
use warnings;

defined(my $pid = fork) or die "can't fork child: $!";

unless($pid){
    # child process
    select STDOUT; $| = 1;
    while(1){
        foreach my $i ('-','\\','|','/'){
            printf("\r%s",$i);
            select(undef,undef,undef,0.1)
        }
    }
}

my $var = `/bin/sh -c "@ARGV"`;
kill INT => $pid or die "Cannot signal to $pid with SIGINT: $!";
printf "\r%s",$var;

假如該perl文件名為mygif.pl,用法:

./mygif "sleep 3;echo haha"
./mygif "service mysql start"
./mygif "service mysql stop"


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM