bash脚本来创建一个HTML导航菜单 [英] bash script to create a HTML nav menu

查看:99
本文介绍了bash脚本来创建一个HTML导航菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前段时间我通过一个目录树写了一个JavaScript程序循环,并从第3级树的建立一个HTML导航菜单文件。我现在正在尝试复制这种使用bash,因为JS程序需要IE浏览器和ActiveX运行。我有理由新打坏,所以这对我来说是很好的学习经验。

A while ago I wrote a javascript program to loop through a directory tree and build a html nav menu file from the first 3 levels of the tree. I now am trying to replicate this using bash, as the JS program requires the IE browser and activeX to run. I am reasonably new to bash, so this is a great learning experience for me.

所以,我已经是一个结构如下:

So, what I have is a structure as follows:

-Folder A
    --Folder B
    --Folder C
         --Folder C1
         --Folder C2
               --Folder CC1
    --Folder D
         --Folder D1
    --Folder E

等。你明白了吧。无论如何,文件夹名称各不相同,但每个文件夹中都有一个单一的文本文件,它被称为foldername.txt。在这个文件是文本的一行具有实际的文件夹名被在菜单上使用的(这是由于一些名称的长度)。

etc. You get the point. Anyway, the folder names vary, but each folder has a single text file in it called "foldername.txt". In this file is a single line of text that has the actual folder name to be used on the menu (this is due to the length of some of the names).

我因此通过每个文件夹/子文件夹下只有3级试图循环,读取该文件夹中的每个foldername.txt文件,返回的名称,同时保持文件夹层次结构。我希望是有道理的。输出附有HTML标签和呼应到.htm文件。

I am therefore trying to loop through each folder/subfolder down to level 3 only, read each foldername.txt file in the folder and return the name, whilst maintaining the folder heirarchy. I hope that makes sense. The output is appended with html tags and echoed to a .htm file.

到目前为止,我已经尝试不同的东西。在code以下几乎不我想,它会扫描目录,并按照文本文件返回的名字是什么,但不维护层次结构。我没有一个版本发现,不幸的是,包括-maxdepth的。正如你所看到的,我已经尝试嵌套的循环,因为它是只有3个深,但递归继续为每个级别,使我得到重复的和奇怪的结果。

So far I have tried different things. The code below almost does what I want, it will scan directories and return the names as per the text file, but does not maintain heirarchy. I do not have a version of find that includes -maxdepth unfortunately. As you can see, I've tried nested loops as it's only 3 deep, but the recursion continues for each level so that I get duplicate and odd results.

#!/bin/bash

ROOT=/data/
OUTPUTFILE=${ROOT}/Menu-test.html

# Create first level items - these are static
HEADING="<UL class=navlist1>
<LI><SPAN class=plus><p>-</p></SPAN><A class=''>Level 1 products</A></LI>"

END="</UL>"
L2="<UL class=navlist2>"
L3="<UL class=navlist3>"
LI="<LI><SPAN class=plus><P>+</P></SPAN>"
LIEND="</LI>"

echo $HEADING > $OUTPUTFILE;

# set shell options
shopt -s nullglob

