用于插入和更新序列号文件夹名称的bash脚本

时间:2015-08-03 16:48:39

标签: bash shell .bash-profile

重要提示:请参阅下面的 update-1 ,建议新的目录结构。

重要提示:请参阅下面的 update-2 ,了解我为什么要寻找bash_script来编写我的图书。

重要信息:请参阅 update-3 ,了解是否需要与addPage()分开的insertPage()命令。

最后更新:这是我用这个脚本编写的书!:Demo-in-Sixty-Seconds

我手动编写了一本书,它有以下directory结构:

The-Solar-System/
├── README.md
├── css
│   ├── page-1
│   │   └── style.css
│   ├── page-2
│   │   └── style.css
│   └── page-3
│       └── style.css
├── head
│   ├── page-1
│   │   └── head.html
│   ├── page-2
│   │   └── head.html
│   └── page-3
│       └── head.html
├── html
│   ├── page-1
│   │   └── body.html
│   ├── page-2
│   │   └── body.html
│   └── page-3
│       └── body.html
├── images
│   └── page-1
│       └── bubblin-publishers-icon-96x96.png
├── js
│   ├── page-1
│   │   └── script.js
│   ├── page-2
│   │   └── script.js
│   └── page-3
│       └── script.js

到目前为止,事情很简单,我手动完成了大部分目录的添加/删减。现在页面数量(具有页面编号的目录)已经增长,并且逐渐更新所有目录名称以在其间插入新页面变得越来越困难。当一个页面被删除。

所以我决定编写一些脚本并自动执行此操作。这就是我想要初始化我的书(项目)页面的方式。我在Mac上。

步骤1初始化项目(重要信息:请参阅下面的更新-1)

  

book(){mkdir -p" $ 1" &安培;&安培; cd" $ 1" &安培;&安培; mkdir -p" html" "头" " CSS" " JS" "图像" "盖" &安培;&安培;触摸README.md; 11;}

上面的工作,但我想迭代地进入子目录htmlheadcss等等,并创建命名如下的页面目录:

page-1
page-2
page-3
...
...
page-N

其中N是我传递给脚本的参数。像这样:

  

$ book MY-BOOK-NAME N

创建页面目录后,我想初始化每个页面目录页面-1,页面2,等等,如果它位于body.html超级目录中,则使用名为html的文件, style.css如果它在css目录中。

步骤2添加页面

我希望能够添加一个页面(名为page-P的文件夹,其中1 < P <= N)并将现有目录从{page-P重命名为page-N}到{page-P + 1到页面N + 1}。

当书中已存在多个页面(目录)时,会发生这种情况,范围从page-1 ... page-(P-1), page-(P), page-(P+1)... page-N,我们想在中间插入一个页面。

