<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jaro</title>
    <description>The latest articles on DEV Community by Jaro (@mnjaro).</description>
    <link>https://dev.to/mnjaro</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F248732%2F92479abf-675d-41d4-87d5-cee9fca53ec3.png</url>
      <title>DEV Community: Jaro</title>
      <link>https://dev.to/mnjaro</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mnjaro"/>
    <language>en</language>
    <item>
      <title>Advanced bash scripting with awk</title>
      <dc:creator>Jaro</dc:creator>
      <pubDate>Mon, 03 Jan 2022 13:38:36 +0000</pubDate>
      <link>https://dev.to/mnjaro/advanced-bash-scripting-with-awk-56kg</link>
      <guid>https://dev.to/mnjaro/advanced-bash-scripting-with-awk-56kg</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;AWK is an interpreted programming language. It is very powerful and specially designed for text processing. Its name is derived from the family names of its authors − Alfred Aho, Peter Weinberger, and Brian Kernighan.&lt;br&gt;
&lt;a href="https://www.tutorialspoint.com/awk/awk_overview.htm"&gt;https://www.tutorialspoint.com/awk/awk_overview.htm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The version of AWK that GNU/Linux distributes is written and maintained by the Free Software Foundation (FSF); it is often referred to as GNU AWK.&lt;/p&gt;

&lt;p&gt;AWK is a very powerful tool for&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Text processing&lt;/li&gt;
&lt;li&gt;Producing formatted text reports&lt;/li&gt;
&lt;li&gt;Performing arithmetic operations&lt;/li&gt;
&lt;li&gt;Performing string operations, and many more&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;AWK breaks each line of input into fields where a field is a string of characters separated by a whitespace. Don't worry though, this is also a configurable parameter. You can actually also manage what delimiter you want. AWK has its specific strength when having to handle structured text files, tables, or chunk structured data. &lt;br&gt;
First of all, let's start with a simple example where we format the output of a string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo &lt;/span&gt;tomatoe potatoe | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print $1}'&lt;/span&gt;
&lt;span class="c"&gt;# tomatoe &lt;/span&gt;

&lt;span class="nb"&gt;echo &lt;/span&gt;tomatoe potatoe | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print $2}'&lt;/span&gt;
&lt;span class="c"&gt;# potatoe&lt;/span&gt;

&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print $3}'&lt;/span&gt; &lt;span class="nv"&gt;$filename&lt;/span&gt;
&lt;span class="c"&gt;# Prints field #3 of file $filename to stdout.&lt;/span&gt;

&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print $1 $5 $6}'&lt;/span&gt; &lt;span class="nv"&gt;$filename&lt;/span&gt;
&lt;span class="c"&gt;# Prints fields #1, #5, and #6 of file $filename.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is one of the core features of AWK, the print command. Now we just played with orders in how matches occur in the string. What is actually possible with AWK is manipulating patterns. You could for exemple print a line of a file where the pattern you requested matched. Or for example print all the possible http error code 500 in a web server log.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'$9 == 500 { print $0}'&lt;/span&gt; /var/log/httpd/access.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course, here the &lt;code&gt;$9&lt;/code&gt; refers to the position of where the error code appears.&lt;br&gt;
The part actually outside of the curly brackets represents the pattern we are looking for and the part inside represents the action to be executed by AWK. You can use all of the possible comparison operators that are also included in C language such as : ==, !=, &amp;lt;, &amp;gt;, &amp;lt;=, &amp;gt;=, ?:&lt;/p&gt;

&lt;p&gt;If you do not pass any pattern the action applies to all lines. If no action is given, the entire line gets printed. &lt;/p&gt;

&lt;p&gt;AWK is actually a programming language which means it can also perform arithmetic operations. &lt;br&gt;
Let's say we have a file with a column of numbers and we want to get the sum of all the numbers in that column. Keep in mind this column is simply placed at position 1 of that file such as it resembles to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
2
3
4
5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to gather the sum we use operator brackets such as :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{total += $1} END {print total}'&lt;/span&gt; file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens here is kind of like a loop, where awk gets all the first position words on each line, declares at first a variable called total, sums those words or here numbers into that variable and finally when this operation is over, prints that variable.&lt;/p&gt;

