Podx Nachorrx Podx Nachorrx - 4 months ago 17
Bash Question

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

so basically, instead of writing over and over again the html code for a website i am building (install page so i need one for each app i offer to download)so i made this script. i want the output to be in a file generated so, on the console it asks what should i name the file (e.g: hi.html) and the output will be in that file in an specific directory on my 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


So i dont know how to write in bash to create a file( which i want to name) in an specific directory with the output in it

Answer

Given you want to shovel a string into a file. You can do so in two different ways.

The first way to do this would be with a string:

# 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

The second way would be using an argument you pass to the script:

# 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

ie. bash myscript.sh foo.html

Comments