大致以下步骤:

  1. 输入:
      

    $ addPage() P

  2. 通过bash page_number:&#39; P&#39;必须是1 < P <= N

    1. 将{page-Ppage-N}之间的所有现有目录重命名为{page-(P+1)page-(N+1)}

    2. 创建一个new_blank_page目录page-P,其中包含以下资源:body.htmlstyle.css和空白子目录images

    3. 回声成功消息:Page has been added at 'P'

    4. 步骤3:删除页面。

      现在我甚至无法手动执行此操作,因为删除后重新编号页面(根据我)是乏味,低效,不切实际的。如果有一个命令工具可以自动删除和重新编号页面,那就太好了。

      这里有remove_page() P(1 < P <= N)这本书中删除页面P的方式:

      1. 接受命令:
          

        $ removePage() P

      2. 通过bash page_number:&#39; P&#39;必须是1 < P <= N

        1. 删除目录page-P并移动其中的资源:body.htmlstyle.css其他人在项目中说/ trash目录。仅软文删除稿件。

        2. 将{page-P+1page-N}之间的所有现有目录重命名为{page-(P)page-(N-1)}

        3. 如何修改我的脚本以便我可以从终端命令这些步骤?有关初学者的任何优秀文章/资料来源,我建议我可以学习足够的bash编程来做到这一点吗?

          更新-1

          正如下面的评论中所建议的那样,对目录执行反向层次结构是完全有道理的。不知怎的,这个简单的事情对我来说并不明显(感觉很愚蠢),当我开始写这本书时,现在我将不得不重新重做76页my book。唷。

          无论如何,新的目录结构将是这样的:

          The-Solar-System/
          ├── README.md
          ├── page-1
          │   ├── head.html
          │   │   
          │   ├── style.css
          │   │   
          │   ├── body.html
          │   │   
          │   ├── script.js
          │   │   
          │   └── images
          │       └── cat.png
          ├── page-2
          │   ├── head.html
          │   │   
          │   ├── style.css
          │   │   
          │   ├── body.html
          │   │   
          │   ├── script.js
          │   │   
          │   └── images
          │       └── dog.png
          

          ......等等。所以新的bash_script必须像这样工作:

          new_book () {
            mkdir -p "$1" && cd "$1" 
            touch README.md;
            mkdir cover
            && 
          # for loop 1 to N
          # mkdir page_directories
          # mk
          &&  ll;}
          

          我还在考虑如何传递N页面数量,并在项目内迭代创建N页(默认最少4页)。

          然后查看步骤2和步骤3的脚本,如上所述。

          更新-2

          以下答案中一般都有很好的建议,关于如何写书以及如何写书。不幸的是,大多数答案都假设作者总是想要来创建一个工件 - 一个PDF或一个ePub或某种只有简单文本和图像的文件。在我的情况下,情况并非如此。我已经在使用markdownlatex以及下面某些答案中建议的其他工具了。

          另外,我不希望书看起来像静态网站或单个html文件,即网页。

          我已经有一种编写我的书的方法,并且希望使用bash命令来自动执行我经常重复的一些步骤。让我解释一下这里发生了什么以及我为什么需要它:

          我所指的The Solar System上的那本书已在网上撰写和发表。它是使用Bubblin上提供的浏览器内代码编辑器编写的(下面的屏幕截图)。

          Code playground @bubblin

          作为一项单独的练习,我在Github上手动提供了源代码。

          如果您在Firebug / Chrome Developer上查看本书的来源,您会发现每个页面都是一个 iframe ,就像Codepen / JSFiddle一样呈现HTML,CSS和JS在运行时,在客户端。所以这里的页面更像是&#34;单页应用程序&#34; (网页),该书是webpages的集合。当然,这里的练习比普通形式的书写更接近app / web开发。

          我之所以看bash_commands,是因为我正在写两本书,我想在Github上提供这些书的代码。为此,我已经有了git initialized个项目,但是我为每个页面重复了一些步骤(上面讨论过),以便将各个部分组合在一起。我认为如果使用脚本自动执行这些步骤会很好。可以在PerlRubyPython中完成,但将来可以使用{{1}},{{1}}或{{1}}。

          注意我渴望保持干燥,可访问和有意义的建议,但重要的是要保持对各种可能的书籍的灵活性。

          更新-3

          在执行此操作的过程中,我意识到insertPage()方法必须与addPage();前者插入中间,重命名并移动所有后续目录,而后者直接到书的末尾。

          Github上提供gists of all the bash scripts。没什么好看的,只是完成了工作!

          更新-4

          我接着创建了一个完整的框架/ CLI工具,可以让你编写&amp;有效地出版书籍:Bookiza Node CLI

4 个答案:

答案 0 :(得分:6)

我可能错了,但我认为你问的是X-Y problem

您正在用HTML编写一本书

我很惊讶你想用纯HTML写一本书。我认为甚至等其他语言可能更适合您的应用。

当然,通过使用等工具,以后任何事情都无法阻止您生成HTML页面。

通常,当我必须写一本书时,我宁愿专注于内容,而不是风格或渲染。我敢打赌,用更通用的语言编写你的书会更好,这将允许你使用自制脚本或在线转换器生成任何其他格式。通过这种方式,可以轻松创建电子书,PDF格式,多个HTML页面甚至单个页面。