&lt;p&gt;Now let's try to use all of that into some more useful stuff such as getting your CPU temperatures from the sensors utility and stripping out the "+" and "°C" from it.&lt;br&gt;
Getting the temperature is usually done by typing &lt;code&gt;sensors&lt;/code&gt; but this might vary depending on what distro/OS you are using.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;sensors
dell_smm-virtual-0
Adapter: Virtual device
Processor Fan: 2706 RPM
CPU:            +44.0°C  
Ambient:        +37.0°C  
SODIMM:         +36.0°C  

&lt;span class="nv"&gt;$ &lt;/span&gt;sensors | &lt;span class="nb"&gt;grep &lt;/span&gt;CPU
CPU:            +44.0°C  

&lt;span class="nv"&gt;$ &lt;/span&gt;sensors | &lt;span class="nb"&gt;grep &lt;/span&gt;CPU | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{printf "%d\n", $2}'&lt;/span&gt;
44
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's try to do some more programming language logic and try to implement our if-else logic. Below is an example that pipes only the data and then it changes a column value to a string based on a condition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;pi_data.txt
&lt;span class="nb"&gt;time &lt;/span&gt;temp wave&lt;span class="o"&gt;(&lt;/span&gt;ft&lt;span class="o"&gt;)&lt;/span&gt; comments
&lt;span class="nt"&gt;----&lt;/span&gt; &lt;span class="nt"&gt;----&lt;/span&gt; &lt;span class="nt"&gt;--------&lt;/span&gt; &lt;span class="nt"&gt;--------&lt;/span&gt;
10:00 24   3       No wind
12:00 26   5       High winds
14:00 25   4       wind calming down

&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="c"&gt;# Show time and small or medium for wave size&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;pi_data.txt | &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;   &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{if ( $1 ~ /[0-9]/ ) print $0'&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt; | &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;   &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{if ($3 &amp;lt; 4) {print $1 "\t small"} else { print $1 "\t medium"} }'&lt;/span&gt;
10:00    small
12:00    medium
14:00    medium
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A single AWK command to adjust the title and then change the data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;pi_data.txt | &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;   &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{if ( $1 ~ /[0-9]/ ) \
&amp;gt;            { \
&amp;gt;               {if ($3 &amp;lt; 4) {print $1 "\t small"} else { print $1 "\t medium"} } \
&amp;gt;        } else { print $1 "\t " $3} \
&amp;gt;        }'&lt;/span&gt;
&lt;span class="nb"&gt;time     &lt;/span&gt;wave&lt;span class="o"&gt;(&lt;/span&gt;ft&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nt"&gt;----&lt;/span&gt;     &lt;span class="nt"&gt;--------&lt;/span&gt;
10:00    small
12:00    medium
14:00    medium
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And finally and more importantly, killing tasks, one of the most useful tool in existence.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="c"&gt;#&lt;/span&gt;
&lt;span class="c"&gt;# stop_task.sh - stop a task&lt;/span&gt;
&lt;span class="c"&gt;#&lt;/span&gt;
&lt;span class="nv"&gt;task1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"edublocks"&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Stopping &lt;/span&gt;&lt;span class="nv"&gt;$task1&lt;/span&gt;&lt;span class="s2"&gt;..."&lt;/span&gt;
ps &lt;span class="nt"&gt;-e&lt;/span&gt; | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-E&lt;/span&gt; &lt;span class="nv"&gt;$task1&lt;/span&gt; | &lt;span class="se"&gt;\&lt;/span&gt;
 &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print $1}'&lt;/span&gt; | xargs &lt;span class="nb"&gt;sudo kill&lt;/span&gt; &lt;span class="nt"&gt;-9&lt;/span&gt; 1&amp;gt;&amp;amp;-
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Final comments
&lt;/h2&gt;

&lt;p&gt;I’ve found that learning a little bit of AWK has really paid off.&lt;/p&gt;

&lt;p&gt;AWK supports a lot of functionality and it can be used to create full on scripting applications with user inputs, file I/O, math functions and shell commands, but despite all this I’ll stick to Python if things get complex.&lt;/p&gt;

