我希望我的shell脚本生成一个包含输出的文件

时间:2016-07-19 12:24:23

标签: html bash macos shell

所以基本上,而不是一遍又一遍地写我正在构建的网站的html代码(安装页面,所以我需要为我提供的每个应用程序下载一个)所以我制作了这个脚本。我希望输出在生成的文件中,因此,在控制台上它会询问我应该为文件命名的内容(例如:hi.html),输出将在我mac上特定目录中的该文件中。

# prompt for input.
APP=$1
if [ -z "${APP}" ]
then
    read -p "App: " APP
fi

REPORT=$2
if [ -z "${REPORT}" ]
then
    read -p "Report App: " REPORT
fi

APPICON=$3
if [ -z "${APPICON}" ]
then
    read -p "AppIcon: " APPICON
fi

PLISTURL=$4
if [ -z "${PLISTURL}" ]
then
    read -p "plist URL: " PLISTURL
fi

DESCRIPTION=$5
if [ -z "${DESCRIPTION}" ]
then
    read -p "Description: " DESCRIPTION
fi

VERSION=$6
if [ -z "${VERSION}" ]
then
    read -p "Version: " VERSION
fi

DEV=$7
if [ -z "${DEV}" ]
then
    read -p "Developer: " DEV
fi
# Generate the P-List
read -r -d '' HTML << EndOfHTML
================================================================================

<!DOCTYPE html>
<html>
  <head>
    <!-- Required meta tags-->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <!-- Your app title -->
    <title>My App</title>
    <!-- Path to Framework7 iOS CSS theme styles-->
    <link rel="stylesheet" href="css/framework7.ios.min.css">
    <!-- Path to Framework7 iOS related color styles -->
    <link rel="stylesheet" href="css/framework7.ios.colors.min.css">
    <!-- Path to your custom app styles-->
    <link rel="stylesheet" href="css/my-app.css">
  </head>
  <body>
    <!-- Status bar overlay for full screen mode (PhoneGap) -->
    <div class="statusbar-overlay"></div>

    <!-- Views -->
    <div class="views">
      <!-- Your main view, should have "view-main" class -->
      <div class="view view-main">
        <!-- Top Navbar-->
        <div class="navbar">
  <div class="navbar-inner">
    <div class="left"><a href="index.html" class="back link"> <i class="icon icon-back"></i><span>Back</span></a></div>
    <div class="center sliding">$APP</div>
    <div class="right"><a href="https://twitter.com/intent/tweet?original_referer=https%3A%2F%2Fabout.twitter.com%2Fes%2Fresources%2Fbuttons&ref_src=twsrc%5Etfw&related=ItsNash0&screen_name=TweakBoxApp&text=$REPORT%20does%20not%20work!&tw_p=tweetbutton" class="link external">Report App</a></div>
  </div>
</div>



        <!-- Pages container, because we use fixed-through navbar and toolbar, it has additional appropriate classes-->
        <div class="pages navbar-through toolbar-through">
          <!-- Page, "data-page" contains page name -->
          <div data-page="index" class="page">
            <!-- Scrollable page content -->
            <div class="page-content">
              <!-- Content Here-->
     <img src="appicons/$APPICON" class="appinstallicon"/>

     <a class="button button-big button-round active installbutton link external" href="itms-services://?action=download-manifest&url=$PLISTURL"><b>1. Install App!</b></a>

     <a class="button button-big button-round active installbutton link external" href="prefs:root=General&path=DATE_AND_TIME"><b>2. Change Date!</b></a>

     <div class="content-block-title">Description</div>
        <div class="content-block tablet-inset">
      <div class="content-block-inner">
        <p>$DESCRIPTION</p>
        <ul>
        <li>Name: $APP</li>
        <li>Version: $VERSION</li>
        <li>Developer: $DEV</li>
        </ul>
      </div>
    </div>


            <!-- Content Here-->
            </div>
          </div>
        </div>
        <!-- Bottom Toolbar-->

            <!-- Toolbar links -->

    <!-- Path to Framework7 Library JS-->
    <script type="text/javascript" src="js/framework7.min.js"></script>
    <!-- Path to your app js-->
    <script type="text/javascript" src="js/my-app.js"></script>
  </body>
</html>
================================================================================

EndOfHTML

# WHAT TO DO WITH THE OUTPUT.
OUTPUT=$8
if [ -z "${OUTPUT}" ]
then
    echo "$HTML"
else
    echo "$HTML" >> $OUTPUT
fi

所以我不知道怎么写bash来创建一个文件(我想命名)在一个特定的目录中,其中有输出

2 个答案:

答案 0 :(得分:0)

鉴于你想将字符串铲成文件。您可以通过两种不同的方式完成此操作。

第一种方法是使用字符串:

# Here we put the filename which is a string into the variable $FILENAME.
$FILENAME = "foo.html"

# Given that you have built your HTML string and 
# put it into a variable called $HTML.
# We can then redirect the output of echoing 
# our $HTML variable to our $FILENAME variable.
echo $HTML >> $FILENAME

第二种方法是使用传递给脚本的参数:

# Here $1 contains the first argument you can pass when running a bash script.
# For example if you ran `bash myscript.sh foo.html` $1 would then contain
# a string "foo.html".
# This will allow you to dynamically generate the file name without having 
# to edit your script.
$FILENAME = $1
echo $HTML >> $FILENAME

即。 bash myscript.sh foo.html

答案 1 :(得分:0)

您是否将文件名作为第8个参数传递。第0个参数是包含脚本的脚本文件名。正如@Fazlin指出的那样,显示您正在使用的整个命令行会告诉您是否遗漏了某些内容。

如果上述脚本包含在名为GenerateHtml.sh的文件中,则命令行应如下所示:

GenerateHtml.sh "app" "report" "appicon" "plisturl" "description" "version" "dev" "[output].htm"

这里[output]是html文件的名称。