2023-01-28T12:43:51+00:00https://www.redditstatic.com/icon.png//r/bash.rssA subreddit dedicated to bash scripting.#!/bin/bash/u/trollinDChttps://www.reddit.com/user/trollinDC<!-- SC_OFF --><div class="md"><p>I enjoy looking through all the posts in this sub, to see the weird shit you guys are trying to do. Also, I think most people are happy to help, if only to flex their knowledge. However, a huge part of programming in general is <em>learning</em> how to troubleshoot something, not just having someone else fix it for you. One of the basic ways to do that in bash is <a href="https://linuxhint.com/set-x-command-bash/"><code>set -x</code></a>. Not only can this help <strong>you</strong> figure out what your script is doing and how it&#39;s doing it, but in the event that you need help from another person, posting the output can be beneficial to the person attempting to help. </p> <p>Also, writing scripts in an <a href="https://marketplace.visualstudio.com/items?itemName=mads-hartmann.bash-ide-vscode">IDE that supports Bash</a>. syntax highlighting can immediately tell you that you&#39;re doing something wrong. </p> <p>If an IDE isn&#39;t an option, <a href="https://www.shellcheck.net/">https://www.shellcheck.net/</a></p> <p>Edit: Thanks to the mods for pinning this!</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/trollinDC"> /u/trollinDC </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/xcejrb/set_x_is_your_friend/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/xcejrb/set_x_is_your_friend/">[comments]</a></span>t3_xcejrb2022-09-12T14:39:30+00:002022-09-12T14:39:30+00:00set -x is your friend/u/Electronic_Youthhttps://www.reddit.com/user/Electronic_Youth<!-- SC_OFF --><div class="md"><p>Yes homework questions are allowed, but do remember the point of homework is for <strong><em>YOU</em></strong> to learn the material not us.<br/> Here are some guidelines on how to get the best answer to help you learn. </p> <p><strong>What we like:</strong></p> <ul> <li>Provide full context. Don&#39;t just drop a question without details.<br/></li> <li>Tell us exactly what you need to solve and what information you&#39;ve been given.<br/></li> <li>Include any specific restrictions given by your professor.<br/></li> <li>If you can&#39;t get yourself started, that&#39;s OK, but do tell us and we will see if we can get you going without just spoon feeding you. It really is better for you and better for everyone.</li> <li>Tell us what you&#39;ve tried already, it will help with explaining the best future direction.<br/></li> <li>Make a start on solving it, share your code and what errors you are seeing. <em>Share error messages exactly.</em><br/></li> </ul> <p><strong>What we don&#39;t like:</strong></p> <ul> <li>&quot;Do it for me, with complete explanation please&quot;</li> <li>URGENT, DUE IN 15 MINUTES, or other phrases in ALL CAPS.</li> <li>People who delete their question once they get an answer.</li> </ul> <p><strong>When you share code, do it in a way that works for everyone.</strong></p> <ul> <li>Share code, not pictures of code. We don&#39;t want to work from a screenshot or a photo of a terminal.</li> <li>If your script is longer than 30 or so lines, it might be better to share it via a code service like github or via a pastebin.</li> <li>Shorter scripts or code snippets can just be included in your post.</li> <li>When you paste code into your question, ideally don&#39;t use the &#39;fancypants&#39; editor because the code will be broken for users of old reddit.</li> <li>When you use &#39;markdown&#39; mode or old.reddit then please leave 1 blank line before your script and 4 spaces before each line to get the correct formatting. </li> <li>Try running your code through shellcheck (link in sidebar) before submitting it. Shellcheck will catch many errors and offer suggestions for common problems and might save you some time.</li> </ul> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/Electronic_Youth"> /u/Electronic_Youth </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/z2gzzi/homework_questions/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/z2gzzi/homework_questions/">[comments]</a></span>t3_z2gzzi2022-11-23T05:58:21+00:002022-11-23T05:58:21+00:00Homework questions./u/curlyfries922https://www.reddit.com/user/curlyfries922<!-- SC_OFF --><div class="md"><p><a href="https://github.com/Agb43/text-editor.git">https://github.com/Agb43/text-editor.git</a></p> <p>&#x200B;</p> <p>I decided to write a terminal based text editor in bash for fun. Considering that i&#39;ve never taken a coding class in my life, i&#39;m pretty proud of it, even if inserting whitespace via blank echos is incredibly bad im sure. Also, this isn&#39;t an ide. It&#39;s simply a script that consolidates a bunch of other functions into one. Mainly manipulations like redeacting words, stuff likr that. Either way its 400 lines. This includes batch functions as well as single file operations. You can go back and forth between batch and single file operations with a nice menu</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/curlyfries922"> /u/curlyfries922 </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/10n6u6m/text_editor_in_bash/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10n6u6m/text_editor_in_bash/">[comments]</a></span>t3_10n6u6m2023-01-28T05:08:17+00:002023-01-28T05:08:17+00:00text editor in bash!/u/EmilySeville7cfghttps://www.reddit.com/user/EmilySeville7cfg<!-- SC_OFF --><div class="md"><p>Hi! As an input for sed program I have smth like this: <code>{bool some flag description}</code> (it&#39;s a part of an input string to be more precise) where:</p> <ul> <li><code>bool</code> is a placeholder type</li> <li><code>some flag description</code> is it&#39;s description</li> </ul> <p>How do I replace all such placeholders to this <code>{{some_flag_description}}</code>? I don&#39;t know how to replace spaces between 3 description words with underscores. What sed technique should I use? I wanna transform <code>some flag description</code> in a such way not to broke the whole placeholder. Another difficulty is that this placeholder can appear in any place in a command invocation example like <code>some_executable --dark {bool whether to enable dark more} --colorize {bool whether to colorize files}</code>. I need to know how to do this to fix my <a href="https://github.com/emilyseville7cfg-better-tldr/prototypes/blob/main/md-to-btldr/md-to-btldr.sh">script</a>.</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/EmilySeville7cfg"> /u/EmilySeville7cfg </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/10nblyy/changing_placeholder_description_via_sed/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10nblyy/changing_placeholder_description_via_sed/">[comments]</a></span>t3_10nblyy2023-01-28T10:01:11+00:002023-01-28T10:01:11+00:00Changing placeholder description via sed/u/CrunchyTesticlehttps://www.reddit.com/user/CrunchyTesticle<!-- SC_OFF --><div class="md"><p>Hello,</p> <p>I&#39;m new to Bash, and I&#39;ve been hammering my way through issues with the help of ChatGPT, but it&#39;s running me in circles on this one.</p> <p>I&#39;m using Curl to make a request to Pushover&#39;s API. Many of the options that I call Curl with are exactly the same, so I thought that I&#39;d put them in a variable to make things easier. I know that In an actual request, I would have to include more options, but I wanted to trim this example down.</p> <pre><code>po_token=&quot;ExampleToken&quot; po_user=&quot;ExampleUserKey&quot; curl_ops=&#39;-s -o &quot;/dev/null&quot; --connect-timeout &quot;5&quot; --retry &quot;6&quot; --retry-delay &quot;600&quot; -F &quot;token=$po_token&quot; -F &quot;user=$po_user&quot; &quot;https://api.pushover.net/1/messages.json&quot;&#39; curl $curl_ops </code></pre> <p>It seems that everything in my &quot;curl_ops&quot; variable is being passed as a single argument instead of a literal string, and I&#39;m certain it has to do with my usage of quotes, but I have no idea how to fix it. The only scripting language that I&#39;m familiar with is AutoIt, and Bash seems to handle this kind of thing very differently. I was hoping someone here may have some suggestions.</p> <p>Thanks</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/CrunchyTesticle"> /u/CrunchyTesticle </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/10nbdv5/beginner_having_trouble_using_variables_as/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10nbdv5/beginner_having_trouble_using_variables_as/">[comments]</a></span>t3_10nbdv52023-01-28T09:47:39+00:002023-01-28T09:47:39+00:00Beginner having trouble using variables as literal strings/u/Revolutionary_Cydiahttps://www.reddit.com/user/Revolutionary_Cydia&#32; submitted by &#32; <a href="https://www.reddit.com/user/Revolutionary_Cydia"> /u/Revolutionary_Cydia </a> <br/> <span><a href="https://www.trendmicro.com/en_gb/research/21/f/bash-ransomware-darkradiation-targets-red-hat--and-debian-based-linux-distributions.html">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10mmz8r/interesting_bash_malware/">[comments]</a></span>t3_10mmz8r2023-01-27T15:03:47+00:002023-01-27T15:03:47+00:00Interesting bash malware/u/erikadehttps://www.reddit.com/user/erikade<!-- SC_OFF --><div class="md"><p>The first public release of BashkitV1 is now available with the URL</p> <p><a href="https://bashkit.wuage.io">https://bashkit.wuage.io</a></p> <p>and from the main branch of the Bashkit git repository</p> <p>(<a href="https://github.com/Wuageorg/bashkit">https://github.com/Wuageorg/bashkit</a>).</p> <p>Bashkit is the Wuage’s shell scripting framework. It is a collection of pure bash functions along with a lightweight scripting model that, we believe, remediate some of the Bash quirks.</p> <p>Out of the box, It supports advanced yet classical logging (including NDJSON), fail fast scripting pattern (less to none `set -e` side effects), coloured outputs and more. It can easily be extended by adding modules to accommodate various use cases.</p> <p>For more information on the features of Bashkit that are new to this type of shell scripts, see the file `doc/manual.pdf’. There is also Unix-style man pages for every released function. The man pages are the definitive descriptions of the framework’s features.</p> <p>The framework is released under APACHE LICENSE, VERSION 2.0.</p> <p>The release tar file includes the formatted documentation (pdf).</p> <p>Please use <a href="https://github.com/Wuageorg/bashkit/issues">https://github.com/Wuageorg/bashkit/issues</a> to report bugs with this version. It complements at least bash 5.x and can not be used without.</p> <p>Happy scripting!</p> <p>The Wuage Team</p> <p>Jan. 2023</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/erikade"> /u/erikade </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/10moz5g/bashkit_v1/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10moz5g/bashkit_v1/">[comments]</a></span>t3_10moz5g2023-01-27T16:25:35+00:002023-01-27T16:25:35+00:00Bashkit V1/u/threehappypenguinshttps://www.reddit.com/user/threehappypenguins<!-- SC_OFF --><div class="md"><p>How would I place echo commands inside a variable? I am attempting to send an email with postfix under certain circumstances. For example:</p> <pre><code>OUTPUT=$(MP4Box -info $INPUT_FILE 2&gt;&amp;1) HTML=&quot;&lt;pre style=\&quot;white-space: pre-wrap; word-break: keep-all; background-color: whitesmoke;\&quot;&gt;&lt;code&gt;${OUTPUT}&lt;/code&gt;&lt;/pre&gt;&quot; if [[ $(MP4Box -info $INPUT_FILE 2&gt;&amp;1) =~ &quot;Incomplete&quot; ]]; then ( echo &quot;From: from@email.com&quot;; echo &quot;To: send@email.com&quot;; echo &quot;Subject: Transfer Interrupted&quot;; echo &quot;Content-Type: text/html&quot;; echo &quot;MIME-Version: 1.0&quot;; echo &quot;&quot;; echo -e &quot;The mp4 transfer was interrupted and did not complete.&quot; \ &quot;The output is as follows:\n\n&quot; \ &quot;${HTML}&quot; ) | sendmail -t elif [[ $(MP4Box -info $INPUT_FILE 2&gt;&amp;1) =~ &quot;Movie Info&quot; ]]; then ( echo &quot;From: from@email.com&quot;; echo &quot;To: send@email.com&quot;; echo &quot;Subject: Transfer complete.&quot;; echo &quot;Content-Type: text/html&quot;; echo &quot;MIME-Version: 1.0&quot;; echo &quot;&quot;; echo -e &quot;The mp4 transfer is complete.&quot; \ &quot;The output is as follows:\n\n&quot; \ &quot;${HTML}&quot; ) | sendmail -t else ( echo &quot;From: from@email.com&quot;; echo &quot;To: send@email.com&quot;; echo &quot;Subject: Check on Transfer.&quot;; echo &quot;Content-Type: text/html&quot;; echo &quot;MIME-Version: 1.0&quot;; echo &quot;&quot;; echo -e &quot;Something could be wrong. Manually check the mp4.&quot; \ &quot;The output is as follows:\n\n&quot; \ &quot;${HTML}&quot; ) | sendmail -t fi </code></pre> <p>I would like to simplify this with variables. When I try, I get the postfix error <code>No recipient addresses found in message header</code>. For example:</p> <pre><code>OUTPUT=$(MP4Box -info $INPUT_FILE 2&gt;&amp;1) HTML=&quot;&lt;pre style=\&quot;white-space: pre-wrap; word-break: keep-all; background-color: whitesmoke;\&quot;&gt;&lt;code&gt;${OUTPUT}&lt;/code&gt;&lt;/pre&gt;&quot; EMAILHEADER=$(echo &quot;From: from@email.com&quot;; echo &quot;To: send@email.com&quot;; echo &quot;Subject: ${SUBJECT}&quot;; echo &quot;Content-Type: text/html&quot;; echo &quot;MIME-Version: 1.0&quot;; echo &quot;&quot;;) if [[ $(MP4Box -info $INPUT_FILE 2&gt;&amp;1) =~ &quot;Incomplete&quot; ]]; then ( $EMAILHEADER echo -e &quot;The mp4 transfer was interrupted and did not complete.&quot; \ &quot;The output is as follows:\n\n&quot; \ &quot;${HTML}&quot; ) | sendmail -t elif [[ $(MP4Box -info $INPUT_FILE 2&gt;&amp;1) =~ &quot;Movie Info&quot; ]]; then ( $EMAILHEADER echo -e &quot;The mp4 transfer is complete.&quot; \ &quot;The output is as follows:\n\n&quot; \ &quot;${HTML}&quot; ) | sendmail -t else ( $EMAILHEADER echo -e &quot;Something could be wrong. Manually check the mp4.&quot; \ &quot;The output is as follows:\n\n&quot; \ &quot;${HTML}&quot; ) | sendmail -t fi </code></pre> <p>I know I still have to define <code>$SUBJECT</code> and will get to that, but I at least want to get the basics of this working first.</p> <p>I have one additional question... how can I break up the <code>HTML=</code> line (within the script)? It&#39;s long and I want to make it two lines to make the script more readable. So instead of:</p> <pre><code>HTML=&quot;&lt;pre style=\&quot;white-space: pre-wrap; word-break: keep-all; background-color: whitesmoke;\&quot;&gt;&lt;code&gt;${OUTPUT}&lt;/code&gt;&lt;/pre&gt;&quot; </code></pre> <p>I want something like:</p> <pre><code>HTML=&quot;&lt;pre style=\&quot;white-space: pre-wrap; word-break: keep-all;&quot; \ &quot;background-color: whitesmoke;\&quot;&gt;&lt;code&gt;${OUTPUT}&lt;/code&gt;&lt;/pre&gt;&quot; </code></pre> <p>But this fails for me (doesn&#39;t print out in the email).</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/threehappypenguins"> /u/threehappypenguins </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/10mwah8/echo_commands_in_variable/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10mwah8/echo_commands_in_variable/">[comments]</a></span>t3_10mwah82023-01-27T21:15:32+00:002023-01-27T21:15:32+00:00Echo commands IN variable/u/West-Salad-2546https://www.reddit.com/user/West-Salad-2546<!-- SC_OFF --><div class="md"><p>Trying to run a bash script. when i save it in my home, it can run just fine but when I move the script to a /app location, the script does not run. All permissions are ok but when i run the script, it does not run and also does not return an error. I also noticed that the color code I have in the script in my home is not appearing in when i have a script in the /app location. please help if you have any idea what the issue is here.</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/West-Salad-2546"> /u/West-Salad-2546 </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/10mvsvh/cannot_run_a_script_in_app_directory/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10mvsvh/cannot_run_a_script_in_app_directory/">[comments]</a></span>t3_10mvsvh2023-01-27T20:55:45+00:002023-01-27T20:55:45+00:00cannot run a script in /app directory/u/_a_techie_https://www.reddit.com/user/_a_techie_<!-- SC_OFF --><div class="md"><p>What is your favourite bash oneliner?</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/_a_techie_"> /u/_a_techie_ </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/10m7pml/bash_oneliner/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10m7pml/bash_oneliner/">[comments]</a></span>t3_10m7pml2023-01-27T00:51:07+00:002023-01-27T00:51:07+00:00Bash oneliner/u/lovemac18https://www.reddit.com/user/lovemac18<!-- SC_OFF --><div class="md"><p>Hi everyone, I’m new to this sub so I apologize in advance for any mistakes. </p> <p>I created a script that checks if a python application is running and if not starts it. This is what it looks like:</p> <pre><code>#!/bin/bash if pgrep -x &quot;python3.10&quot; &gt; /dev/null then echo &quot;Application is running.&quot; else source dev/bin/activate cd dev nohup python3.10 main.py &gt; output.log 2&gt;&amp;1 &amp; echo &quot;Application restarted.&quot; fi` </code></pre> <p>This code works perfectly fine when I run it from the terminal, however, when I try to run it using cron I get the correct output but main.py doesn’t actually start. </p> <p>I’m not sure what I’m doing wrong. Any help would be appreciated!</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/lovemac18"> /u/lovemac18 </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/10mjh9x/help_with_a_scheduled_cron_bash_script/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10mjh9x/help_with_a_scheduled_cron_bash_script/">[comments]</a></span>t3_10mjh9x2023-01-27T12:20:18+00:002023-01-27T12:20:18+00:00Help with a scheduled (cron) bash script/u/Sidneys1https://www.reddit.com/user/Sidneys1<!-- SC_OFF --><div class="md"><p>This is a fairly short (59 LOC) utility script that allows you to tail the output of a command, but while only showing the last <code>-n</code> (default 5) lines of output without scrolling the output buffer. It will trim lines to the terminal width (rather than wrap them) to avoid splitting terminal escape sequences. You can also optionally <code>-p PREFIX</code> each line of output. Finally, it (by default) removes blank/whitespaces lines, but they can be preserved with <code>-w</code>.</p> <p><a href="https://i.imgur.com/VkBzA5S.mp4">Video here</a>.</p> <p><a href="https://reddit.com/link/10m102l/video/0y2nzxf22gea1/player">https://reddit.com/link/10m102l/video/0y2nzxf22gea1/player</a></p> <p>Code on <a href="https://gist.github.com/Sidneys1/8973f8868300c8bac2e714de3d99ede4">GitHub Gist</a>.</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/Sidneys1"> /u/Sidneys1 </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/10m102l/stailsh_short_tail/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10m102l/stailsh_short_tail/">[comments]</a></span>t3_10m102l2023-01-26T20:02:53+00:002023-01-26T20:02:53+00:00stail.sh - Short Tail/u/Alternative_Brick_72https://www.reddit.com/user/Alternative_Brick_72<!-- SC_OFF --><div class="md"><p>Hi I am newbie to Linux, I would like to learn Bash scripting </p> <p>Please suggest me some good resources.</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/Alternative_Brick_72"> /u/Alternative_Brick_72 </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/10ltudi/books_or_websites_to_learn_bash_scripting/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10ltudi/books_or_websites_to_learn_bash_scripting/">[comments]</a></span>t3_10ltudi2023-01-26T15:04:24+00:002023-01-26T15:04:24+00:00Books or Websites to learn Bash scripting/u/No_Basket_3037https://www.reddit.com/user/No_Basket_3037<!-- SC_OFF --><div class="md"><p>What is the website I&#39;m thinking of!!!??? We use it all the time for bash scripting, and I can not figure it outt. I&#39;m pretty sure it&#39;s shell something, but nothing so far sounds right. Can anyone help me or at least explain what this is doing, I get the general idea, but I&#39;m curious how it helps make scripts ready for reddit.</p> <pre><code>echo ; sed &#39;s/^/ /&#39; script.sh </code></pre> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/No_Basket_3037"> /u/No_Basket_3037 </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/10mepie/shell/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10mepie/shell/">[comments]</a></span>t3_10mepie2023-01-27T07:01:30+00:002023-01-27T07:01:30+00:00shell...???/u/_7567Rexhttps://www.reddit.com/user/_7567Rex&#32; submitted by &#32; <a href="https://www.reddit.com/user/_7567Rex"> /u/_7567Rex </a> <br/> <span><a href="https://i.redd.it/uho98ghbkdea1.jpg">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10lpwi9/bash_script_says_file_not_found_even_though_the/">[comments]</a></span>t3_10lpwi92023-01-26T11:41:27+00:002023-01-26T11:41:27+00:00Bash script says file not found even though the filename is the script itself/u/JonB0nerhttps://www.reddit.com/user/JonB0ner&#32; submitted by &#32; <a href="https://www.reddit.com/user/JonB0ner"> /u/JonB0ner </a> <br/> <span><a href="https://www.reddit.com/gallery/10m4ave">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10m4ave/read_wont_take_any_userinput_except_one/">[comments]</a></span>t3_10m4ave2023-01-26T22:19:43+00:002023-01-26T22:19:43+00:00Read won't take any userinput except one/u/nowhereman531https://www.reddit.com/user/nowhereman531<!-- SC_OFF --><div class="md"><p>I decided I was going to learn a bit more about the <code>PS1</code> prompt. I started out with a nice easy prompt with <code>\w</code> on one line and the prompt with a custom user token <code>𝝅</code> rather than the stock <code>$</code> on the next.</p> <p>I liked the setup a lot, so I used the same configuration on my home work station and the termux installation on my phone.</p> <p>The setup worked great until I started running the wrong commands on the wrong system. For good reason, as the prompts were identical, I decided to see if I could setup my prompt to show a different prompt for an ssh connection.</p> <p>I had a fun time getting that to actually work. I was making it more complicated than it needed to be, but wait there&#39;s more. Now when I connect to my ssh server it shows the IP address of the login before last rather than the current login. With a remote login it is kind of useless to see that you just logged in but it is useful to see if someone logged in before you... Just in case you know.</p> <p>Once I got that working I decided to take it to a whole new level of ridiculous... Solely because why not. I wanted to see what it would take to show in my local terminal that there was an active connection. Next was to make the command search for an active connection on my SSH port, if one was active it ran one prompt and if no connection, another. it took some trial and error to get that running correctly. Once it was running, I found that it would only update when a new terminal session was opened or if I sourced .bashrc. Which in and of itself wasn&#39;t that bad but there had to be a way to get that info without sourcing or starting a new terminal session.</p> <p>After a bit more trial and error and research on the topic i found the answer to getting that info to update after each command. The <code>PROMPT_COMMAND</code> setting was what did the trick. By wrapping the whole command into a function i was able to call it in the <code>PROMPT_COMMAND</code> which gets evaluated after every command runs.</p> <p>Now not only will it show an active connection, it will show the last IP to login to that users account. It will also search through a predefined list of known IP addresses and if they match the IP address will be green to denote a known IP and display a custom string so you don&#39;t have to look at your own IP addresses. If it returns an unknown IP address it will turn red.</p> <p>Then to add the finishing touches to this ridiculous project, a red/green light for inactive/active connections respectively, just because I could</p> <p>I&#39;d like to hear how you all would make it better/different. It was a fun project to learn about the prompt and make sure I used proper escaping. So here is my absolutely, totally, over the top, unnecessary PS1 prompt. Complete with functions and PROMPT_COMMAND</p> <p>&#x200B;</p> <pre><code># This will show if there is an active SSH connection after each command is executed. # Shows second to last login of current user; best used with AllowUsers in sshd_config psONE_ssh() { if [[ &quot;$(last | sed -n &#39;2p&#39; | awk &#39;{ print $3 }&#39;)&quot; =~ (&quot;XXX.XXX.&quot;*|&quot;XXX.XXX.XXX.XXX&quot;|&quot;XXX.XXX.XXX.XXX&quot;|&quot;XXX.XXX.XXX.XXX&quot;) ]]; then printf %b &quot;\\[\\e[1;32m\\]KNOWN CONNECTION\n&quot; else last | sed -n &#39;2p&#39; | awk &#39;{ print $3 }&#39; fi } psONE_local() # Shows the last login from the current user; best used with AllowUsers in sshd_config { if [[ &quot;$(lastlog | grep $(whoami) | awk &#39;{ print $3 }&#39;)&quot; =~ (&quot;XXX.XXX.&quot;*|&quot;XXX.XXX.XXX.XXX&quot;|&quot;XXX.XXX.XXX.XXX&quot;|&quot;XXX.XXX.XXX.XXX&quot;) ]]; then printf %b &quot;\\[\\e[1;32m\\]KNOWN CONNECTION\n&quot; else lastlog | grep $(whoami) | awk &#39;{ print $3 }&#39; fi } _prompt_command() { if [[ -n $SSH_CLIENT ]]; then PS1=&quot;\\[\\e[1;31m\\]🟢 $(psONE_ssh)\\[\\e[0m\\]\\[\\e[1;33m\\]\n\w/\n\\[\\e[0m\\]\\[\\e[1;31m\\]𝝅 \\[\\e[0m\\]\\[\\e[1;32m\\] &quot; else ss -tn src :8222 | grep ESTAB &amp;&gt; /dev/null if [ $? -ne &quot;1&quot; ]; then PS1=&quot;\\[\\e[1;31m\\]🟢 $(psONE_local)\\[\\e[1;33m\\]\n\w/\n\\[\\e[0m\\]\\[\\e[1;31m\\]𝝅\\[\\e[0m\\]\\[\\e[1;32m\\] &quot; else PS1=&quot;\\[\\e[1;31m\\]🔴 $(psONE_local)\\[\\e[1;33m\\]\n\w/\n\\[\\e[0m\\]\\[\\e[1;31m\\]𝝅\\[\\e[0m\\]\\[\\e[1;32m\\] &quot; } # This will show if there is an active SSH connection after each command is executed. PROMPT_COMMAND=&quot;_prompt_command; history -n; history -w; history -c; history -r; $PROMPT_COMMAND&quot; </code></pre> <p>I know this is total overkill but it was fun.</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/nowhereman531"> /u/nowhereman531 </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/10kyw1i/boredom_ps1_project/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10kyw1i/boredom_ps1_project/">[comments]</a></span>t3_10kyw1i2023-01-25T13:42:10+00:002023-01-25T13:42:10+00:00Boredom PS1 project/u/ka1ikasanhttps://www.reddit.com/user/ka1ikasan<!-- SC_OFF --><div class="md"><p>I am a junior/mid data-scientist and developer and it&#39;s been years the first thing I do on a new workstation is to add a couple of aliases I cannot live without. But I want to step up a little in terms of best practices. And I have a couple of questions.</p> <p>The first one: how long can my .bash_aliases file grow? Mine are about 10-20 lines total, but I have so many ideas of adding new stuff. Not sure whether it is a good practice to automate / simplify things to myself or it would just make me forget about the actual syntax of some complex commands. </p> <p>The second one: do you add custom functions in your .bash_aliases or do they go elsewhere? I have a couple of functions that _really_ are a quick lifehacks such as:</p> <pre><code>function cds { cd $1 &amp;&amp; ls -lah } </code></pre> <p>How would you organize such functions in order to avoid future yourself any headache?</p> <p>Thanks!</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/ka1ikasan"> /u/ka1ikasan </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/10kxidg/bash_aliases_best_practice/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10kxidg/bash_aliases_best_practice/">[comments]</a></span>t3_10kxidg2023-01-25T12:28:40+00:002023-01-25T12:28:40+00:00Bash aliases best practice/u/West-Salad-2546https://www.reddit.com/user/West-Salad-2546<!-- SC_OFF --><div class="md"><p>would love to get some input on this please. having some issues with getting the script to decrypt the gpg file. i am using the file to store passwords i need to to run the script. any help would be appreciated. thanks</p> <p>#!/bin/bash </p> <p># Decrypt the file using the passphrase<br/> gpg --decrypt-files --batch --yes --passphrase &quot;$(cat passphrase.txt)&quot; encrypted_passwords.gpg -o decrypted_passwords </p> <p># Assign the decrypted passwords to variables<br/> password1=&quot;$(cut -d &#39;,&#39; -f1 decrypted_passwords)&quot;<br/> password2=&quot;$(cut -d &#39;,&#39; -f2 decrypted_passwords)&quot; </p> <p># first password as an argument<br/> PKR_VAR_vsphere_password= &quot;$password1&quot; </p> <p># second password as an argument<br/> PKR_VAR_winadmin_password=&quot;$password2&quot;</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/West-Salad-2546"> /u/West-Salad-2546 </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/10kunza/decrypt_a_gpg_file_inside_a_script/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10kunza/decrypt_a_gpg_file_inside_a_script/">[comments]</a></span>t3_10kunza2023-01-25T09:20:41+00:002023-01-25T09:20:41+00:00decrypt a gpg file inside a script<br/> <span><a href="/r/linuxquestions/comments/10kz23l/key_modifiers_checker/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10kz2ke/key_modifiers_checker/">[comments]</a></span>t3_10kz2ke2023-01-25T13:51:29+00:002023-01-25T13:51:29+00:00Key modifiers checker/u/threehappypenguinshttps://www.reddit.com/user/threehappypenguins<!-- SC_OFF --><div class="md"><p>How can I check for a string from a command output? For example, if I execute:</p> <pre><code>MP4Box -info $INPUT_FILE </code></pre> <p>And if the file is incomplete, it outputs:</p> <pre><code>[iso file] Incomplete box mdat - start 40 size 2998060684 [iso file] Incomplete file while reading for dump - aborting parsing File has no movie (moov) - static data container </code></pre> <p>I want my script to pick up on the word &quot;Incomplete&quot; or something. For example, my bash script:</p> <pre><code>#!/bin/bash FILENAME=*.mp4 WATCH_DIR=/path/to/directory /usr/bin/inotifywait \ --recursive \ --monitor \ --quiet \ -e moved_to \ -e close_write \ --format &#39;%w%f&#39; \ --includei &quot;$FILENAME&quot; \ &quot;$WATCH_DIR&quot; \ | while read -r INPUT_FILE; do echo &quot;&quot; echo &quot;Transfer stopped. Checking if mp4 file is complete:&quot; echo &quot;&quot; if [[ $(MP4Box -info $INPUT_FILE) == &quot;Incomplete&quot; ]]; then echo &quot;&quot; echo &quot;Transfer interrupted.&quot; else echo &quot;&quot; echo &quot;Transfer complete.&quot; fi done </code></pre> <p>No matter what happens, I cannot get &quot;Transfer interrupted&quot; to echo out even when I interrupt the transfer and the output shows <code>Incomplete file while reading for dump - aborting parsing</code>.</p> <p>I have tried grep, and every which option I can think of, and it won&#39;t work for me. What am I doing wrong?</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/threehappypenguins"> /u/threehappypenguins </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/10knklt/check_for_string_of_command_output/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10knklt/check_for_string_of_command_output/">[comments]</a></span>t3_10knklt2023-01-25T02:24:54+00:002023-01-25T02:24:54+00:00Check for string of command output/u/JackJones2018https://www.reddit.com/user/JackJones2018<!-- SC_OFF --><div class="md"><p>Hi Guys</p> <p>How do I get this command to be either true or false </p> <p>diff $LAST $CURRENT | grep --exclude-dir=* &#39;<sup>&lt;&#39;</sup> &gt;&gt;$EMAILTEXT </p> <p>Basically if there is no difference between last and current give me a false and if there is a difference give me a true and output the results to email text. Thanks</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/JackJones2018"> /u/JackJones2018 </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/10kitld/diff_and_grep_inside_if_statement/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10kitld/diff_and_grep_inside_if_statement/">[comments]</a></span>t3_10kitld2023-01-24T22:50:31+00:002023-01-24T22:50:31+00:00Diff and Grep inside IF statement/u/threehappypenguinshttps://www.reddit.com/user/threehappypenguins<!-- SC_OFF --><div class="md"><p>I am creating an inotify-tools script and this currently works for any file that looks something like <code>2023-01-24_14-59-22.mp4</code>:</p> <pre><code>inotifywait --recursive --monitor --quiet -e moved_to -e close_write --format &#39;%w%f&#39; --include [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]_[0-9][0-9]-[0-9][0-9]-[0-9][0-9].mp4 /watch/folder </code></pre> <p>Occasionally, an mp4 file might arrive as such: <code>2023-01-24_14-59-22_edit.mp4</code> or <code>2023-01-24_14-59-22_edit_edit.mp4</code> or other random text or characters at the end of the date/time and before the .mp4 extension. How do I watch for these files as well? I tried <code>*</code> or <code>$</code> or <code>\w</code> and stuff like that and it didn&#39;t work, but I&#39;m not entirely understanding the bash documentation. Can someone point me in the right direction?</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/threehappypenguins"> /u/threehappypenguins </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/10kd8rd/recognize_any_text_or_characters_at_the_end_of_a/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10kd8rd/recognize_any_text_or_characters_at_the_end_of_a/">[comments]</a></span>t3_10kd8rd2023-01-24T19:04:43+00:002023-01-24T19:04:43+00:00Recognize any text or characters at the end of a filename before the extension/u/InternetSea8293https://www.reddit.com/user/InternetSea8293<!-- SC_OFF --><div class="md"><p>How do i answer prompts like (y/n), (yes/no) or enter?</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/InternetSea8293"> /u/InternetSea8293 </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/10k24bf/how_to_send_a_enter_press/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10k24bf/how_to_send_a_enter_press/">[comments]</a></span>t3_10k24bf2023-01-24T10:01:02+00:002023-01-24T10:01:02+00:00how to send a "enter" press?/u/InternetSea8293https://www.reddit.com/user/InternetSea8293<!-- SC_OFF --><div class="md"><p>Example</p> <p>&#x200B;</p> <pre><code>[specific line] text text 2 </code></pre> <p>I now want to add &quot;text 3&quot; under &quot;specific line&quot; so it looks like this</p> <pre><code>[specific line] text 3 text text 2 </code></pre> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/InternetSea8293"> /u/InternetSea8293 </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/10k3fg1/how_to_insert_text_after_a_specific_line_in_a_file/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10k3fg1/how_to_insert_text_after_a_specific_line_in_a_file/">[comments]</a></span>t3_10k3fg12023-01-24T11:33:36+00:002023-01-24T11:33:36+00:00How to insert text after a specific line in a file?/u/Lord_Schnitzelhttps://www.reddit.com/user/Lord_Schnitzel<!-- SC_OFF --><div class="md"><pre><code>1 #!/bin/bash 1 2 bt=&quot;bluetoothctl info 74:45:CE:90:9C:4F | grep Connected&quot; 3 if [[ $bt=&#39;Connected: yes&#39; ]] 4 then 5 dunstify &quot;headphones connected&quot; 6 else 7 dunstify &quot;unknown error&quot; 8 fi </code></pre> <p>Edit. I made this to work by the help of user <a href="https://www.reddit.com/user/sadsack_of_shit/"><strong>sadsack_of_shit</strong></a> so thank you!</p> <p>The correct line with awk is: bt=&quot;$(bluetoothctl info 74:45:CE:90:9C:4F | awk &#39;/Connected/ {print $2}&#39;)&quot;</p> <p>What is the wrong here? It always prints the &#39;headphones connected&#39; -line even if my headphones isn&#39;t connected.</p> <p>I know awk would be much better, but I couldn&#39;t make that to work. (The &quot;Connected: yes&quot; is the 10th line of that command)</p> <p>&#x200B;</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/Lord_Schnitzel"> /u/Lord_Schnitzel </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/10jjxd9/beginner_cant_make_a_simple_script_to_work/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10jjxd9/beginner_cant_make_a_simple_script_to_work/">[comments]</a></span>t3_10jjxd92023-01-23T18:42:14+00:002023-01-23T18:42:14+00:00Beginner can't make a simple script to work/u/West-Salad-2546https://www.reddit.com/user/West-Salad-2546<!-- SC_OFF --><div class="md"><p>Hey everyone, having a project deadline and would love some help please. </p> <p>i have the below script set to run and get the credentials from another file but i keep getting an error saying credentials file cannot be found. would love some help please. i will also take any other ideas for a better script.</p> <p>#!/bin/bash</p> <p>fail() { printf &quot;%s\n&quot; &quot;$2&quot;; exit $1; } </p> <p>credsfile=/home/njames/.config/windows.credentials</p> <p>[[ -e &quot;$credsfile&quot; ]] || fail 1 &quot;Credentials file &#39;$credsfile&#39; not found&quot; </p> <p>source &quot;$credsfile&quot; </p> <p>[[ -n &quot;PKR_VAR_vsphere_password&quot; ]] || fail 1 &quot;PKR_VAR_vsphere_password not set in &#39;$credsfile&#39;&quot;</p> <p>[[ -n &quot;PKR_VAR_winadmin_password&quot; ]] || fail 1 &quot;PKR_VAR_winadmin_password not set in &#39;$credsfile&#39;&quot; </p> <p>packer init .</p> <p>packer validate .</p> <p>packer build .</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/West-Salad-2546"> /u/West-Salad-2546 </a> <br/> <span><a href="https://www.reddit.com/r/bash/comments/10jwg35/bash_script_to_copy_password_from_a_different_file/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/bash/comments/10jwg35/bash_script_to_copy_password_from_a_different_file/">[comments]</a></span>t3_10jwg352023-01-24T03:53:13+00:002023-01-24T03:53:13+00:00bash script to copy password from a different file