为什么划分内容?

通常一本书一直都有统一的风格。在您的示例中,您提到了多个文件。我真的不明白它的目的。也许您可以向我们提供有关您预订的更多详细信息以及您需要此类复杂性的原因。

此外,我认为每页有一个文件有点过分。通过这种方式,您将失去整体视图,并使您难以编辑和改进本书。如果您仍想用HTML编写图书,我可能会建议您将整本书放在一个文件中。 JavaScript脚本可以在章节中自动划分,并使用目录填充侧栏。

为什么选择了bash?

Bash确实是一种非常强大的语言,但它也非常具有密码性,并不是很适合你想做的事情。

我建议您查看其他语言,例如甚至。使用这些脚本语言,您可以轻松编写带有子命令和参数的主程序。有这样的事情会更方便:

$ book create --name =“My Book”--pages = 200    $ book insert-page --after-page 23    $ book remove-page 55

但是,对于单个HTML文件,您不需要使用这种复杂性。

的-太阳能系统

我查看了你的书,我觉得它有duplicate code。大多数CSS文件看起来都一样。想象一下,您想要更改所有页面的默认字体。你将如何进行?您当然可以使用findsed替换就地,但您也可以在git commit下创建相当长的差异。这对跟踪所有更改并不是很有帮助。

现在考虑第42页:

