<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Knowledge Base (Posts about bash)</title><link>https://bgstack15.ddns.net/blog/</link><description></description><atom:link href="https://bgstack15.ddns.net/blog/categories/bash.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><copyright>Contents © 2023 &lt;a href="mailto:bgstack15@gmail.com"&gt;bgstack15&lt;/a&gt; 
&lt;a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/"&gt;
&lt;img alt="Creative Commons License BY-SA"
style="border-width:0; margin-bottom:12px;"
src="https://bgstack15.ddns.net/.images/l_by-sa_4.0_88x31.png"&gt;&lt;/a&gt;</copyright><lastBuildDate>Tue, 18 Jul 2023 13:16:36 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Load nfs-mounted ssh keys at login automatically</title><link>https://bgstack15.ddns.net/blog/posts/2020/06/02/load-nfs-mounted-ssh-keys-at-login-automatically/</link><dc:creator>bgstack15</dc:creator><description>&lt;p&gt;I use multiple ssh keys across multiple systems. Some systems need to have the
same ssh key loaded. My solution is to store the generic ones on my nfs mount,
accessible only to my user, and to use a function in .bashrc:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;load_ssh_key&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="s2"&gt;"${1}"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;SSHKEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"${1}"&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="s2"&gt;"${SSHKEY}"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;SSHKEY&lt;/span&gt;&lt;span class="o"&gt;=/&lt;/span&gt;&lt;span class="n"&gt;mnt&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;bgstack15&lt;/span&gt;&lt;span class="o"&gt;/.&lt;/span&gt;&lt;span class="n"&gt;ssh&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;bgstack15_devuan&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;
   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="s2"&gt;"${SSHKEY}"&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="n"&gt;then&lt;/span&gt;
      &lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="s2"&gt;"${SSH_AGENT_PID}"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;eval&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;ssh&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;agent&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;grep&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;viE&lt;/span&gt; &lt;span class="s1"&gt;'Agent pid'&lt;/span&gt;
      &lt;span class="n"&gt;ssh&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="s2"&gt;"${SSHKEY}"&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;grep&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;viE&lt;/span&gt; &lt;span class="s2"&gt;"Identity added:"&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;
   &lt;span class="k"&gt;else&lt;/span&gt;
      &lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Unable to get to private key!"&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;
   &lt;span class="n"&gt;fi&lt;/span&gt;
   &lt;span class="n"&gt;unset&lt;/span&gt; &lt;span class="n"&gt;SSHKEY&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;load_ssh_key&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Also, now, the function is generally available for invoking with a filename to
