{"id":108,"date":"2022-09-26T21:17:49","date_gmt":"2022-09-26T15:47:49","guid":{"rendered":"http:\/\/yuvishere.co.in\/?p=108"},"modified":"2022-10-17T17:58:16","modified_gmt":"2022-10-17T12:28:16","slug":"how-i-started-writing-shell-scripts","status":"publish","type":"post","link":"https:\/\/yuvishere.co.in\/blog\/how-i-started-writing-shell-scripts\/","title":{"rendered":"How I started writing shell scripts"},"content":{"rendered":"\n<p>As a developer, we use the CLI a lot for various things, and as someone who has to probably learn about computers, it is highly recommended to have some understanding about CLI and it&#8217;s capabilities.<\/p>\n\n\n\n<p>For me, it all started when I was trying to work with <strong><a rel=\"noreferrer noopener\" href=\"https:\/\/ffmpeg.org\/\" target=\"_blank\">ffmpeg<\/a><\/strong> and the <code>bash<\/code> shell to iterate through a number of mp4 files and extract their audio in mp3 files. The idea was, to take each item with the <code><strong>.mp4<\/strong><\/code> file format in the current directory and execute the following code for each of these items: <code><strong>ffmpeg -i filename.mp4 filename.mp3<\/strong><\/code>. In simple words, just take the file with the name <code>filename<\/code> which is of the mp4 format and convert it to mp3 format. I wrote a useful piece of code to do it in the bash shell itself, and it was this: <code><strong>for file in *.mp4; do ffmpeg -i \"$file\" \"${file%???}mp3\"; done;<\/strong><\/code>. The ones who are familiar with bash shell probably know this and are cringing at it, but it simply means this: &#8220;for&#8221; every item which matches with .mp4 in the end (*.mp4), reference to such an item as &#8220;file&#8221; and do (or execute) &#8216;ffmpeg -i &#8220;$file&#8221; &#8220;${file%???}mp3&#8217;. Here, &#8220;$file&#8221; means the item with &#8220;.mp4&#8221; in the end and &#8220;${file%???}mp3&#8221; means remove the last three items (or in this case, letters) from &#8220;$file&#8221; and attach &#8220;mp3&#8221; to the remaining string.<\/p>\n\n\n\n<p>For the longest time, I had this command saved somewhere for any rainy day when it would be needed. Just copy pasting it in the CLI. Then I thought about it and figured, there must be a way to automate this. Sometime later, I came across the fact that you can write proper scripts and save them in files, just like writing code in a particular language and saving it in the given file format. I started with some very basic programs and operations, played with the shebang operator. It was good times. In the end, I finally wrote the following script to automate this and saved it as a binary executable in my <em>bash_profile<\/em>.<\/p>\n\n\n\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-1 wp-block-group-is-layout-flex\">\n<p class=\"has-background-color has-foreground-background-color has-text-color has-background\"><code><em>#!\/bin\/bash<\/em><br><strong>mkdir<\/strong> -p output<br><strong>for<\/strong> name <strong>in<\/strong> *.<strong>$1<\/strong>; <strong>do<\/strong><br>ffmpeg -i \"<strong>$name<\/strong>\" \"output\/<strong>${name%.*}<\/strong>.<strong>$2<\/strong>\"<br><strong>done<\/strong><\/code><\/p>\n\n\n\n<p>Definitely an upgrade over the previous one and much more scalable. I replaced a couple of parameters and named this script as <code><strong>ffmpegrec<\/strong><\/code>. So the usage for this script is <code><strong>ffmpegrec &lt;input format&gt; &lt;output format&gt;<\/strong><\/code>.<\/p>\n\n\n\n<p>Without even realising at the moment, this was my very first script which automated some mundane yet essential processes for me. A couple of years down the line, I am a fan of the bash shell trying to work my way through various tasks a developer. Surely, there are a number of benefits to knowing and automating your work using whatever script you use, there are a number of them to choose from with little to no differences.<\/p>\n<\/div>\n\n\n\n<p>Some references you can use:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/tldp.org\/LDP\/Bash-Beginners-Guide\/html\/sect_02_01.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/tldp.org\/LDP\/Bash-Beginners-Guide\/html\/sect_02_01.html<\/a><\/li><li><a href=\"https:\/\/www.baeldung.com\/linux\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.baeldung.com\/linux\/<\/a><\/li><li><a href=\"https:\/\/www.shellscript.sh\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.shellscript.sh\/<\/a><\/li><li><a href=\"https:\/\/www.tomshardware.com\/how-to\/write-bash-scripts-linux\" data-type=\"URL\" data-id=\"https:\/\/www.tomshardware.com\/how-to\/write-bash-scripts-linux\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.tomshardware.com\/how-to\/write-bash-scripts-linux<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>As a developer, we use the CLI a lot for various things, and as someone who has to probably learn about computers, it is highly recommended to have some understanding about CLI and it&#8217;s capabilities. For me, it all started when I was trying to work with ffmpeg and the bash shell to iterate through [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,6],"tags":[24,25,23],"class_list":["post-108","post","type-post","status-publish","format-standard","hentry","category-introduction","category-technology","tag-bash","tag-programming","tag-shell-scripting"],"_links":{"self":[{"href":"https:\/\/yuvishere.co.in\/blog\/wp-json\/wp\/v2\/posts\/108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yuvishere.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yuvishere.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yuvishere.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yuvishere.co.in\/blog\/wp-json\/wp\/v2\/comments?post=108"}],"version-history":[{"count":5,"href":"https:\/\/yuvishere.co.in\/blog\/wp-json\/wp\/v2\/posts\/108\/revisions"}],"predecessor-version":[{"id":119,"href":"https:\/\/yuvishere.co.in\/blog\/wp-json\/wp\/v2\/posts\/108\/revisions\/119"}],"wp:attachment":[{"href":"https:\/\/yuvishere.co.in\/blog\/wp-json\/wp\/v2\/media?parent=108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yuvishere.co.in\/blog\/wp-json\/wp\/v2\/categories?post=108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yuvishere.co.in\/blog\/wp-json\/wp\/v2\/tags?post=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}