<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>shell Archives - Creatronix</title>
	<atom:link href="https://creatronix.de/tag/shell/feed/" rel="self" type="application/rss+xml" />
	<link>https://creatronix.de/tag/shell/</link>
	<description>My adventures in code &#38; business</description>
	<lastBuildDate>Mon, 22 Apr 2024 07:41:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>How I learned to love the Bash</title>
		<link>https://creatronix.de/how-i-learned-to-love-the-bash/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Fri, 18 Jun 2021 05:39:57 +0000</pubDate>
				<category><![CDATA[DevOps & Automation]]></category>
		<category><![CDATA[arguments]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[conditions]]></category>
		<category><![CDATA[loops]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[variables]]></category>
		<guid isPermaLink="false">https://creatronix.de/?p=3637</guid>

					<description><![CDATA[<p>My tale of woe When I started to write my first ebook I decided to use pandoc to transform markdown into pdf. It basically looked like this pandoc metadata_de.yaml -o ./level_up_de.pdf --from markdown -V lang=de-DE level_up_de.md This simple command can be pasted into the command line everytime I want to build a new version of&#8230;</p>
<p>The post <a href="https://creatronix.de/how-i-learned-to-love-the-bash/">How I learned to love the Bash</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h2>My tale of woe</h2>
<p>When I started to write my first ebook I decided to use pandoc to transform markdown into pdf.<br />
It basically looked like this</p>
<pre>pandoc metadata_de.yaml -o ./level_up_de.pdf --from markdown -V lang=de-DE level_up_de.md</pre>
<p>This simple command can be pasted into the command line everytime I want to build a new version of the ebook.<br />
But: I have to keep in mind the command which is tedious. I could write it into a text file and copy it everytime I use it.<br />
There is a more elegant way: let&#8217;s make a script!</p>
<h2>Make a script</h2>
<p>A shell script is basically a file with the extension &#8220;.sh&#8221;<br />
When You want to specify which shell shall be used, you put</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>#!/bin/bash</code></pre>
</div>
<p>in the first line of your script.</p>
<h2>Echo</h2>
<p>To put some text on the command line for e.g. displaying the start of your script, you can use the command echo like this</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>echo "Start generating PDF"</code></pre>
</div>
<p>The script now looks like that</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>#!/bin/bash 
echo "Start generating PDF" 
pandoc metadata_de.yaml -o ./level_up_de.pdf --from markdown -V lang=de-DE level_up_de.md</code></pre>
</div>
<h2>Variables</h2>
<p>After finishing the German version I also wanted to create an English version,<br />
so the &#8220;de&#8221; strings should be replaced by a variable to switch between languages.</p>
<p>To declare a variable, you write down the name and assign it a value with the equal sign.<br />
You use a variable by using thew $ sign to access its content.</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>language="en" 
echo "Language: $language"</code></pre>
</div>
<p><strong>Caveat: whitespaces are not allowed around the equal sign!</strong></p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>language = "en" # this won't work</code></pre>
</div>
<p>Let&#8217;s have a look at our script now:</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>#!/bin/bash 
echo "Start generating PDF" 
lang="en" echo "Language: $lang" 
pandoc metadata_$lang.yaml -o ./level_up_$lang.pdf --from markdown -V lang=de-DE level_up_$lang.md</code></pre>
</div>
<p>Pretty neat!</p>
<p>But we need to get rid of the &#8220;de-DE&#8221; as well</p>
<h2>Conditions</h2>
<p>When we get &#8220;en&#8221; as language we want to use en-US as language subtag. When &#8220;de&#8221; is used we want de-DE<br />
Let&#8217;s write some conditions.</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>if [[ $lang = "en" ]] 
  then lang_subtag="$lang-US" 
elif [[ $lang = "de" ]] 
  then lang_subtag="$lang-DE" 
else 
  echo "Wrong language" 
fi</code></pre>
</div>
<p>Now we can replace the de-DE with $lang_subtag</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>pandoc metadata_$lang.yaml -o ./level_up_$lang.pdf --from markdown -V lang=$lang_subtag level_up_$lang.md</code></pre>
</div>
<h2>Command Line Arguments</h2>
<p>Now that we can generate the ebook for english and german we could introduce a command line parameter.<br />
In bash every argument provided to the script is stored in variables $1 .. $n</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>lang="$1"</code></pre>
</div>
<h2>Arrays and loops</h2>
<p>I wanted to break down the big markdown file into individual chapters.<br />
To make an array variable in bash you can do the following:</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>chapters=( 
  ./de/chapter_01.txt 
  ./de/chapter_02.txt 
  ./de/chapter_03.txt 
)</code></pre>
</div>
<p>Looping over an array looks like this:</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>for chapter in ${chapters[*]} 
do 
  cat "$chapter" printf "\n" 
done</code></pre>
</div>
<p>So now we can pass the chapters array to pandoc</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>pandoc metadata_$lang.yaml -o ./level_up_$lang.pdf --from markdown -V lang=$lang_subtag ${chapters[*]}</code></pre>
</div>
<p>To be continued &#8230;</p>
<p>You can find the source code here: <a href="https://github.com/jboegeholz/shell_tutorial">https://github.com/jboegeholz/shell_tutorial</a></p>
<p>The post <a href="https://creatronix.de/how-i-learned-to-love-the-bash/">How I learned to love the Bash</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