</description>
      <category>bash</category>
      <category>awk</category>
      <category>tutorial</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Bash scripting introduction</title>
      <dc:creator>Jaro</dc:creator>
      <pubDate>Mon, 20 Dec 2021 13:22:00 +0000</pubDate>
      <link>https://dev.to/mnjaro/bash-scripting-introduction-1dfb</link>
      <guid>https://dev.to/mnjaro/bash-scripting-introduction-1dfb</guid>
      <description>&lt;p&gt;Bash scripting is a powerful tool. If you have been working on a UNIX based system, after quite some time, you end up working with it because you just realize that it exists for simplicity, efficiency and scalability. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;One program's output shall be another one's input. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That sums up how things are done and how you should think them too. Now before wanting to do anything fancy with bash scripting, there are already a lot of tools that boost your productivity and that I realized I was only using 10% of the actual power.&lt;br&gt;
The aim of that article is to go through some of them, view some very useful tricks and hopefully, interest you in going further into that topic.&lt;/p&gt;
&lt;h2&gt;
  
  
  Brace expansion
&lt;/h2&gt;

&lt;p&gt;Brace expansion is a very useful tool to manipulate lists of "things". It is used in most cases to generate lists, files with redundant naming(s) etc... This tool allows you to save time and avoid mistakes because you do not need to type that much to generate those lists. &lt;br&gt;
Something worth noting about bash scripts is the fact that before the bash shell actually executes a command it will check whether it needs to perform any substitutions on the command. You probably used that with variables, or aliases before. Brace expansion is one of the multiple expansion forms that are supported by bash. Be wary, those expansions are not available in old shells. But most of current distros will allow you to use them.&lt;br&gt;
Brace expansions are contained between a pair of &lt;code&gt;{}&lt;/code&gt; braces. Inside of those will be comma-separated items or a range specifier.&lt;br&gt;
The most common example could be done like that :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;1,2,3,4,5&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will output the values within the brackets. Do you get where this is going and how you can use that? Not yet? Look at this other example with a range this time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Will output all numbers from 1 to 10&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;1..10&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="c"&gt;# Will output all characters from A to Z and a to z&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;A..z&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now imagine you had the need to create 10 files with the same name but with a specific delimiter&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;touch &lt;/span&gt;filename&lt;span class="o"&gt;{&lt;/span&gt;1..10&lt;span class="o"&gt;}&lt;/span&gt;.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You are now able to create 10 files in one single command line.&lt;/p&gt;

&lt;h2&gt;
  
  
  GREP
&lt;/h2&gt;

&lt;p&gt;There is a handy tool called grep which lets you search files for specific patterns. &lt;br&gt;
Let's take a simple example and go ahead an create a simple log file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ping google.com &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /your/destination/output-logfile.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let that command run for a few seconds and kill the process. You should be left with a file with several lines in it.&lt;br&gt;
Now if you would just want to read the file, you would run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; /your/destination/output-logfile.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But that would print out everything. What you want is to only view the line where the statistics are displayed. This is where grep comes in handy with something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; /your/destination/output-logfile.log | &lt;span class="nb"&gt;grep &lt;/span&gt;packets
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where the word packets is the word contained in one line I want to display. The output would be something like :&lt;br&gt;
&lt;code&gt;7 packets transmitted, 7 received, 0% packet loss, time 6009ms&lt;/code&gt;&lt;br&gt;
Now this outputs us the whole line but we can actually format the output to only display what is interesting to us.&lt;br&gt;
Let's imagine I only want to see the number of packets that were transmitted. This can be done by piping an additional command after the &lt;code&gt;grep&lt;/code&gt;. Also, instead of creating a file and reading it with grep, we will be running grep directly on the running script :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ping &lt;span class="nt"&gt;-c&lt;/span&gt; 1 google.com | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="s1"&gt;'packets'&lt;/span&gt; | &lt;span class="nb"&gt;cut&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; , &lt;span class="nt"&gt;-f&lt;/span&gt; 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this command we say :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ping once google.com and give me the details&lt;/li&gt;
&lt;li&gt;only display the line with the word packets in it&lt;/li&gt;
&lt;li&gt;cut everything after the first delimiter ',' and only output the first group which leaves us with : 
&lt;code&gt;1 packets transmitted&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Saving commands in script files
&lt;/h2&gt;

&lt;p&gt;Now all of those shell tools are useful but sometimes they can get lengthy and this is exactly why script files exist. Most of the time you want to reuse those useful scripts you designed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating the file
&lt;/h3&gt;

