228 lines
12 KiB
HTML
228 lines
12 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en-US">
|
||
<head>
|
||
<meta charset="utf-8"/>
|
||
<meta content="#0e1112" media="(prefers-color-scheme: dark)" name="theme-color"/>
|
||
<meta content="#566B78" media="(prefers-color-scheme: light)" name="theme-color"/>
|
||
<meta content="Michael Housh" name="author"/>
|
||
<meta content="Mhoush" name="apple-mobile-web-app-title"/>
|
||
<meta content="initial-scale=1.0, width=device-width" name="viewport"/>
|
||
<meta content="telephone=no" name="format-detection"/>
|
||
<meta content="True" name="HandheldFriendly"/>
|
||
<meta content="320" name="MobileOptimized"/>
|
||
<meta content="Mhoush" name="og:site_name"/>
|
||
<meta content="hvac, developer, swift, home-performance, design" name="keywords"/>
|
||
<title>
|
||
mhoush: Introduction to Programming for HVAC Part-3
|
||
</title>
|
||
<link href="/static/favicon.ico" rel="shortcut icon"/>
|
||
<link href="/static/output.css" rel="stylesheet"/>
|
||
<link href="/static/style.css" rel="stylesheet"/>
|
||
<link href="/articles/feed.xml" rel="alternate" title="mhoush" type="application/rss+xml"/>
|
||
<link href="/static/prism.css" rel="stylesheet"/>
|
||
<meta content="In this article we will put together some of the pieces from the last 2 articles, and build our first program. If you have missed the first
|
||
articles, then you can catch up here before continuing with this article.
|
||
Getting Started
|
||
We are going to make..." name="description"/>
|
||
<meta content="summary_large_image" name="twitter:card"/>
|
||
<meta content="http://localhost:3000/articles/images/2023-09-24-introduction-to-programming-for-hvac-3.png" name="twitter:image"/>
|
||
<meta content="Introduction to Programming for HVAC Part-3" name="twitter:image:alt"/>
|
||
<meta content="http://localhost:3000/articles/images//articles/2023/introduction-to-programming-for-hvac-3/" name="og:url"/>
|
||
<meta content="Introduction to Programming for HVAC Part-3" name="og:title"/>
|
||
<meta content="In this article we will put together some of the pieces from the last 2 articles, and build our first program. If you have missed the first
|
||
articles, then you can catch up here before continuing with this article.
|
||
Getting Started
|
||
We are going to make..." name="og:description"/>
|
||
<meta content="http://localhost:3000/articles/images/2023-09-24-introduction-to-programming-for-hvac-3.png" name="og:image"/>
|
||
<meta content="1014" name="og:image:width"/>
|
||
<meta content="530" name="og:image:height"/>
|
||
<script crossorigin="anonymous" src="https://kit.fontawesome.com/f209982030.js">
|
||
</script>
|
||
</head>
|
||
<body class="bg-page text-white pb-5 font-avenir articles">
|
||
<header class="bg-nav text-gray py-4 text-base/6 lg:fixed w-full lg:h-[62px]">
|
||
<nav class="container flex gap-x-5 lg:gap-x-y items-center">
|
||
<ul class="flex flex-wrap gap-x-2 lg:gap-x-5">
|
||
<li>
|
||
<a class href="/">Home</a>
|
||
</li>
|
||
<li>
|
||
<a class="active" href="/articles/">Articles</a>
|
||
</li>
|
||
<li>
|
||
<a class href="/about/">About</a>
|
||
</li>
|
||
</ul>
|
||
</nav>
|
||
</header>
|
||
<div class="container pt-12 lg:pt-28">
|
||
<article class="prose">
|
||
<h1>
|
||
Introduction to Programming for HVAC Part-3
|
||
</h1>
|
||
<div class="-mt-6">
|
||
<div class="text-gray gray-links text-sm">
|
||
<span class="border-r border-gray pr-2 mr-2">September 24, 2023</span>742 words, posted in <a href="/articles/tag/hvac/">HVAC</a>, <a href="/articles/tag/programming/">programming</a> and <a href="/articles/tag/software/">software</a>
|
||
</div>
|
||
</div>
|
||
<img alt="banner" src="http://localhost:3000/articles/images/2023-09-24-introduction-to-programming-for-hvac-3.png"/>
|
||
<p>In this article we will put together some of the pieces from the last 2 articles, and build our first program. If you have missed the first
|
||
articles, then you can catch up <a href="https://mhoush.com/series/programming-for-hvac/">here</a> before continuing with this article.</p>
|
||
<h2>Getting Started</h2>
|
||
<p>We are going to make our first script / program. This first program is really just setting up some building blocks for our next program we
|
||
will write, that will remove the background from an image.</p>
|
||
<h3>Creating a scripts directory</h3>
|
||
<p>We learned in the <a href="https://mhoush.com/posts/introduction-to-programming-for-hvac-1/">first article</a> how to use our terminal. Today we are
|
||
going to use some of the commands we learned to create a directory where we can store our script and future scripts that we write.</p>
|
||
<p><strong>Create a directory</strong></p>
|
||
<pre><code class="language-bash">mkdir -p ~/.local/bin
|
||
</code></pre>
|
||
<p>The above command will create a “hidden” directory in your home folder. We can go ahead and move into the directory we just created.</p>
|
||
<blockquote>
|
||
<p><strong>Note:</strong> The <code>-p</code> option allows us to create nested directories if the parent directory doesn’t exist.</p>
|
||
</blockquote>
|
||
<pre><code class="language-bash">cd ~/.local/bin
|
||
</code></pre>
|
||
<h3>Hello World</h3>
|
||
<p>It is common in programming to start out with a “Hello World” program when learning a new scripting paradigm. So let’s jump in and get
|
||
started.</p>
|
||
<p><strong>Creating our script file:</strong></p>
|
||
<pre><code class="language-bash">touch hello-world.sh
|
||
</code></pre>
|
||
<p><strong>Now open the file:</strong></p>
|
||
<pre><code class="language-bash">open hello-world.sh
|
||
</code></pre>
|
||
<p>The above command should open the file in the <code>TextEdit</code> application. In order to make the text edit application to not auto-capitalize
|
||
words and play more nicely, we need to adjust some settings. Open the settings by pressing <code>⌘,</code>.</p>
|
||
<p>In the <strong>Format</strong> section, select <em>Plain text</em> and in the <strong>Options</strong> section de-select <em>Check spelling as you type</em>.</p>
|
||
<p><img src="/articles/images/2023-09-24-text-settings.png" alt="text-settings" /></p>
|
||
<p>At this point for changes to take place, you will need to close the file and re-open.</p>
|
||
<blockquote>
|
||
<p><strong>Tip:</strong> In your terminal you can run the last command in your history by using the <code>↑</code> (Up) arrow key.</p>
|
||
</blockquote>
|
||
<p>Now that the file is open again, we will write our hello-world program. The contents of your file should look like the following:</p>
|
||
<pre><code class="language-bash">#!/bin/sh
|
||
|
||
echo 'Hello World!'
|
||
</code></pre>
|
||
<p>The first line is referred to as the <code>shebang</code>, this tells your computer which shell interperter to run your file. I have not explained the
|
||
shell yet, but it currently would just muddy the waters a bit, but there are several shell interperters on your computer with the <code>sh</code> posix
|
||
shell being one of the most universal / lowest level ones, which is why I’m choosing this one (in other words this script would work on just
|
||
about any machine you were on).</p>
|
||
<p>The second line we are using the built-in <code>echo</code> command and passing it the ‘Hello World!’ argument.</p>
|
||
<p>Now save and close the file <code>⌘s</code> (to save) <code>⌘q</code> (to quit the text edit application).</p>
|
||
<p><strong>Run the program from your terminal:</strong></p>
|
||
<pre><code class="language-bash">/bin/sh hello-world.sh
|
||
</code></pre>
|
||
<p>You should see that <code>Hello World!</code> was printed to your console.</p>
|
||
<p><img src="/articles/images/2023-09-24-hello-output.png" alt="hello-output" /></p>
|
||
<h3>Make Executable</h3>
|
||
<p>Now that we have our basic script working, let’s make it an executable.</p>
|
||
<p><strong>In your terminal, type the following:</strong></p>
|
||
<pre><code class="language-bash">chmod u+x hello-world.sh
|
||
</code></pre>
|
||
<p>This will change the mode of the file type to be an executable.</p>
|
||
<p>Now move / rename the file so we don’t have to call it using <code>.sh</code> extension:</p>
|
||
<pre><code class="language-bash">mv hello-world.sh hello-world
|
||
</code></pre>
|
||
<p>Now that the file is executable, we can execute it by just calling the name of the file.</p>
|
||
<pre><code class="language-bash">./hello-world
|
||
</code></pre>
|
||
<blockquote>
|
||
<p><strong>Note:</strong> We have to prefix the file name with <code>./</code> in the above command so that it knows where to find our file. The <code>./</code> is saying run
|
||
this file in our current directory. In the future we will setup our shell so that it knows to look in our <code>~/.local/bin</code> directory for
|
||
scripts, so that we can call them without this prefix.</p>
|
||
</blockquote>
|
||
<h2>Conclusion</h2>
|
||
<p>Congratulations, in this article we wrote our first program. We learned how to edit the file, set it’s permissions, and execute the program
|
||
from our terminal. I should mention that the <code>TextEdit</code> application is generally not how you would program, people typically use what is
|
||
known as an <code>IDE (integrated development environment)</code>, however I chose to use the <code>TextEdit</code> application because it is built-in to <code>macOS</code>
|
||
and allowed us to accomplish our goal without downloading other software.</p>
|
||
<p>In our upcoming articles, we will write a program that I hope is useful to you / something that you can build upon and use for a long time.
|
||
Thank you for reading to this point.</p>
|
||
</article>
|
||
<div class="border-t border-light mt-8 pt-8">
|
||
<h2 class="text-4xl font-extrabold mb-8">
|
||
Written by
|
||
</h2>
|
||
<div class="flex flex-col lg:flex-row gap-8">
|
||
<div class="flex-[0_0_120px]">
|
||
<img class="w-[120px] h-[120px] rounded-full" src="/static/images/avatar.png"/>
|
||
</div>
|
||
<div class="prose">
|
||
<h3 class="!m-0">
|
||
Michael Housh
|
||
</h3>
|
||
<p class="text-gray">
|
||
HVAC business owner with over 27 years of experience. Writes articles about HVAC,
|
||
Programming, Home-Performance, and Building Science
|
||
</p>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="mt-16">
|
||
<h2 class="text-4xl font-extrabold mb-8">
|
||
More articles
|
||
</h2>
|
||
<div class="grid lg:grid-cols-2 gap-10">
|
||
<section>
|
||
<h2 class="text-2xl font-bold mb-2">
|
||
<a class="[&:hover]:border-b border-orange" href="/articles/2025/vapor-htmx-todo-app/">Vapor + HTMX</a>
|
||
</h2>
|
||
<div class="text-gray gray-links text-sm mb-4">
|
||
<span class="border-r border-gray pr-2 mr-2">January 05, 2025</span><a href="/articles/tag/general/">general</a>, <a href="/articles/tag/programming/">programming</a> and <a href="/articles/tag/software/">software</a>
|
||
</div>
|
||
<p>
|
||
<a href="/articles/2025/vapor-htmx-todo-app/"><div>
|
||
Build an example application using Vapor and HTMX.
|
||
</div></a>
|
||
</p>
|
||
</section>
|
||
<section>
|
||
<h2 class="text-2xl font-bold mb-2">
|
||
<a class="[&:hover]:border-b border-orange" href="/articles/2024/free-as-in-freedom/">Free As In Freedom</a>
|
||
</h2>
|
||
<div class="text-gray gray-links text-sm mb-4">
|
||
<span class="border-r border-gray pr-2 mr-2">April 09, 2024</span><a href="/articles/tag/general/">general</a>, <a href="/articles/tag/open-source/">open-source</a> and <a href="/articles/tag/software/">software</a>
|
||
</div>
|
||
<p>
|
||
<a href="/articles/2024/free-as-in-freedom/"><div>
|
||
Salute to open-source software engineers
|
||
</div></a>
|
||
</p>
|
||
</section>
|
||
</div>
|
||
<p class="prose mt-8">
|
||
<a href="/articles/">› See all articles</a>
|
||
</p>
|
||
</div>
|
||
</div>
|
||
<div class="site-footer container text-gray gray-links border-t border-light text-center pt-6 mt-8 text-sm">
|
||
<p>
|
||
Copyright © Michael Housh 2023-2025.
|
||
</p>
|
||
<p>
|
||
Built in Swift using
|
||
<a href="https://github.com/loopwerk/Saga" rel="nofollow" target="_blank">Saga</a>
|
||
(<a href="https://github.com/m-housh/mhoush.com" rel="nofollow" target="_blank">source</a>).
|
||
</p>
|
||
<p>
|
||
<a href="http://localhost:3000/articles/feed.xml" rel="nofollow" target="_blank">RSS</a>
|
||
|
|
||
<a href="https://github.com/m-housh" rel="nofollow" target="_blank">Github</a>
|
||
|
|
||
<a href="https://www.youtube.com/channel/UCb58SeURd5bObfTiL0KoliA" rel="nofollow" target="_blank">Youtube</a>
|
||
|
|
||
<a href="https://www.facebook.com/michael.housh" rel="nofollow" target="_blank">Facebook</a>
|
||
|
|
||
<a href="mailto:michael@mhoush.com" rel="nofollow">Email</a>
|
||
</p>
|
||
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-core.min.js">
|
||
</script>
|
||
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/keep-markup/prism-keep-markup.min.js">
|
||
</script>
|
||
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/autoloader/prism-autoloader.min.js">
|
||
</script>
|
||
</div>
|
||
</body>
|
||
</html> |