load that ssh key. I realize ssh-add is pretty trivial, but I want the
function to fail silently for when I'm off-network (when I won't be doing any
ssh work anyway).&lt;/p&gt;</description><category>bash</category><category>key</category><category>ssh</category><guid>https://bgstack15.ddns.net/blog/posts/2020/06/02/load-nfs-mounted-ssh-keys-at-login-automatically/</guid><pubDate>Tue, 02 Jun 2020 12:41:27 GMT</pubDate></item><item><title>Save common commands on this server to .useful file</title><link>https://bgstack15.ddns.net/blog/posts/2020/05/29/save-common-commands-on-this-server-to-useful-file/</link><dc:creator>bgstack15</dc:creator><description>&lt;p&gt;Command line productivity takes many forms. Some people live by bash
autocompletion. I disdained it when I first started using bash (coming from
ksh93) but over time realized that it's a fantastic method for discovery (it's
much more complete on Debian-based distros than Fedora-based ones) and ease of
use when typing long pathnames. When you manage a disparate set of systems,
and there are some tasks you repeat often, you usually try to list history and
find the useful commands. One of mine:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;sudo&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="k"&gt;var&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;storage1&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;shares&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;public&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;www&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;repo&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;devuan&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;deb&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;update&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;devuan&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;deb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sh&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But over time, I have many commands I log in for, just to run the one command,
and log out. So I have started using a text file: &lt;strong&gt;~/.useful&lt;/strong&gt;. Inside this
file, place your oneliners. So whenever you log onto a box, and you need to
see what commands you run here commonly, check what you've saved:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;cat ~/.useful
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Of course, it's only useful if you bother to populate it.&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;echo&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'vi $( newest /var/log/debmirror/ log )'&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;~/.&lt;/span&gt;&lt;span class="n"&gt;useful&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;</description><category>autocomplete</category><category>bash</category><category>cli</category><category>oneliner</category><category>productivity</category><guid>https://bgstack15.ddns.net/blog/posts/2020/05/29/save-common-commands-on-this-server-to-useful-file/</guid><pubDate>Fri, 29 May 2020 12:34:11 GMT</pubDate></item><item><title>Load your bash profile even when switching user</title><link>https://bgstack15.ddns.net/blog/posts/2018/04/07/load-your-bash-profile-even-when-switching-user/</link><dc:creator>bgstack15</dc:creator><description>&lt;p&gt;If you use a shared application account, for example, &lt;strong&gt;ansible&lt;/strong&gt; , and want
your profile to be used even after switching to that user, here is a solution
for you. Add this to the destination user &lt;strong&gt;~/.bashrc&lt;/strong&gt;.&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;echo "&lt;span class="cp"&gt;${&lt;/span&gt;&lt;span class="n"&gt;SSH_CLIENT&lt;/span&gt;&lt;span class="cp"&gt;}&lt;/span&gt;" | grep -q '192.168.2.88' &lt;span class="err"&gt;&amp;amp;&amp;amp;&lt;/span&gt; __bp=1
test "$( stat -c '%U' "$( tty 2&amp;gt;/dev/null )" 2&amp;gt;/dev/null )" = "bgstack15" &lt;span class="err"&gt;&amp;amp;&amp;amp;&lt;/span&gt; __bp=1
test "&lt;span class="cp"&gt;${&lt;/span&gt;&lt;span class="n"&gt;__bp&lt;/span&gt;&lt;span class="cp"&gt;}&lt;/span&gt;" = "1" &lt;span class="err"&gt;&amp;amp;&amp;amp;&lt;/span&gt; test -f /usr/bin/bp &lt;span class="err"&gt;&amp;amp;&amp;amp;&lt;/span&gt; . /usr/bin/bp --noclear
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I am using a world-readable copy of my &lt;a href="https://gitlab.com/bgstack15/bgscripts/-/blob/master/src/usr/bin/bp"&gt;bgstack15
profile&lt;/a&gt;,
aka bash profile.&lt;/p&gt;</description><category>bash</category><category>profile</category><category>shell</category><category>su</category><guid>https://bgstack15.ddns.net/blog/posts/2018/04/07/load-your-bash-profile-even-when-switching-user/</guid><pubDate>Sat, 07 Apr 2018 13:29:33 GMT</pubDate></item><item><title>Edit terminal title from the command line</title><link>https://bgstack15.ddns.net/blog/posts/2017/05/29/edit-terminal-title-from-the-command-line/</link><dc:creator>bgstack15</dc:creator><description>&lt;h2&gt;tl;dr&lt;/h2&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="k"&gt;export&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;PROMPT_COMMAND&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'echo -ne "&lt;/span&gt;&lt;span class="se"&gt;\033&lt;/span&gt;&lt;span class="s1"&gt;]0;NEW TEXT HERE&lt;/span&gt;&lt;span class="se"&gt;\007&lt;/span&gt;&lt;span class="s1"&gt;"'&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;Edit terminal title from command line&lt;/h2&gt;
&lt;p&gt;To modify the window title directly, you just need to use this:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;echo -ne "\033]0;NEW TEXT HERE\007"
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But in a normal bash environment, your PROMPT_COMMAND will be executed before
each display of the prompt, so to affect your interactive shell, you will need
that export PROMPT_COMMAND.&lt;/p&gt;
&lt;h2&gt;References&lt;/h2&gt;
&lt;h3&gt;Weblinks&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="https://askubuntu.com/questions/22413/how-to-change-gnome-terminal-title"&gt;https://askubuntu.com/questions/22413/how-to-change-gnome-terminal-title&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</description><category>bash</category><category>oneliner</category><category>xterm</category><guid>https://bgstack15.ddns.net/blog/posts/2017/05/29/edit-terminal-title-from-the-command-line/</guid><pubDate>Mon, 29 May 2017 13:37:58 GMT</pubDate></item><item><title>New user in freeipa has plain bash shell instead of reading .bashrc</title><link>https://bgstack15.ddns.net/blog/posts/2017/04/23/new-user-in-freeipa-has-plain-bash-shell-instead-of-reading-bashrc/</link><dc:creator>bgstack15</dc:creator><description>&lt;p&gt;So you have a new user in freeipa, and he can successfully log in to a freeipa
client. And you know for certain you executed ipa-client-install with the
--mkhomedir option. But when you open a terminal as the new user, it shows you
the boring bash prompt 'bash-4.1$' or whatever version. You checked the
/etc/skel, and it has a valid .bashrc file, and when you dot source your own
~/.bashrc, it then loads the prompt you expect. Here's your issue: do a getent
passwd username. Look at the login shell of the user. It's going to be the
default /bin/sh. Just change it in ipa to be /bin/bash! An sss_cache -E
command was not enough; you have to log out and then back in to have it take
effect. It's probably because the terminal emulator is being called from a
process that was started before the account was changed.&lt;/p&gt;</description><category>bash</category><category>freeipa</category><category>shell</category><guid>https://bgstack15.ddns.net/blog/posts/2017/04/23/new-user-in-freeipa-has-plain-bash-shell-instead-of-reading-bashrc/</guid><pubDate>Sun, 23 Apr 2017 13:24:30 GMT</pubDate></item><item><title>Hide comments and blank lines in file</title><link>https://bgstack15.ddns.net/blog/posts/2016/06/24/show-all-non-blank-non-comment-lines-in-file/</link><dc:creator>bgstack15</dc:creator><description>&lt;h2&gt;Hide comments and blank lines when viewing file&lt;/h2&gt;
&lt;h6&gt;Original title: Show all non-blank non-comment lines in file&lt;/h6&gt;
&lt;p&gt;If you want to see just the lines with content, such as in a config file, use
this one-liner:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;grep -viE '^\s*((#|;).*)?$' smb.conf
&lt;/pre&gt;&lt;/div&gt;