&lt;p&gt;Most script files for good notice start with &lt;br&gt;
&lt;code&gt;#!/bin/bash&lt;/code&gt;. This is called the &lt;code&gt;she-bang(shabang)&lt;/code&gt; which derives from the concatenation of the tokens sharp (#) and bang (!). This line is pretty important as it defines which shell is gonna interpret your script. &lt;code&gt;/bin/bash&lt;/code&gt; is usually implemented as a symbolic link pointed to the executable of whichever shell is the system default shell.&lt;br&gt;
You should probably always start your scripts also by some little documentation appending it to your &lt;code&gt;shabang&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="c"&gt;# This is a script to list apples&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way you can always come back to your script and actually understand what it was meant for without having to read the code in it.&lt;br&gt;
Now our very simple script starts with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="c"&gt;# This is a script to list apples&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"I have 3 apples"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not super exciting is it?&lt;br&gt;
Well we can try to add variables&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="c"&gt;# This is a script to list apples&lt;/span&gt;
&lt;span class="nv"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;Hello
&lt;span class="nv"&gt;b&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Good Morning"&lt;/span&gt;
&lt;span class="nv"&gt;c&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;19
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$a&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$b&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$c&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$b&lt;/span&gt;&lt;span class="s2"&gt;! I have &lt;/span&gt;&lt;span class="nv"&gt;$c&lt;/span&gt;&lt;span class="s2"&gt; apples"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here your script has 3 variables defined that you can reuse by using the &lt;code&gt;$&lt;/code&gt; sign before the variable name.&lt;br&gt;
Now let's try to use that with our ping and grep command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="c"&gt;# This is a script to ping and view packets transmitted&lt;/span&gt;
&lt;span class="nv"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;ping &lt;span class="nt"&gt;-c&lt;/span&gt; 1 google.com | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="s1"&gt;'packets'&lt;/span&gt; | &lt;span class="nb"&gt;cut&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; , &lt;span class="nt"&gt;-f&lt;/span&gt; 1&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now 2 in 1, we execute a child script in our script, store the value in our variable and echo the variable.&lt;/p&gt;

&lt;p&gt;Now all of those examples are to demonstrate how shell can be powerful rather than actually explain how things can be done with existing Linux tools. If you want to dig deeper, remember that you can always type &lt;code&gt;man your tool&lt;/code&gt; and you will be prompted with a full documentation or link to wherever you can find information on how to use your tool.&lt;/p&gt;

</description>
      <category>bash</category>
      <category>shell</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Get started with deno (from NodeJS or not...)</title>
      <dc:creator>Jaro</dc:creator>
      <pubDate>Fri, 10 Dec 2021 12:56:01 +0000</pubDate>
      <link>https://dev.to/mnjaro/get-started-with-deno-from-nodejs-or-not-38go</link>
      <guid>https://dev.to/mnjaro/get-started-with-deno-from-nodejs-or-not-38go</guid>
      <description>&lt;p&gt;If you have been working with NodeJS or not even, you surely heard about Deno (no-De) and that's no hazard. Javascript, Typescript and NodeJS are technologies that have been on the rise for quite a while now. Well what if I told you that those three technologies are actually implemented by default in Deno, without you having to do much to set them up if not any.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;A little bit of history&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Differences with NodeJS&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;From scratch web-server&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  A little bit of history
&lt;/h2&gt;

&lt;p&gt;Just like NodeJS, Deno is a &lt;a href="https://en.wikipedia.org/wiki/Runtime_system"&gt;runtime&lt;/a&gt; for Javascript. And also like NodeJS it is Authored by the same person that worked on NodeJS, mister Ryan Dahl. This project is actually not that recent. It was first announced in 2018 during a talk "10 things I regret about NodeJS". It's important to take this lightly as NodeJS is still and will continue to be a stable and robust environment to code in. But some initial design decisions have ignited a will to change or enhance the existent. Deno was first written in Go but was then rewrote in Rust, another rising technology that I would definitely recommend you learn about. Deno's official 1.0 version was released on May 13, 2020 and the current lts version is 1.10.0.  &lt;/p&gt;

&lt;h2&gt;
  
  
  NodeJS comparison
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Security
&lt;/h3&gt;

&lt;p&gt;The most important change, and difference that deno brings compared to NodeJS is security.&lt;br&gt;
Deno's core was written in Rust and Tokio which is the asynchronous run time for Rust. Rust itself is a language that is focused on performance and safety most importantly when it comes to concurrency. It provides a safer way to manage memory without having to use a garbage collector. On top of that, deno does not allow any type of file, environment or network access unless you actually give it the rights. Even so, those permissions are scoped and do not provide a wide range of access.&lt;/p&gt;
&lt;h3&gt;
  
  
  Strict, typed and linted