<div class="leaf ">
  <div class="inner justify">
    <p> <span class="first-word"> M</span>ars is the fourth and the <em>last</em> terrestial planet of our Solar System. What follows beyond Mars are the gas and ice giants - Jupiter, Saturn, Uranus and Neptune - i.e. planets that do not have a solid rocky
      surface. Given that Mercury and Venus are inhospitable, and we couldn't possibly land on gas giants ever, Mars easily becomes a candidate of great interest to see if it can be terraformed. </p>
    <p> Mars is called the red planet because of its reddish-orange tinge off its iron ( Fe<sub>2</sub>O<sub>3</sub> <a href="http://en.wikipedia.org/wiki/Iron%28III%29_oxide">Iron Oxide / Hematite</a> ) rich soil. It has a thin atmosphere of Carbon Dioxide
      (96%), Argon (1.93%) and Nitrogen (1.89%, along with some traces of Oxygen and moisture. The surface of Mars is like that of our Moon: Scarred with thousands of impact craters. It is also exposed to cosmic and solar radiation in absence of enough
      atmospheric resistance or a decent magnetosphere. </p>
    <p> Mars is roughly half the diameter of Earth but only a tenth its mass. Gravity on Mars is thus significantly lower, about 38% of that on Earth. It also receives much lesser sunlight (about 43% less) than we do on our planet. So far no sign of life,
      not even microbial, has been found on Mars. Though, a few plumes of locally ejected methane from below the surface of Mars were detected. </p>
  </div>
</div>

这里真正具体是什么?您需要斜体,可能粗体hyperlinks本身支持所有这些。以下是不是比你的HTML内容更可读?例如,在pandoc上使用Markdown设置为输入语言,将HTML设置为输出语言。

    Mars is the fourth and the *last* terrestial planet of our Solar
 System. What follows beyond Mars are the gas and ice giants - Jupiter, 
Saturn, Uranus and Neptune - *i.e.* planets that do not have a solid rocky
 surface. Given that Mercury and Venus are inhospitable, and we couldn't
 possibly land on gas giants ever, Mars easily becomes a candidate of great
 interest to see if it can be terraformed.

    Mars is called the red planet because of its reddish-orange tinge off its
 iron ( $Fe_2O_3$ ) [Iron Oxide Hematite](http://en.wikipedia.org/wiki/Iron%28III%29_oxide)) rich soil. It has a thin
 atmosphere of Carbon Dioxide (96%), Argon (1.93%) and Nitrogen (1.89%, along
 with some traces of Oxygen and moisture. The surface of Mars is like that of
 our Moon: Scarred with thousands of impact craters. It is also exposed to
 cosmic and solar radiation in absence of enough atmospheric resistance or a
 decent magnetosphere.

    Mars is roughly half the diameter of Earth but only a tenth its mass.
 Gravity on Mars is thus significantly lower, about 38% of that on Earth. 
It also receives much lesser sunlight (about 43% less) than we do on our 
planet. So far no sign of life, not even microbial, has been found on Mars.
 Though, a few plumes of locally ejected methane from below the surface of 
Mars were detected.

你在写游戏手册吗?

我的朋友写了一本在线游戏手册here。它是用法语写的,但这个想法非常好,因为他编写了一个完整的在线编辑器,用于测试here。每个页面都是可以编辑并链接到其他节点的节点。因此,整本书不是用纯HTML编写的。它使用数据库来存储所有信息。

有了这个,我们仍然可以想象使用GitHub来跟踪您的更改并帮助人们为您的图书做出贡献。

结论

我真的不认为你的问题可以帮助你写你的书。您需要以不同的方式考虑您的项目,并检查您的图书的整体结构。

一些提示是:

  • 使用LaTeX或Markdown作为主要编辑语言
  • 考虑使用Pandoc等工具将您的作品转换为其他格式
  • 避免重复信息,不要重复(请查看SSOT
  • 让您的项目简单而愚蠢(KISS

答案 1 :(得分:2)

这是代码

newBook() {
    dir=$1
    N=$2

    mkdir $dir
    i=1
    while [ $i -le $N ]; do
        mkdir ${dir}/page-${i}
        touch ${dir}/page-${i}/head.html
        touch ${dir}/page-${i}/style.css
        touch ${dir}/page-${i}/body.html
        touch ${dir}/page-${i}/script.js
        mkdir ${dir}/page-${i}/images
        i=$(( $i +1 ))
    done
    mkdir ${dir}/trash
}

increasePage(){
    dir=$1
    newPage=$2
    if [ -d ${1}/page-${2} ]; then
        increasePage $1 $(( $2 + 1 ))
        mv ${1}/page-${2} ${1}/page-$(( $2 + 1 ))
    fi
}

CheckPage(){
    #dir=$1
    #newPage=$2
    if [ -d ${1}/page-${2} ]; then
        CheckPage $1 $(( $2 + 1 ))
        return $?
    else
        return $(( $2 - 1 ))
    fi
}

addPage() {
    #dir=$1
    #newPage=$2

    if [ -d ${1}/page-${2} ]; then
        increasePage $1 $2
    fi

    mkdir ${1}/page-${2}
    touch ${1}/page-${2}/head.html
    touch ${1}/page-${2}/style.css
    touch ${1}/page-${2}/body.html
    touch ${1}/page-${2}/script.js
    mkdir ${1}/page-${2}/images 
}

removePage(){
    dir=$1
    pageToRemove=$2

    if [ ! -d ${1}/page-${pageToRemove} ]; then
        echo "Page not Found: $2" >&2
        return
    fi

    mv ${dir}/page-${pageToRemove}/ ${dir}/trash/page-${pageToRemove}.`date +%Y%m%d%H%M%S`

    CheckPage $1 $(( $pageToRemove + 1 ))
    count=$?

    while [ $pageToRemove -lt $count ]; do
        mv ${dir}/page-$(( ${pageToRemove} + 1 ))/ ${dir}/page-${pageToRemove}
        pageToRemove=$(($pageToRemove + 1))
    done
}

这是测试执行

echo
echo "Creating Book"
newBook project 5

find project -name "*" | sort

echo
echo "placing some images for testing..."
echo touch project/page-1/images/Mercury.jpg | sh -v
echo touch project/page-2/images/Venus.jpg | sh -v
echo touch project/page-3/images/Earth.jpg | sh -v
echo touch project/page-4/images/Mars.jpg | sh -v
echo touch project/page-5/images/Jupiter.jpg | sh -v
find project -name "*" | sort

echo
echo "Adding page 3"
addPage project 3

find project -name "*" | sort

echo
echo "Adding page 7"
addPage project 7

find project -name "*" | sort

echo
echo "Removing page 2"
removePage project 2

find project -name "*" | sort

结果

Creating Book
project
project/page-1
project/page-1/body.html
project/page-1/head.html
project/page-1/images
project/page-1/script.js
project/page-1/style.css
project/page-2
project/page-2/body.html
project/page-2/head.html
project/page-2/images
project/page-2/script.js
project/page-2/style.css
project/page-3
project/page-3/body.html
project/page-3/head.html
project/page-3/images
project/page-3/script.js
project/page-3/style.css
project/page-4
project/page-4/body.html
project/page-4/head.html
project/page-4/images
project/page-4/script.js
project/page-4/style.css
project/page-5
project/page-5/body.html
project/page-5/head.html
project/page-5/images
project/page-5/script.js
project/page-5/style.css
project/trash

placing some images for testing...
touch project/page-1/images/Mercury.jpg
touch project/page-2/images/Venus.jpg
touch project/page-3/images/Earth.jpg
touch project/page-4/images/Mars.jpg
touch project/page-5/images/Jupiter.jpg

Adding page 3
project
project/page-1
project/page-1/body.html
project/page-1/head.html
project/page-1/images
project/page-1/images/Mercury.jpg
project/page-1/script.js
project/page-1/style.css
project/page-2
project/page-2/body.html
project/page-2/head.html
project/page-2/images
project/page-2/images/Venus.jpg
project/page-2/script.js
project/page-2/style.css
project/page-3
project/page-3/body.html
project/page-3/head.html
project/page-3/images
project/page-3/script.js
project/page-3/style.css
project/page-4
project/page-4/body.html
project/page-4/head.html
project/page-4/images
project/page-4/images/Earth.jpg
project/page-4/script.js
project/page-4/style.css
project/page-5
project/page-5/body.html
project/page-5/head.html
project/page-5/images
project/page-5/images/Mars.jpg
project/page-5/script.js
project/page-5/style.css
project/page-6
project/page-6/body.html
project/page-6/head.html
project/page-6/images
project/page-6/images/Jupiter.jpg
project/page-6/script.js
project/page-6/style.css
project/trash

Adding page 7
project
project/page-1
project/page-1/body.html
project/page-1/head.html
project/page-1/images
project/page-1/images/Mercury.jpg
project/page-1/script.js
project/page-1/style.css
project/page-2
project/page-2/body.html
project/page-2/head.html
project/page-2/images
project/page-2/images/Venus.jpg
project/page-2/script.js
project/page-2/style.css
project/page-3
project/page-3/body.html
project/page-3/head.html
project/page-3/images
project/page-3/script.js
project/page-3/style.css
project/page-4
project/page-4/body.html
project/page-4/head.html
project/page-4/images
project/page-4/images/Earth.jpg
project/page-4/script.js
project/page-4/style.css
project/page-5
project/page-5/body.html
project/page-5/head.html
project/page-5/images
project/page-5/images/Mars.jpg
project/page-5/script.js
project/page-5/style.css
project/page-6
project/page-6/body.html
project/page-6/head.html
project/page-6/images
project/page-6/images/Jupiter.jpg
project/page-6/script.js
project/page-6/style.css
project/page-7
project/page-7/body.html
project/page-7/head.html
project/page-7/images
project/page-7/script.js
project/page-7/style.css
project/trash

Removing page 2
project
project/page-1
project/page-1/body.html
project/page-1/head.html
project/page-1/images
project/page-1/images/Mercury.jpg
project/page-1/script.js
project/page-1/style.css
project/page-2
project/page-2/body.html
project/page-2/head.html
project/page-2/images
project/page-2/script.js
project/page-2/style.css
project/page-3
project/page-3/body.html
project/page-3/head.html
project/page-3/images
project/page-3/images/Earth.jpg
project/page-3/script.js
project/page-3/style.css
project/page-4
project/page-4/body.html
project/page-4/head.html
project/page-4/images
project/page-4/images/Mars.jpg
project/page-4/script.js
project/page-4/style.css
project/page-5
project/page-5/body.html
project/page-5/head.html
project/page-5/images
project/page-5/images/Jupiter.jpg
project/page-5/script.js
project/page-5/style.css
project/page-6
project/page-6/body.html
project/page-6/head.html
project/page-6/images
project/page-6/script.js
project/page-6/style.css
project/trash
project/trash/page-2.20150811190218
project/trash/page-2.20150811190218/body.html
project/trash/page-2.20150811190218/head.html
project/trash/page-2.20150811190218/images
project/trash/page-2.20150811190218/images/Venus.jpg
project/trash/page-2.20150811190218/script.js
project/trash/page-2.20150811190218/style.css
[opsc@snelnxh51 fabricio]$ rm -r project/
[opsc@snelnxh51 fabricio]$ ./teste.sh

Creating Book
project
project/page-1
project/page-1/body.html
project/page-1/head.html
project/page-1/images
project/page-1/script.js
project/page-1/style.css
project/page-2
project/page-2/body.html
project/page-2/head.html
project/page-2/images
project/page-2/script.js
project/page-2/style.css
project/page-3
project/page-3/body.html
project/page-3/head.html
project/page-3/images
project/page-3/script.js
project/page-3/style.css
project/page-4
project/page-4/body.html
project/page-4/head.html
project/page-4/images
project/page-4/script.js
project/page-4/style.css
project/page-5
project/page-5/body.html
project/page-5/head.html
project/page-5/images
project/page-5/script.js
project/page-5/style.css
project/trash

placing some images for testing...
touch project/page-1/images/Mercury.jpg
touch project/page-2/images/Venus.jpg
touch project/page-3/images/Earth.jpg
touch project/page-4/images/Mars.jpg
touch project/page-5/images/Jupiter.jpg
project
project/page-1
project/page-1/body.html
project/page-1/head.html
project/page-1/images
project/page-1/images/Mercury.jpg
project/page-1/script.js
project/page-1/style.css
project/page-2
project/page-2/body.html
project/page-2/head.html
project/page-2/images
project/page-2/images/Venus.jpg
project/page-2/script.js
project/page-2/style.css
project/page-3
project/page-3/body.html
project/page-3/head.html
project/page-3/images
project/page-3/images/Earth.jpg
project/page-3/script.js
project/page-3/style.css
project/page-4
project/page-4/body.html
project/page-4/head.html
project/page-4/images
project/page-4/images/Mars.jpg
project/page-4/script.js
project/page-4/style.css
project/page-5
project/page-5/body.html
project/page-5/head.html
project/page-5/images
project/page-5/images/Jupiter.jpg
project/page-5/script.js
project/page-5/style.css
project/trash

Adding page 3
project
project/page-1
project/page-1/body.html
project/page-1/head.html
project/page-1/images
project/page-1/images/Mercury.jpg
project/page-1/script.js
project/page-1/style.css
project/page-2
project/page-2/body.html
project/page-2/head.html
project/page-2/images
project/page-2/images/Venus.jpg
project/page-2/script.js
project/page-2/style.css
project/page-3
project/page-3/body.html
project/page-3/head.html
project/page-3/images
project/page-3/script.js
project/page-3/style.css
project/page-4
project/page-4/body.html
project/page-4/head.html
project/page-4/images
project/page-4/images/Earth.jpg
project/page-4/script.js
project/page-4/style.css
project/page-5
project/page-5/body.html
project/page-5/head.html
project/page-5/images
project/page-5/images/Mars.jpg
project/page-5/script.js
project/page-5/style.css
project/page-6
project/page-6/body.html
project/page-6/head.html
project/page-6/images
project/page-6/images/Jupiter.jpg
project/page-6/script.js
project/page-6/style.css
project/trash

Adding page 7
project
project/page-1
project/page-1/body.html
project/page-1/head.html
project/page-1/images
project/page-1/images/Mercury.jpg
project/page-1/script.js
project/page-1/style.css
project/page-2
project/page-2/body.html
project/page-2/head.html
project/page-2/images
project/page-2/images/Venus.jpg
project/page-2/script.js
project/page-2/style.css
project/page-3
project/page-3/body.html
project/page-3/head.html
project/page-3/images
project/page-3/script.js
project/page-3/style.css
project/page-4
project/page-4/body.html
project/page-4/head.html
project/page-4/images
project/page-4/images/Earth.jpg
project/page-4/script.js
project/page-4/style.css
project/page-5
project/page-5/body.html
project/page-5/head.html
project/page-5/images
project/page-5/images/Mars.jpg
project/page-5/script.js
project/page-5/style.css
project/page-6
project/page-6/body.html
project/page-6/head.html
project/page-6/images
project/page-6/images/Jupiter.jpg
project/page-6/script.js
project/page-6/style.css
project/page-7
project/page-7/body.html
project/page-7/head.html
project/page-7/images
project/page-7/script.js
project/page-7/style.css
project/trash

Removing page 2
project
project/page-1
project/page-1/body.html
project/page-1/head.html
project/page-1/images
project/page-1/images/Mercury.jpg
project/page-1/script.js
project/page-1/style.css
project/page-2
project/page-2/body.html
project/page-2/head.html
project/page-2/images
project/page-2/script.js
project/page-2/style.css
project/page-3
project/page-3/body.html
project/page-3/head.html
project/page-3/images
project/page-3/images/Earth.jpg
project/page-3/script.js
project/page-3/style.css
project/page-4
project/page-4/body.html
project/page-4/head.html
project/page-4/images
project/page-4/images/Mars.jpg
project/page-4/script.js
project/page-4/style.css
project/page-5
project/page-5/body.html
project/page-5/head.html
project/page-5/images
project/page-5/images/Jupiter.jpg
project/page-5/script.js
project/page-5/style.css
project/page-6
project/page-6/body.html
project/page-6/head.html
project/page-6/images
project/page-6/script.js
project/page-6/style.css
project/trash
project/trash/page-2.20150811190259
project/trash/page-2.20150811190259/body.html
project/trash/page-2.20150811190259/head.html
project/trash/page-2.20150811190259/images
project/trash/page-2.20150811190259/images/Venus.jpg
project/trash/page-2.20150811190259/script.js
project/trash/page-2.20150811190259/style.css

我希望它有所帮助。

答案 2 :(得分:1)

new_book() {
  mkdir -p "$1" && cd "$1" && touch README.md || return $?
  for dir in html head css js images cover; do
    p=1
    while [ "$p" -le "$2" ]; do
      mkdir -p "$dir/page-$p"
      case $dir in
        (html)   f="body.html" ;;
        (css)    f="style.css" ;;
        (*)      continue ;;
      esac
      touch "$dir/page-$p/$f"
      p=$((p+1))
    done
  done
  ls -l
}

这应该创建您请求的骨架结构。我建议只有一个css目录和一个@include从这个目录中提取样式,但是如果你有大量的每页定制,那么每页有一个是有意义的。

第一行创建目录(如果它尚未存在),进入其中,并创建README.md。任何失败都会阻止其余的功能运行。

之后,每个子目录上都有一个循环来创建它们,并且其中的每个页面(mkdir -p将创建缺少的父目录)。 case语句确保创建body.html或style.css,否则循环继续。

我假设您函数末尾的ll是对ls -l的调用,因此将其包含在此处。请注意,您将在新书的目录中完成。

答案 3 :(得分:0)

将您的图书视为网站并使用所有WEB正在使用的通用模式:

- Individual HTML static pages or backend
- /images/*.png
- /javascripts/*.js
- /styles/*.css

使用CSS选择器来区分页面中的各个样式。请不要让事情变得更加复杂。