&lt;h4&gt;How it works&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;grep -v&lt;/strong&gt; means invert the selection, i.e., everything that does not match
this search. &lt;strong&gt;-iE&lt;/strong&gt; case Insensitive, and treat this as a regular Expression.
Technically there are no letters being searched, so the i is irrelevant, but I
always use it in my searches anyway. &lt;strong&gt;^&lt;/strong&gt; start of line &lt;strong&gt;\s&lt;/strong&gt;&lt;em&gt; white space,
any amount from zero onward. This is a greedy search, so it will match all the
white space (spaces, tabs, etc.) &lt;/em&gt;&lt;em&gt;(#|;)&lt;/em&gt;&lt;em&gt; either a pound or a semicolon,
which usually denote comments in config files (in my case, smb.conf)
&lt;/em&gt;&lt;em&gt;((#|;).&lt;/em&gt;)&lt;strong&gt; the above sentence, followed by any character (the period), and
any amount of those "any characters." &lt;/strong&gt;((#|;).&lt;em&gt;)?&lt;/em&gt;&lt;em&gt; the whole thing in
parentheses shown here, optionally. &lt;/em&gt;&lt;em&gt;$&lt;/em&gt;* end of line So any line that starts
with any amount of white space, followed by (a comment symbol, followed by
anything else) optionally, and the end of the line. So show everything but the
above sentence, and tada, just the important stuff.&lt;/p&gt;</description><category>bash</category><category>blank</category><category>comments</category><category>config</category><category>grep</category><category>hide</category><category>line</category><category>oneliner</category><category>regex</category><guid>https://bgstack15.ddns.net/blog/posts/2016/06/24/show-all-non-blank-non-comment-lines-in-file/</guid><pubDate>Fri, 24 Jun 2016 18:37:35 GMT</pubDate></item><item><title>Replace config file entry or append if not found</title><link>https://bgstack15.ddns.net/blog/posts/2016/05/31/replace-config-file-entry-or-append-if-not-found/</link><dc:creator>bgstack15</dc:creator><description>&lt;p&gt;Suppose you need to replace whatever current setting for a particular
application to a specific setting. It doesn't matter what it was before, or if
it was absent. You want to define it. Suppose you want to replace the session
pam_mkhomedir setting in /etc/pam.d/common-session to be required. If it was
optional, replace it. If it wasn't listed at all, add it. This line updates an
&lt;a href="http://stackoverflow.com/a/3560072/3569534"&gt;answer on stackoverflow&lt;/a&gt;.&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;sed -i -e '\|session.*pam_mkhomedir.so|h; &lt;span class="cp"&gt;${&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;mkhomedir&lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="p"&gt;;{&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="n"&gt;tF&lt;/span&gt;&lt;span class="cp"&gt;}&lt;/span&gt;;a\' -e 'session\trequired\tpam_mkhomedir.so umask=0022' -e '};:F;s/.*mkhomedir.*/session\trequired\tpam_mkhomedir.so umask=0022/g;' /etc/pam.d/common-session
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;strong&gt;-i&lt;/strong&gt; makes the change inline directly in the file. I write deployment
scripts so I don't care about displaying the output; I want to change the
original. Basically the command looks like this:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;sed&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="s1"&gt;'\|variable.*(possible|values|go|hereornot)|h; ${x;s/variable//;{g;tF};a&lt;/span&gt;&lt;span class="se"&gt;\'&lt;/span&gt;&lt;span class="s1"&gt; -e '&lt;/span&gt;&lt;span class="n"&gt;variable&lt;/span&gt; &lt;span class="n"&gt;updatedvalue1&lt;/span&gt; &lt;span class="n"&gt;updatedvalue2&lt;/span&gt;&lt;span class="s1"&gt;' -e '&lt;/span&gt;&lt;span class="p"&gt;};:&lt;/span&gt;&lt;span class="n"&gt;F&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;/.*&lt;/span&gt;&lt;span class="n"&gt;variable&lt;/span&gt;&lt;span class="o"&gt;.*/&lt;/span&gt;&lt;span class="n"&gt;variable&lt;/span&gt; &lt;span class="n"&gt;thisvalueadded1&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="s1"&gt;' /etc/inputfile&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Questions? I would be happy to explain this if you want a guided tour.&lt;/p&gt;
&lt;h2&gt;References&lt;/h2&gt;
&lt;p&gt;http://stackoverflow.com/a/3560072/3569534&lt;/p&gt;</description><category>bash</category><category>linux</category><category>sed</category><guid>https://bgstack15.ddns.net/blog/posts/2016/05/31/replace-config-file-entry-or-append-if-not-found/</guid><pubDate>Tue, 31 May 2016 16:12:22 GMT</pubDate></item></channel></rss>