博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

Ajian的学习天地


简单人生 Simple Life

生存是为了自己。。。。 而生活是为了所有的人。。。。自己快乐就能感染不快乐的人。自己学会快乐也就教会别人快乐。。。。 所以从现在起学会生活,学会快乐。。-----Ajian

  ajian.cublog.cn

关于作者
姓名: Ajian
年龄: 21
职位: 系统工程师
Q Q:  63894624
MSN:  5root@live.cn
mail: 5root@live.cn
   转眼一年过去了,凭着自己的热情和执着,实现了自己不少的愿望,可以慢慢还我欠下的所有的情,其中不枉有很多艰辛也有很多乐趣,新的一年继续加油!A ZA A ZA ^_^
   补充句我的Blog搬家了,新家的地址http://www.AjianZone.cn
|| << >> ||
我的分类


Shell程序集锦三——花样Shell[转载]
    前言:程序是由我之前收集的具体的出去已经不知道了,程序已经自己用过,我归为花样Shell,因为可以实现文字的颜色还可以造出不同的图案出来。
     
   

程序一:
彩色显示文件程序,每个字符的颜色都不一样,随机变化,让你看花眼睛^_^:
程序代码:
#! /usr/bin/awk
#  Write by dbcat
#  EMail:deeperbluecat@Gmail.com
#  run : awk -f ColorCat.awk YourFile
BEGIN{
        srand()
}
{
        split($0,Myth,"")
        ColorPrint(Myth,length($0))
}

function ColorPrint(Myth,xlen)
{
   for(i=1;i<=xlen;i++)
    {
       Color="\033[1;"int(31+7*rand())
       printf "%s;3m%s\033[0m",Color,Myth[i]
    }
    printf "\n"
}
 
程序二:
彩色作图程序,有点像Gnuplot,可以作出圆形,正弦图,抛物线等等。
例如:
   圆:      awk 'BEGIN{while(k<10){print sin(k),cos(k);k=k+0.01}}'   |  awk -f ColorPlot.awk
   正弦线:   awk 'BEGIN{while(k<10){print sin(k),k;k=k+0.01}}'        |  awk -f ColorPlot.awk
   抛物线:  awk 'BEGIN{k=-10;while(k<10){print k^2,k;k=k+0.01}}'     |  awk -f ColorPlot.awk
   直线:    paste <(seq 1 0.01 10)  <(seq 1 0.01 10)                 |  awk -f ColorPlot.awk

如果你有想象力的话还可以作出很多意想不到的图形,比如:
   圆盘:    awk 'BEGIN{while(k<100){print sin(k),rand()*cos(k);k=k+0.01}}' |awk -f ColorPlot.awk
   花圈:    awk 'BEGIN{srand()
                        while(k++<20000){
                           x=2-3*rand()
                           y=2-4*rand()
                           if(x^2+y^2>0.6&&x^2+y^2<1||x^2+y^2<0.3&&x^2+y^2>0.1)
                              print x,y
                           }
                       }'       | awk -f ColorPlot.awk
   菱圈:    awk 'BEGIN{srand()
                        while(k++<20000){
                           x=1-2*rand()
                           y=1-2*rand()
                           if(x+y<=1&&x-y<=1&&-x+y<=1&&-x-y<=1&&x^2+y^2>=1/2)
                              print x,y
                           }
                       }'       | awk -f ColorPlot.awk
 

程序代码:
#! /usr/bin/awk
# GAWK彩色作图程序
# 作者: dbcat
# Email: deeperbluecat@Gmail.Com
# 日期: 2006-9-25
# 测试环境: Gawk 3.1.4, bash 3.00.16(1), SUSE 9.3
# 运行方法: awk 'BEGIN{while(k<10){print sin(k),cos(k);k=k+0.01}}' >datafile
#           awk -f ColorPlot.awk datafile
BEGIN{
        srand()
        xlen=35
        ylen=35
        InitGraph(Myth,xlen,ylen)
}

{
        X_Max=X_Max>$1?X_Max:$1
        X_Min=X_Min<$1?X_Min:$1
        Y_Max=Y_Max>$2?Y_Max:$2
        Y_Min=Y_Min<$2?Y_Min:$2
        X_Label[NR]=$1
        Y_Label[NR]=$2
}