# loop through top level dir
for d in $DIR/*/
do
    for file in $(find $d -name "foldername.txt");
    do
        OUT=$(awk '{ print $0 }' $file)
        echo $LI$OUT$LIEND >> $OUTPUTFILE; 
    done

    # loop through second level dir
    for e in $d/*/
        do
            echo $L2 >> $OUTPUTFILE;
            for file2 in $(find $e -type f -name "foldername.txt");
                do
                    OUT2=$(awk '{ print $0 }' $file2)
                    echo $LI$OUT2$LIEND >> $OUTPUTFILE; 
                done
            echo $END >> $OUTPUTFILE;

                # loop through third level dir
                for f in $e/*/
                    do
                        echo $L3 >> $OUTPUTFILE;
                        for file3 in $(find $f -type f -name "foldername.txt");
                            do
                                OUT3=$(awk '{ print $0 }' $file3)
                                echo $LI$OUT3$LIEND >> $OUTPUTFILE; 
                            done
                        echo $END >> $OUTPUTFILE;
                    done
        done
done
echo $END >> $OUTPUTFILE;

很抱歉的长期职位和凌乱code,但我真的很想尝试这种做自己第一次,因为这是我如何学习最好的。等我怎么能得到这个工作的任何想法。请注意,我没有进入Python或任何其他语言,因此打坏它。

Sorry for the long post and messy code, but I really wanted to try doing this myself first as this is how I learn best. So any ideas on how I could get this to work. Please note that I do not have access to Python or any other language, so bash it is.

我要找的输出结果类似如下(连字符仅保持格式和不在输出):

The output I'm looking for would something like as follows (hyphens are only to maintain formatting and are not in the output):

**<LI><SPAN class=plus><P>+</P></SPAN><A href=''>**
<UL class='navlist1'> 
    <LI><SPAN class='plus'><p>-</p></SPAN><A class=''>Folder A</A> 
        <UL class='navlist2' style='display:block'> 
            <LI><SPAN class='bull'><p class='bull'>&bull;</p></SPAN><A href='http://www.somewhere.com/index.htm'>Folder A1</A></LI> 
            <LI><SPAN class='bull'><p class='bull'>&bull;</p></SPAN><A href='http://www.somewhere.com/index.htm'>Folder A2</A></LI> 
            <LI><SPAN class='bull'><p class='bull'>&bull;</p></SPAN><A href='http://www.somewhere.com/index.htm'>Folder A3</A></LI> 
        </UL**></A>
    <UL class=navlist1>**
        <LI><SPAN class=plus><P>+</P></SPAN><A href=''>Folder B</A></LI>
        <LI><SPAN class=plus><P>+</P></SPAN><A href=''>Folder C</A></LI>
        <LI><SPAN class=plus><P>+</P></SPAN><A href=''>Folder D</A></LI>
           <UL class=navlist2>
                <LI><SPAN class=plus><P>+</P></SPAN><A href=''>Folder D1</A>
                    <UL class=navlist3>
                        <LI><SPAN class=plus><P>+</P></SPAN><A href=''>Folder D1A</A></LI>
                        <LI><SPAN class=plus><P>+</P></SPAN><A href=''>Folder D1B</A></LI>
                        <LI><SPAN class=plus><P>+</P></SPAN><A href=''>Folder D1C</A></LI>
                        <LI><SPAN class=plus><P>+</P></SPAN><A href=''>Folder D1D</A></LI>
                        <LI><SPAN class=plus><P>+</P></SPAN><A href=''>Folder D1E</A></LI>
                    </UL>
                </LI>
                <LI><SPAN class=plus><P>+</P></SPAN><A href=''>Folder D2</A>
                    <UL class=navlist3>
                        <LI><SPAN class=plus><P>+</P></SPAN><A href=''>Folder D2A</A></LI>
                        <LI><SPAN class=plus><P>+</P></SPAN><A href=''>Folder D2B</A></LI>
                        <LI><SPAN class=plus><P>+</P></SPAN><A href=''>Folder D2C</A></LI>
                    </UL>
                </LI>
            </UL>
        </LI>
        <LI><SPAN class=plus><P>+</P></SPAN><A href=''>Folder E</A></LI>
        <LI><SPAN class=plus><P>+</P></SPAN><A href=''>Folder F</A></LI>
        <LI><SPAN class=plus><P>+</P></SPAN><A href=''>Folder G</A></LI>
            <UL class=navlist2>
                <LI><SPAN class=plus><P>+</P></SPAN><A href=''>Folder G1</A>
                    <UL class=navlist3>
                        <LI><SPAN class=plus><P>+</P></SPAN><A href=''>Folder G1A</A></LI>
                        <LI><SPAN class=plus><P>+</P></SPAN><A href=''>Folder G1B</A></LI>
                        <LI><SPAN class=plus><P>+</P></SPAN><A href=''>Folder G1C</A></LI>
                        <LI><SPAN class=plus><P>+</P></SPAN><A href=''>Folder G1D</A></LI>
                    </UL>
                </LI>
            </UL>
        </LI>
    </UL>
**</LI>**

所以,这是当前的工作输出。我还需要做的是包括在每个在href链接,但是,子文件夹中的每个级别都会有不同的路径,在以下结构:

So, this is the current working output. What I also need to do is include the href link in each of the , however, each level of subfolder will have a different path, in the following structure:

navlist1 = http://www.somewhere.com/here/ /landing.htm

navlist1 = http://www.somewhere.com/here//landing.htm

&navlist2放大器; 3 = http://www.somewhere.com/here/there/ 将/index.htm

navlist2 & 3 = http://www.somewhere.com/here/there//index.htm

文件夹是目录中的实际文件夹名称,而不是在文本文件中的名称,很明显的联系,否则不会工作。

folder is the actual folder name of the directory, not the name in the text file, obviously the link won't work otherwise.

在上面的粗体字行不应该在输出中。

The lines in bold above should not be in the output.

感谢您。

推荐答案

另外一个递归一(我忍不住了,抱歉); - )

Also a recursive one (I couldn't resist, sorry) ;-)

#!/bin/bash

#preset variables, exec redirects everything to outputfile
ROOT="/data"
LABEL="foldername.txt"
MAXDEPTH=5
DEPTH=0
HTTP="http://www.somewhere.com"
exec > "$ROOT/Menu-test.html"

#functions for indentation, definition and printing tags
LI="<LI><SPAN class=plus><P>+</P></SPAN>"
ULecho() { Dent ; echo "<UL class='navlist$DEPTH'>"                    ;}
LIecho() { echo -n "$LI<A href='$HTTP${1/$ROOT/}/'>$( cat $LABEL)</A>" ;}
Indent() { for (( i=1 ; i < DEPTH ; ++i )); do Dent; Dent; done ; Dent ;}
Dent()   { echo -n "    "                                              ;}
LIstrt() { Indent; LIecho "$( pwd )" ; echo "</LI>"                    ;}
ULstrt() { Indent; LIecho "$( pwd )" ; echo; Indent; ULecho            ;}
TAGend() { Indent ; Dent ; echo "</UL>"; Indent; echo "</LI>"          ;}
DEPchk() { [ "$DEPTH" -gt "0" ] && ${1} ;}

:> $ROOT/$LABEL

Dive()
{
    local DPATH="$1"


    if [ "$( echo */$LABEL )" = "*/$LABEL" ] || [ $DEPTH -gt $MAXDEPTH ]
    then
        DEPchk LIstrt
    else
        DEPchk ULstrt
        for DPATH in */$LABEL
        do
            cd ${DPATH%/*}
              (( ++DEPTH ))
            Dive "$DPATH"
              (( --DEPTH ))
            cd ..
        done
        DEPchk TAGend
    fi
}

cd $ROOT
Dive "$ROOT"
echo "</UL>"

更新:结果
我尝试添加一些你也提到了额外的,但我觉得我没有它的一个具体的构想。也许是因为我的HTML知识几乎是不存在的。所以,你真的要拼出来给我; - )

UPDATE:
I tried to add some of the extra's you also mentioned but I think I do not have a detailed vision of it. Maybe because my knowledge of HTML is almost non-existent. So you really have to spell it out for me ;-)

这篇关于bash脚本来创建一个HTML导航菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