即将收集的字符画输出屏幕, 并隔一段时间清空屏幕更新输出的字符画.

OS: Deepin Linux 15.11 stable

Shell: zsh

字符画内容来源: www.asciiartfarts.com

字符画内容获取与处理

首先下载下Ascii Art Farts网站的所有字符画.

wget -r http://www.asciiartfarts.com

得到一个网站目录./www.asciiartfarts.com.

提取含字符画的网页

每副字符画都存在由上传日期命名的网页中,保存在<pre>标签中,题目在<pre>前的<h1>标签中.网站作者还贴心的按照日历/关键词进行了分类,在分类的网页中也储存了相关字符画.用grep+正则表达式穷举处上述几套网页中的一套中的所有网页.

for i in `ls ./www.asciiartfarts.com | grep -x '[0-1][0-9][0-9][0-9].html'`
  do
  # ...
  # 提取网页中的字符画
  # ...
  done

我提取了网站作者按照日历进行分类的网页, grep 用-x是为了严格匹配,否则会把以上传日期命名的网页也匹配进去.

提取网页中的字符画

我的思路是设置提取标记flag,逐行提取网页代码,查找><pre>/</pre>,找到><pre>flag=1,找到</pre>flag=0;flag=1时将提取的字符输出到extract.txt,flag=0时候不输出.

注意每个网页中重复出现的网站Logo保存在第一个<pre>标签里,所以干脆用它的</pre>初始化flag,并查找><pre>以忽略之.

    _    ____   ____ ___ ___      _         _     _____          _       
   / \  / ___| / ___|_ _|_ _|    / \   _ __| |_  |  ___|_ _ _ __| |_ ___ 
  / _ \ \___ \| |    | | | |    / _ \ | '__| __| | |_ / _` | '__| __/ __|
 / ___ \ ___) | |___ | | | |   / ___ \| |  | |_  |  _| (_| | |  | |_\__ \
/_/   \_\____/ \____|___|___| /_/   \_\_|   \__| |_|  \__,_|_|   \__|___/

如果仍然使用for提取内容,因为for默认将空格也视作分隔符,所以要将空格暂时替换以掩藏之.用sed命令将空格替换为&nbsp;.并且,在每副字符画后中插入识别符______识别字符画的结束.

    for j in `sed 's/ /\&nbsp;/g' ./www.asciiartfarts.com/$i`
        do
    echo $j | grep '<h1>' >> extract.txt # 提取字符画的标题
        echo $j | grep '</pre>'
        if [ $? -eq 0 ];then 
        flag=0
        echo '______' >> extract.txt
        fi
        if [ $flag -eq 1 ];then
        echo $j >> extract.txt
        fi
        echo $j | grep '><pre>'
        if [ $? -eq 0 ]; then
        flag=1
        fi
        done

sed脚本中第一个s为替换,最后一个g为贪心/全局(全文操作). ?变量存储了上一条命令的执行状态,$? -eq 0表示命令正常执行(grep找到了对应内容).

最后用sed命令将得到的extract.txt中的&nbsp;还原回空格得到字符画库aa-pic.txt.

sed 's/\&nbsp;/ /g' extract.txt > aa-pic.txt

实际上后续屏保用的数据是extract.txt(可以用for逐行提取)而非aa-pic.txt.

屏保主体脚本

逐行提取extract.txt,用sed替换&nbsp;为空格后打印到屏幕上,并查找字符画终止标记______,找到了就表示一幅画打印完了,停一个$interval给大家欣赏画,再clear屏幕,接着提取打印字符画.

interval=7
for i in `cat extract.txt`
    do
    echo $i | sed 's/\&nbsp;/ /g' # 此步输出字符画
    echo $i | grep -x '______'
    if [ $? -eq 0 ];then
        sleep $interval
    clear
    fi
    done

大功告成.

附上所有代码

提取内容: extract.sh

#!/bin/zsh
wget -r http://www.asciiartfarts.com
/bin/rm extract.txt &> /dev/null
cp 1-pic.txt extract.txt
for i in `ls ./www.asciiartfarts.com | grep -x '[0-1][0-9][0-9][0-9].html'`
    do
    echo "Now extract $i."
    flag=0
    for j in `sed 's/ /\&nbsp;/g' ./www.asciiartfarts.com/$i`
        do
        echo $j | grep '<h1>' >> extract.txt
        echo $j | grep '</pre>'
        if [ $? -eq 0 ];then 
        flag=0
        echo '______' >> extract.txt
        fi
        if [ $flag -eq 1 ];then
        echo $j >> extract.txt
        fi
        echo $j | grep '><pre>'
        if [ $? -eq 0 ]; then
        flag=1
        fi
        done
    done
sed 's/\&nbsp;/ /g' extract.txt > aa-pic.txt

屏保

pp=`pwd`
interval=7
cd /path/to/extract.txt/
for i in `cat extract.txt`
    do
    echo $i |    sed 's/\&nbsp;/ /g'
    echo $i | grep -x '______'
    if [ $? -eq 0 ];then
        sleep $interval
    fi
    echo $i | grep '<h1>' #一显示画的名字就清屏, 这样就不会看到它了.
    if [ $? -eq 0 ];then
        clear
    fi
    done
cd $pp


shell

本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!