看gif图:html
脚本内容以下:mysql
#!/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
必须将待运行的命令放进引号中包围,并做为脚本的参数。sql
## example1: $0 "sleep 3;echo haha" ## example2: $0 "service mysql start" ## example3: $0 "service mysql stop"
下面是用perl写的,做用彻底同样。将内容保存到一个文件中,赋予可执行权限便可。一样,待执行的命令须要使用双引号包围。shell
#!/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,用法:bash
./mygif "sleep 3;echo haha" ./mygif "service mysql start" ./mygif "service mysql stop"