END{
        CreateGraph(Myth,NR)
        PrintGraph(Myth)
}
function InitGraph(Myth,xlen,ylen,i,j)
{
   for(i=1;i<=xlen;i++)
     for(j=1;j<=ylen;j++)
        Myth[i,j]=" "
}

function CreateGraph(Myth,Len,i)
{
       for(i=1;i<=Len;i++)
          {
           X_Label[i]=int((X_Label[i]-X_Min)/(X_Max-X_Min)*(xlen-1) + 1)
           Y_Label[i]=int((Y_Label[i]-Y_Min)/(Y_Max-Y_Min)*(ylen-1) + 1)
           Myth[X_Label[i],Y_Label[i]]=int(40+60*rand())
          }
}

function PrintGraph(Myth,i,j)
{
   for(i=1;i<=xlen;i++)
    {
      for(j=1;j<=ylen;j++)
         {
          color="\033[1;"int(31+7*rand())
          printf " %s;1m%c\033[0m",color,Myth[i,j]
         }
      printf "\n"
    }
}
心型:
awk 'BEGIN{while(u<20){print sin(u)*sin(v),cos(u)*sin(v+u);v=v+0.01;u=u+0.01}}' | awk -f ColorPlot.awk

 

显示彩色字符的shell脚本
#!/bin/bash
#"Colorizing" Scripts
#First,define functions.
function subColor()
{
Fg=$1
Bg=$2
SetColor="\E[""$Fg;$Bg""m"
UseColor="\033[""$3""m"
EndColor="\033[0m"
Content=$e
echo -en "$SetColor""$UseColor"$Content"$EndColor"
}
 
function ShowHelp()
{
echo "Error!"
echo "Your parameters were $a,$b,$c,they are unexpected parameters."
echo "Show help file or continue?(h|c)"
read Choice
case $Choice in
    h|H)
        echo "This is a script for coloring characters and strings."
        echo "There are four parameters.Parameters are seperated by spacebars."
        echo "The frst parameter is a number ranged from 1 to 10,represents the foreground color."
        echo "The second parameter is  a number ranged from 1 to 10,represents the background color."
        Color 1 8 2 "1    red";echo
        Color 2 8 2 "2    green";echo
        Color 3 8 2 "3    yellow";echo
        Color 4 8 2 "4    blue";echo
        Color 5 8 2 "5    magenta";echo
        Color 6 8 2 "6    cyan";echo
        Color 7 8 2 "7    gray";echo
        Color 8 9 2 "8    white";echo
        Color 9 8 2 "9    white";echo
        Color 10 9 2 "10   black";echo
        echo "The third parameter is a number ranged from 1 to 9,represents the style of the characters."
        Color 10 8 1 "1    lighter,and bold";echo
        Color 9 8 4 "4    draw a line under the string.";echo
        Color 9 8 5 "5    coruscate the string.";echo
        Color 9 8 7 "7    swap the foreground color and the background color";echo
        Color 9 8 9 "9    draw a deleting line";echo
        echo "The fourth parameter is the content you wanna clolor,a string."
        echo "Thanks for using this script ! "
echo "Script halted."
exit 1
        ;;
    c|C)
        echo "script halted."
        exit 1
        ;;
    *)
        ShowHelp
esac
   
}
 
function ParaCheck()
{
if [ $a -le 0 ] || [ $b -le 0 ] || [ $c -le 0 ] || [ $a -gt 10 ] || [ $b -gt 10 ] || [ $c -gt 9 ]
then
    ShowHelp
else
    if [ $a -eq 10 ]
    then
        a=30
    else
        a=30+$a
    fi
    if [ $b -eq 10 ]
    then
        b=40
    else
        b=40+$b
    fi
fi
}
 
function GenString()
{
    e=""
    declare -i f=1
 
    for d in $@
    do
        if [ $f -eq 1 ] || [ $f -eq 2 ] || [ $f -eq 3 ]
        then
            e=$e
        elif [ $f -eq 4 ]
        then
            e=$d
        else
            e="$e $d"
        fi
       
        let f=$f+1
    done
 
}
 
function Color()
{
declare -i a=$1
declare -i b=$2
declare -i c=$3
ParaCheck
GenString $@
subColor $a $b $c $e
}
#Here is where the script begins.

Color $@
 
 
 

发表于: 2007-12-16,修改于: 2007-12-16 14:40,已浏览287次,有评论0条 推荐 投诉


网友评论
 发表评论