<?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: Julian Toscani</title>
    <description>The latest articles on DEV Community by Julian Toscani (@jtoscani).</description>
    <link>https://dev.to/jtoscani</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%2F226933%2Faa0289e3-20d4-4bf5-9e59-70b92ee27d2b.png</url>
      <title>DEV Community: Julian Toscani</title>
      <link>https://dev.to/jtoscani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jtoscani"/>
    <language>en</language>
    <item>
      <title>TIL: Stubbing nested, global properties in Vitest</title>
      <dc:creator>Julian Toscani</dc:creator>
      <pubDate>Fri, 31 Mar 2023 09:15:16 +0000</pubDate>
      <link>https://dev.to/jtoscani/til-stubbing-nested-global-properties-in-vitest-35o7</link>
      <guid>https://dev.to/jtoscani/til-stubbing-nested-global-properties-in-vitest-35o7</guid>
      <description>&lt;p&gt;I recently upgraded some functionality of our legacy codebase to fit our new Stack containing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Vue3/Nuxt3&lt;/li&gt;
&lt;li&gt;Vite&lt;/li&gt;
&lt;li&gt;Vitest&lt;/li&gt;
&lt;li&gt;Typescript&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The feature that had to be rewritten was a scroll-depth tracking. It extensively uses &lt;code&gt;IntersectionObservers&lt;/code&gt; and some global properties like &lt;code&gt;innerWidth&lt;/code&gt; or &lt;code&gt;document.visibilityState&lt;/code&gt; to do some checks. As I had a hard time testing changes on &lt;code&gt;document.visibilityState&lt;/code&gt; changes, I write this post so you won't have to deal with them.&lt;/p&gt;

&lt;p&gt;TLDR;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vi.spyOn(document, 'visibilityState', 'get').mockImplementationOnce(() =&amp;gt; 'hidden');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  First approach: simply setting it
&lt;/h2&gt;

&lt;p&gt;Does not work as &lt;code&gt;document.visibilityState&lt;/code&gt; is defined as &lt;code&gt;read-only&lt;/code&gt;. It is, however a solution if you want to manipulate &lt;code&gt;window.innerWidth&lt;/code&gt; as shown in the &lt;a href="https://vitest.dev/api/vi.html#vi-stubglobal"&gt;docs&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Second Approach: Using global stubs
&lt;/h2&gt;

&lt;p&gt;The first idea I had was to use &lt;a href="https://vitest.dev/api/vi.html#vi-stubglobal-1"&gt;global stubs&lt;/a&gt;. With these you can overwrite a global property or object and provide &lt;a href="https://vitest.dev/guide/mocking.html"&gt;Mock&lt;/a&gt; to use instead. I used this to stub the &lt;code&gt;IntersectionObservers&lt;/code&gt; as suggested in the docs.&lt;/p&gt;

&lt;p&gt;Unfortunately, if you need the functionality of the original object somewhere else this won't work as it apparently gets hoisted, meaning it is not scoped to your current test. For me, this did not work. &lt;code&gt;vi.unstubAllGlobals()&lt;/code&gt; did not reset the stubs which may have prevented this issue as we use JSDOM.&lt;/p&gt;

&lt;h2&gt;
  
  
  Third Approach: Using a &lt;code&gt;spy&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://vitest.dev/api/vi.html#vi-spyon"&gt;&lt;code&gt;spy&lt;/code&gt;s&lt;/a&gt; are &lt;code&gt;function&lt;/code&gt;s which observe the use of &lt;code&gt;method&lt;/code&gt;s. You can use them like so &lt;code&gt;vi.spyOn(object, "methodName")&lt;/code&gt;. They are usefull in cases where you check if calling a &lt;code&gt;function&lt;/code&gt; or &lt;code&gt;method&lt;/code&gt; calls another &lt;code&gt;function&lt;/code&gt; or &lt;code&gt;method&lt;/code&gt;. This helped me to find out if the &lt;code&gt;function&lt;/code&gt; I was testing was called. However, I needed to now if it was called with the correct argument.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution: mocking the return of a &lt;code&gt;getter&lt;/code&gt; you &lt;code&gt;spy&lt;/code&gt; on
&lt;/h2&gt;

&lt;p&gt;After a bit of tinkering I found out, that &lt;code&gt;vi.spy&lt;/code&gt; accepts a third argument and allows me to provide a &lt;code&gt;mockImplementation&lt;/code&gt; for that &lt;code&gt;setter/getter&lt;/code&gt;. The third parameter describes if the property - instead of being a method - is a &lt;code&gt;getter&lt;/code&gt; or &lt;code&gt;setter&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Hence I ended up with the following solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;vi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;spyOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;visibilityState&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="s1"&gt;get&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;mockImplementationOnce&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hidden&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// and later&lt;/span&gt;

&lt;span class="nx"&gt;vi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;spyOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;visibilityState&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="s1"&gt;get&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;mockImplementationOnce&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;visible&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;If you have questions, better ideas or find errors, please leave a comment. &lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>testing</category>
      <category>vite</category>
      <category>vue</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>TIL: The easiest way to handle index overflow...</title>
      <dc:creator>Julian Toscani</dc:creator>
      <pubDate>Thu, 22 Dec 2022 08:25:49 +0000</pubDate>
      <link>https://dev.to/jtoscani/til-the-easiest-way-to-handle-index-overflow-38np</link>
      <guid>https://dev.to/jtoscani/til-the-easiest-way-to-handle-index-overflow-38np</guid>
      <description>&lt;h2&gt;
  
  
  TLDR;
&lt;/h2&gt;

&lt;p&gt;You can use the modulo operator in conjunction with the length of the array. This requires you to set you position after every change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;handleOverflow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;chosen&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;chosen&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Recently, I was working on a search input with an autocomplete feature. One of the requirements was that users should be able to cycle through the options by pressing arrow keys. Additionally, if you were at the last item and pressed &lt;code&gt;down&lt;/code&gt; it should jump to the first item in the list. Similarly, if you were at the first element and pressed &lt;code&gt;up&lt;/code&gt; it should jump to the last item. &lt;/p&gt;

&lt;h2&gt;
  
  
  First Solution
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The Mental Model
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;I have an array of options&lt;/li&gt;
&lt;li&gt;I have a numerical value to access one element of that array, the index

&lt;ol&gt;
&lt;li&gt;In order to get the next element I increase the index.&lt;/li&gt;
&lt;li&gt;In order to get the previous element I decrease the index.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;If the index gets below &lt;code&gt;0&lt;/code&gt; I need to jump to the end&lt;/li&gt;

&lt;li&gt;If the index gets bigger than the index of the last element, I need to jump to first element.&lt;/li&gt;

&lt;/ol&gt;

&lt;h3&gt;
  
  
  Solution
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;handleOverflow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;chosen&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;chosen&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;min&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;max&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;next&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;h3&gt;
  
  
  What I did not like about the implementation
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;It is verbose&lt;/li&gt;
&lt;li&gt;I have to reset a value inside of the function&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  'Improving' the solution
&lt;/h2&gt;

&lt;p&gt;What basically happens is that I need to access an element in a list with a known size. So basically, I only need to worry about the size of the array and the position I am at.&lt;/p&gt;

&lt;p&gt;So, if I am at position 5 (meaning the 6th element) in an array with 6 elements and I want to go to the next element I go to index 6. Unfortunately, that does not exist so I go back to the start which is at index 0. I know that &lt;code&gt;6 % 6&lt;/code&gt; equals &lt;code&gt;0&lt;/code&gt;. Hence, accessing the array by &lt;code&gt;index % array.length&lt;/code&gt; would work for positive numbers. &lt;/p&gt;

&lt;p&gt;With negative numbers, that does not work unfortunately as &lt;code&gt;-1 % array.length&lt;/code&gt; equals &lt;code&gt;-1&lt;/code&gt; so even by removing the &lt;code&gt;-&lt;/code&gt; I would land at the first element, not the last. This forces me to add the length of the array to the result of the modulo operation and BAM, we have the solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;handleOverflow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;chosen&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;chosen&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What do you think about this solution? &lt;br&gt;
Is it better or worse than the first?&lt;br&gt;
How would you handle this problem?&lt;/p&gt;

&lt;p&gt;Best and Happy Holidays!&lt;/p&gt;

</description>
      <category>python</category>
      <category>django</category>
      <category>backend</category>
      <category>learning</category>
    </item>
    <item>
      <title>Linux - View and Edit File Permissions.</title>
      <dc:creator>Julian Toscani</dc:creator>
      <pubDate>Wed, 30 Nov 2022 07:34:28 +0000</pubDate>
      <link>https://dev.to/jtoscani/linux-view-and-edit-file-permissions-450j</link>
      <guid>https://dev.to/jtoscani/linux-view-and-edit-file-permissions-450j</guid>
      <description>&lt;p&gt;When working with servers most of the time you will work with &lt;strong&gt;linux&lt;/strong&gt; servers (Ubuntu, Debian, etc.). In order to change, view or create &lt;strong&gt;files&lt;/strong&gt; you need the right &lt;strong&gt;permissions&lt;/strong&gt;. To get a basic understanding of what that means, I will tell you how to view, read and interpret these permissions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Viewing Permissions
&lt;/h2&gt;

&lt;p&gt;An easy way to view permissions for files inside a folder you can use the &lt;code&gt;ls&lt;/code&gt; command with the &lt;code&gt;o&lt;/code&gt; or &lt;code&gt;l&lt;/code&gt; flag like so:&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;ls&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt;
&lt;span class="c"&gt;#or&lt;/span&gt;
&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Move to a directory that has files in it and type the above command into your terminal. It will generate multiple lines of output that look similar to 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="c"&gt;# permissions   hard links  user    group of user   file size    change date     file/folder name&lt;/span&gt;
  drwxrwxr-x    9           toscani toscani         288         11 Nov 09:07    public
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important part are the first ten letters &lt;code&gt;drwxrwxr-x&lt;/code&gt;. They describe the permissions. These characters are broken down into four groups.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The first letter, in this case &lt;code&gt;d&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Letters two to four &lt;code&gt;rwx&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Letters five to seven &lt;code&gt;rwx&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;and finally eight to ten &lt;code&gt;r-x&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Group one states if the entry describes a file &lt;code&gt;-&lt;/code&gt;, a directory &lt;code&gt;d&lt;/code&gt; or a link &lt;code&gt;l&lt;/code&gt;. The other three groubs describe the permissons each type of user has. On linux we distinguish between the &lt;strong&gt;user&lt;/strong&gt;, the &lt;strong&gt;group&lt;/strong&gt; and &lt;strong&gt;others&lt;/strong&gt; - in this order. Hence &lt;code&gt;drwxrwxr-x&lt;/code&gt; means:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It is a &lt;strong&gt;directory&lt;/strong&gt; which can be&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;read&lt;/em&gt;, &lt;em&gt;written&lt;/em&gt; and &lt;em&gt;executed&lt;/em&gt; by the &lt;strong&gt;user&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;read&lt;/em&gt;, &lt;em&gt;written&lt;/em&gt; and &lt;em&gt;executed&lt;/em&gt; by the &lt;strong&gt;group&lt;/strong&gt; the user is in&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;read&lt;/em&gt; and &lt;em&gt;executed&lt;/em&gt; by the &lt;strong&gt;others&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;You are maybe wondering what "executing a directory" means. It basically lets the you open that directory. "Reading a directory" on the other hand means "to be able to see" the directory, e.g. being able to list it with the &lt;code&gt;ls&lt;/code&gt; command.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Changing permissions
&lt;/h2&gt;

&lt;p&gt;Now that you know how to view file permissions, I will tell you how to change them. For this we use the &lt;code&gt;chmod&lt;/code&gt; (change mode) command. There are two different methods for this. The &lt;em&gt;symbolic method&lt;/em&gt; and the &lt;em&gt;absolute method&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you have ever seen the &lt;code&gt;chmod&lt;/code&gt; command in a tutorial before the &lt;em&gt;symbolic method&lt;/em&gt; is the one with the letters, the &lt;em&gt;absolute method&lt;/em&gt; is the one with the numbers ;).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Symbolic method
&lt;/h3&gt;

&lt;p&gt;The &lt;em&gt;symbolic method&lt;/em&gt; combines an &lt;em&gt;access class&lt;/em&gt; with an &lt;em&gt;operator&lt;/em&gt; and an &lt;em&gt;access type&lt;/em&gt; to set permissions. A common example is to make a file executable for everybody:&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;chmod &lt;/span&gt;a+x my_file.sh
&lt;span class="c"&gt;# or, as `a` is the default&lt;/span&gt;
&lt;span class="nb"&gt;chmod&lt;/span&gt; +x my_file.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Access class&lt;/th&gt;
&lt;th&gt;Operator&lt;/th&gt;
&lt;th&gt;Access Type&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;u&lt;/code&gt;, user&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;+&lt;/code&gt;, to add access&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;r&lt;/code&gt;, read&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;g&lt;/code&gt;, group&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;-&lt;/code&gt;, remove access&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;w&lt;/code&gt;, write&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;o&lt;/code&gt;, other&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;=&lt;/code&gt;, set exact access&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;x&lt;/code&gt;, execute&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;a&lt;/code&gt;, all&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;These parameters can be combined in many different ways. Although I can´t tell you what the right configuration for a specific usecase is, the following examples might help you to find that out yourself.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;u&lt;/code&gt; always means the current user and &lt;code&gt;g&lt;/code&gt; always refers to the group the current user is in.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# remove read and write permissions for all except you&lt;/span&gt;
&lt;span class="nb"&gt;chmod &lt;/span&gt;go-rw my_file.sh