&lt;/h3&gt;

&lt;p&gt;Across different projects, if we want to have these 3 principles in place with NodeJS, a lot of configuration, installation and tweaking are required. This pollutes the workspace and complicates deployments. &lt;br&gt;
Deno ships Typescript, a linter and an assertion library out of the box without needing any configuration whatsoever. You can still configure if you wish but that will probably not be needed.&lt;/p&gt;
&lt;h2&gt;
  
  
  From scratch web-server
&lt;/h2&gt;

&lt;p&gt;In any case, Deno ships a lot more things but this is enough to get us started. Any additional information can be found on the official repository url that I will be linking at the end of the article.&lt;br&gt;
As for a little start, since nodejs has been used as a very popular web server, I thought it might be interesting to start building a deno version of it.&lt;br&gt;
To start off, let's install deno. Depending on your OS you might want to refer to &lt;a href="https://github.com/denoland/deno_install"&gt;Install Docs&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Shell
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-fsSL&lt;/span&gt; https://deno.land/x/install/install.sh | sh &lt;span class="nt"&gt;-s&lt;/span&gt; v0.38.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;That's basically it! We don't need to look at typescript configurations we can directly start up creating our first deno file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;touch &lt;/span&gt;api.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now just like nodejs, Deno ships a ready to use http server package that we need to import.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;serve&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://deno.land/std@v0.42.0/http/server.ts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, let's go ahead and import our sql. Of course, I am using sqlite for demo purposes but you might go ahead and get whatever suites your needs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;DB&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;https://deno.land/x/sqlite/mod.ts&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, let's add some simple sample of code to run our http server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8162&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;serve&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;` Listening on &amp;lt;http://localhost&amp;gt;:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;respond&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;n&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And voila! A running http server. Now compared to nodejs, what was not needed to do beforehand: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install manually the sqlite dependency&lt;/li&gt;
&lt;li&gt;Install manually typescript, and configure
Now, we did say a running http server but that may not mean it is fully functional. Remember we said authorizations are required in order for deno to access different functionalities? Well http or network access requires a specific authorization.
In order to do this we must append a flag to our deno command as such:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Deno api —allow-net
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can now go ahead and on our localhost:8162 to check out our newly created HTTP Deno server!&lt;br&gt;
Now on to using that sqlite module we imported. It wouldn't be a &amp;gt;simple&amp;lt; get started tutorial with some basic database action so here is a snippet of code to get started :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Open a database&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;DB&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;test.db&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`
  CREATE TABLE IF NOT EXISTS people (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT
  )
`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Peter Parker&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Clark Kent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bruce Wayne&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Run a simple query&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;names&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;INSERT INTO people (name) VALUES (?)&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Print out data in table&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;SELECT name FROM people&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Close connection&lt;/span&gt;
&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;close&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can open a database, run a query and print out data. If you weren't in a Deno article, you would have probably never realized that this is not nodejs and that is the whole point! Deno is not, a fundamental coding change coming from nodejs. And if you start with Deno, you could totally switch to nodejs without loosing any of the acquired knowledge.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Now as you can see if you come from nodejs or from a JavaScript environment, there isn't much difference from what you know. Although, if you actually come from nodejs, you would find interesting that the root nodejs api is fully promised built. The difference and additions are more at the root of the software. As a growing technology, nodejs needed a security upgrade. Again as a growing technology, nodejs also needed a simple environment that is not bloated in dozens of config files. As of me, I would use Deno for from scratch projects. I don't feel like I am getting onboard of a totally new ship. Documentation is understandable and modules are maintained if they are featured on the official deno.land page. Now should you upgrade your existing projects from nodejs to deno? Deno does give you some upgrading best practices and guidelines but it is not something that will get done with a snap of the fingers. Keep in mind, deno is not here to replace node but to give a more secure and less error-prone environment. Node is still going to be out there and will probably never get less popular than Deno. But Deno is a good alternative if you are looking for a strict, secure and tidy Javascript runtime environment to code your favorite projects on!&lt;/p&gt;

</description>
      <category>deno</category>
      <category>node</category>
      <category>typescript</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
