{"id":1305,"date":"2023-11-06T12:00:00","date_gmt":"2023-11-06T20:00:00","guid":{"rendered":"https:\/\/letsbuildroboticswithshadow8472.com\/?p=1305"},"modified":"2023-11-03T17:00:44","modified_gmt":"2023-11-04T00:00:44","slug":"how-i-would-relearn-linux-5-basic-scripting","status":"publish","type":"post","link":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/2023\/11\/06\/how-i-would-relearn-linux-5-basic-scripting\/","title":{"rendered":"How I would Relearn Linux #5: Basic Scripting"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Good Morning from my Robotics Lab! This is Shadow_8472 with another tip for how I would relearn Linux. Let\u2019s get started.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Scripts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">No serious system administrator has time to memorize long chains of commands taking minutes to type and might need fixing after the first try. When faced with a periodic task involving a long, involved [set of] command[s], it\u2019s preferable to write them in a script to be run sequentially from file. While I did get by for a few years by using Bash\u2019s (terminal shell, see section on shells) history functionality, a script is a much more sustainable way to \u201cremember\u201d commands.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To write a script, put your command[s] into a text document \u2013 one per line. The pound sign # at the start of a line turns it into a comment to clue future you or others into your thought process. Using a backslash \\ character and additional whitespace for alignment, lines may be broken up at any point to increase readability. This becomes especially useful when dealing with half a dozen flags.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#this line is a comment\npodman run \\\n    --name testpilot \\\n    --network podman \\\n    --ip 10.88.2.88 \\\n    -v ~\/sandbox\/testVolume:\/root\/vol1 \\\n    --rm \\\n    busybox:latest\n#No interrupting commands \u2013 even with commented flags.\n#    -v ~\/sandbox\/wrongTestVolume:\/root\/vol2 \\<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A script will then need permission to execute.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod +x &lt;filename&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Lacking a proper explanation of the Linux permissions system, just know this is a fast way to ensure YOU have permission to EXECUTE your script.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Even understanding up to this point unlocks a powerhouse of possibility. These tools \u2013sequential commands, comments, line breaks, and white space\u2013 are all I need to make scripts to stitch together more complicated programs I work with to build my home server. Most importantly: scripting commands allows me to clearly visualize the command I\u2019m working on.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Shells<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">One slightly mind bending concept is the shell. In simplified terms, a computer shell is an operating environment. Open a terminal on a Linux workstation, and you probably have a Bash shell. Some part of desktop environment you opened that shell from is a graphical shell. From Bash, you can open another instance of Bash inside the last one like a Russian matrushka doll (nesting dolls). Connect to another computer with SSH, and there\u2019s another shell (one one each machine working together, I think?). Other programs \u2013such as a Python interpreter\u2013 may have a shell of their own.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Not all shells are intended for human interaction. Somewhere below the graphical environment is logically a shell either running directly atop the kernel (I don\u2019t actually know if the kernel itself counts as a shell) optimized for running quickly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Most important to this conversation: running a script will open a shell for its own use and close it afterwords. Fancier shell scripts can leverage many powers characteristic of typical programming languages. Flow control allows logic to branch and loop depending on the state of a whole assortment of variable types. And thanks to shell manipulation, variables can be isolated from one another in ways I\u2019m totally unfamiliar with. And all this exposed power comes packaged directly into the operating system itself.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Takeaway<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Shell scripting is a powerful tool. This single lesson is enough to unlock a wealth of potential, yet only a fraction of its total capabilities. Variables \u2013for example\u2013 turned out more involved than I thought, so I scrapped a section them after writing about shells to support it. While it\u2019s OK to run with only a partial knowledge, it\u2019s also good to be aware of additional capacities when reading scripts written, polished, and published for use by other people.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Final Question<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">What projects have you designed with scripts?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Good Morning from my Robotics Lab! This is Shadow_8472 with another tip for how I would relearn Linux. Let\u2019s get started. Scripts No serious system administrator has time to memorize long chains of commands taking minutes to type and might need fixing after the first try. When faced with a periodic task involving a long, &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/2023\/11\/06\/how-i-would-relearn-linux-5-basic-scripting\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How I would Relearn Linux #5: Basic Scripting&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[2,237],"tags":[300,37,299,257],"class_list":["post-1305","post","type-post","status-publish","format-standard","hentry","category-linux","category-relearning-linux","tag-bash","tag-linux","tag-scripting","tag-terminal"],"_links":{"self":[{"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/posts\/1305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/comments?post=1305"}],"version-history":[{"count":1,"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/posts\/1305\/revisions"}],"predecessor-version":[{"id":1306,"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/posts\/1305\/revisions\/1306"}],"wp:attachment":[{"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/media?parent=1305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/categories?post=1305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/tags?post=1305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}