&lt;span class="c"&gt;# set access to only read for everybody&lt;/span&gt;
&lt;span class="nb"&gt;chmod &lt;/span&gt;&lt;span class="nv"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;r myfile.sh

&lt;span class="c"&gt;# remove write access from and add execution rights for the group&lt;/span&gt;
&lt;span class="nb"&gt;chmod &lt;/span&gt;g-w+x myfile.sh

&lt;span class="c"&gt;# grant read permission for a directory and all files in it to all&lt;/span&gt;
&lt;span class="c"&gt;# -R is used as a flag, not as a parameter to describe the permissions&lt;/span&gt;
&lt;span class="nb"&gt;chmod&lt;/span&gt; &lt;span class="nt"&gt;-R&lt;/span&gt; +r mydir
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Absolute method
&lt;/h3&gt;

&lt;p&gt;In contrast to the &lt;em&gt;symbolic method&lt;/em&gt;, the &lt;em&gt;absolute method&lt;/em&gt; sets all permissions at once by using a three digit number. Each of the digits is the sum of it´s individual permissions. Let´s use the following example:&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;chmod &lt;/span&gt;774 myfile.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Base permissions
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Permission&lt;/th&gt;
&lt;th&gt;Number&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;read&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;write&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;execute&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;no permissions&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h4&gt;
  
  
  Combinations
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Permission&lt;/th&gt;
&lt;th&gt;Number&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;read, write and execute (4 + 2 + 1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;read and write (4 + 2)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;read and execute (4 + 1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;execute and write (1 + 2)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The first number represents the &lt;strong&gt;user&lt;/strong&gt;, the second the &lt;strong&gt;group&lt;/strong&gt; and the last represents &lt;strong&gt;others&lt;/strong&gt;. Hence, the example above sets the following permissions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;read, write and execute for the &lt;strong&gt;user&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;read, write and execute for the &lt;strong&gt;group&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;read for &lt;strong&gt;others&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now you know how to read and change file and directory permissions on linux. If you need further information just consult the &lt;code&gt;man&lt;/code&gt; page by typing &lt;code&gt;man chmod&lt;/code&gt; into the command line.&lt;/p&gt;

&lt;p&gt;If you have questions or feedback, please leave a comment :).&lt;/p&gt;

</description>
      <category>react</category>
    </item>
    <item>
      <title>TIL: Bash - Redirect Errors (stderr) to other files</title>
      <dc:creator>Julian Toscani</dc:creator>
      <pubDate>Thu, 17 Nov 2022 16:37:40 +0000</pubDate>
      <link>https://dev.to/jtoscani/til-bash-redirect-errors-stderr-to-other-files-1p54</link>
      <guid>https://dev.to/jtoscani/til-bash-redirect-errors-stderr-to-other-files-1p54</guid>
      <description>&lt;p&gt;Today I learned that, when &lt;strong&gt;redirecting output&lt;/strong&gt; to a file in &lt;strong&gt;bash&lt;/strong&gt;, you can specify where you want to redirect the &lt;code&gt;stdout&lt;/code&gt; and &lt;code&gt;stderr&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Redirecting in bash
&lt;/h2&gt;

&lt;p&gt;In bash you can use &lt;code&gt;&amp;gt;&lt;/code&gt; to redirect output of a command to a file. A basic example would be 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="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Hello World"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; hello.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will redirect the output of the &lt;code&gt;echo&lt;/code&gt; command to the file. As a result, it will create a &lt;code&gt;hello.txt&lt;/code&gt; file with the content &lt;code&gt;Hello World&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;cat&lt;/code&gt; command prints the content of a file to the terminal. Hence&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cat foo.txt &amp;gt; new.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will create a file called &lt;code&gt;new.txt&lt;/code&gt; with the content of &lt;code&gt;foo.txt&lt;/code&gt;. However, if foo.txt does not exist, it will prompt you with an error and create a file without content. Why?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cat foo.txt &amp;gt; hello.txt
$ cat: foo.txt: No such file or directory
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The reason for this is that the command differentiates between the handles &lt;code&gt;stdout&lt;/code&gt; and &lt;code&gt;stderr&lt;/code&gt;. Per default it only redirects &lt;code&gt;stdout&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Explicitly redirecting
&lt;/h2&gt;

&lt;p&gt;In order to redirect the output explicitly you have to prepend &lt;code&gt;&amp;gt;&lt;/code&gt; with either &lt;code&gt;1&lt;/code&gt; or &lt;code&gt;2&lt;/code&gt;. &lt;code&gt;1&lt;/code&gt; standing for &lt;code&gt;stdout&lt;/code&gt; and &lt;code&gt;2&lt;/code&gt; for &lt;code&gt;stderr&lt;/code&gt;. E.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cat foo.txt 1&amp;gt; hello.txt 2&amp;gt; error.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will now redirect all &lt;code&gt;stdout&lt;/code&gt; to &lt;code&gt;hello.txt&lt;/code&gt; and all errors to &lt;code&gt;error.txt&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As long as you are only working with one file, this might not be very helpful. However, if you loop over files in a directory it might be a different story.&lt;/p&gt;

&lt;p&gt;If you find any mistakes or want to add something, don´t hesitate to leave a comment!&lt;/p&gt;

</description>
      <category>bash</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>Bash Basics - Navigating the Filesystem.</title>
      <dc:creator>Julian Toscani</dc:creator>
      <pubDate>Fri, 11 Nov 2022 09:00:00 +0000</pubDate>
      <link>https://dev.to/jtoscani/bash-basics-navigating-the-filesystem-511f</link>
      <guid>https://dev.to/jtoscani/bash-basics-navigating-the-filesystem-511f</guid>
      <description>&lt;p&gt;Being able to use a server without a UI is a &lt;strong&gt;crucial skill&lt;/strong&gt; for every developer. In this article I will tell how to navigate a servers filesystem using &lt;strong&gt;bash&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Commands
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;pwd&lt;/span&gt;         &lt;span class="c"&gt;# print path to current directory&lt;/span&gt;
&lt;span class="nb"&gt;ls&lt;/span&gt; &amp;lt;path&amp;gt;   &lt;span class="c"&gt;# print visible content of current directory&lt;/span&gt;
&lt;span class="nb"&gt;cd&lt;/span&gt; &amp;lt;path&amp;gt;   &lt;span class="c"&gt;# change current directory&lt;/span&gt;
&lt;span class="nb"&gt;cat&lt;/span&gt; &amp;lt;path&amp;gt;  &lt;span class="c"&gt;# print content of a file&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Paths can be expressed in different ways. Here is an overview of what this means in practise.&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;# Ways to express paths&lt;/span&gt;
foo/bar      &lt;span class="c"&gt;# relative to current directory&lt;/span&gt;
./foo/bar    &lt;span class="c"&gt;# equivalent to foo/bar&lt;/span&gt;

/foo/bar     &lt;span class="c"&gt;# from root&lt;/span&gt;
~/foo/bar    &lt;span class="c"&gt;# from home directory&lt;/span&gt;
../          &lt;span class="c"&gt;# to parent directory&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hence, if you want to move to the parent directory, you type:&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;cd&lt;/span&gt; ../
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to check the content of the &lt;code&gt;assets/&lt;/code&gt; folder in your current directory (if one exists) you would type:&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;ls &lt;/span&gt;assets/
&lt;span class="c"&gt;# or&lt;/span&gt;
&lt;span class="nb"&gt;ls&lt;/span&gt; ./assets/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some of these commands can show even more information. For that to happen, you can apply &lt;code&gt;flags&lt;/code&gt; to them. A good example of this is the &lt;code&gt;ls&lt;/code&gt; command. Here are some examples:&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;# show all files and folders with their current permissions, creation date and file size&lt;/span&gt;
&lt;span class="c"&gt;# remove `a` to hide hidden files (prefixed with `.`)&lt;/span&gt;
&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-ao&lt;/span&gt; &amp;lt;path&amp;gt;

&lt;span class="c"&gt;# also show all subdirectories recursively&lt;/span&gt;
&lt;span class="c"&gt;# caution - gets big fast!&lt;/span&gt;
&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-R&lt;/span&gt;

&lt;span class="c"&gt;# show all files and folders including all their subdirectories&lt;/span&gt;
&lt;span class="c"&gt;# with their current permissions, creation date and file size&lt;/span&gt;
&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-aoR&lt;/span&gt; &amp;lt;path&amp;gt;

&lt;span class="c"&gt;# Add line-numbers to the output&lt;/span&gt;
&lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; &amp;lt;path&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Did I miss something? Let me know!&lt;br&gt;
&lt;small&gt;&lt;br&gt;
Image taken from unsplash by &lt;a href="https://unsplash.com/photos/4Mw7nkQDByk"&gt;Gabriel Heinzer&lt;/a&gt;&lt;br&gt;
&lt;/small&gt;&lt;/p&gt;

</description>
      <category>bash</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
