簡體   English   中英

我希望我的shell腳本生成一個包含輸出的文件

[英]i want my shell script to generate a file with the output in it

因此,基本上,而不是一遍又一遍地為我正在構建的網站編寫html代碼(安裝頁面,因此我需要為我提供的每個應用下載一個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編寫在特定目錄中創建文件(我要命名),並在其中輸出

鑒於您想將字符串鏟入文件。 您可以通過兩種不同的方式進行操作。

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

# 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

您是否將文件名作為第8個參數傳遞。 第0個參數是包含腳本的腳本文件名。 正如@Fazlin指出的那樣,顯示您正在使用的整個命令行將告訴您是否缺少某些內容。

如果以上腳本包含在一個名為GenerateHtml.sh的文件中,則您的命令行應類似於:

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

這里[output]是html文件的名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM