Bash Advanced Topics: Difference between revisions

From MediaWiki
Jump to navigationJump to search
(Created page with '__TOC__ === systemctl Bash Completion === The '''[http://www.gnu.org/software/bash bash]''' shell's completion function can be extended to external programs. The '''[http://www.…')
 
 
(25 intermediate revisions by the same user not shown)
Line 1: Line 1:
__TOC__
__TOC__
== '''Overview''' ==
The topics below cover various advanced '''[http://www.gnu.org/software/bash bash]''' shell usage that can be applied to NST when working at the command line. See the '''[http://tldp.org/LDP/abs/html/ Advanced Bash-Scripting Guide]''' for an excellent document on advanced shell scripting.
== '''Extended Bash Completion''' ==
The bash shell's completion functionality can be extended to external programs.


=== systemctl Bash Completion ===
=== systemctl Bash Completion ===
The '''[http://www.gnu.org/software/bash bash]''' shell's completion function can be extended to external programs. The '''[http://www.freedesktop.org/wiki/Software/systemd systemd]-units''' RPM package contains a bash completion script ("/etc/bash_completion.d/systemctl-bash-completion.sh") for the '''[http://0pointer.de/public/systemd-man/systemctl.html systemctl]''' command. Bash completion extension for the systemctl command can be quite useful for its long command argument's naming. To enable it, source it in within your bash shell:
The '''[http://www.freedesktop.org/wiki/Software/systemd systemd]-units''' RPM package contains a bash completion script ("/etc/bash_completion.d/systemctl-bash-completion.sh") for the '''[http://0pointer.de/public/systemd-man/systemctl.html systemctl]''' command which allows controlling of the '''systemd''' system daemon and '''service manager'''. Bash completion extensions for the systemctl command can be quite useful due to its long command arguments. To enable it, source the script in at the bash shell:
<div class="screen">
<div class="screen">
<div class="userInput"><span class="prompt">[root@probe ~]# </span>. /etc/bash_completion.d/systemctl-bash-completion.sh</div>
<div class="userInput"><span class="prompt">[root@probe ~]# </span>. /etc/bash_completion.d/systemctl-bash-completion.sh</div>
Line 22: Line 28:


'''***Note:''' If more than one completion value is found, then hitting the tab key will not complete and fill in a value. In this case one may use the '''<esc>''' + '''=''' key sequence (i.e., The Escape key  followed by the Equal Sign key) to find out the possible completion values.
'''***Note:''' If more than one completion value is found, then hitting the tab key will not complete and fill in a value. In this case one may use the '''<esc>''' + '''=''' key sequence (i.e., The Escape key  followed by the Equal Sign key) to find out the possible completion values.
== Starting the Bash Shell for a '/sbin/nologin' User ==
One can use the '''[http://en.wikipedia.org/wiki/Su_%28Unix%29 su]''' command to run the bash shell for a user that has a "'''/sbin/nologin'''" login shell. This can be handy to see if a user has specific system permissions. Below is an example for the "'''apache'''" user:
<div class="screen">
<div class="userInput"><span class="prompt">[root@probe ~]# </span>cat /etc/passwd | grep apache</div>
<pre class="computerOutput">
apache:x:48:48:Apache:/var/www:/sbin/nologin
</pre>
<div class="userInput"><span class="prompt">[root@probe ~]# </span>su - apache</div>
<pre class="computerOutput">
This account is currently not available.
</pre>
<div class="userInput"><span class="prompt">[root@probe ~]# </span>su - apache -s /bin/bash</div>
<div class="userInput"><span class="prompt">[apache@probe ~]$ </span>id -a</div>
<pre class="computerOutput">
uid=48(apache) gid=48(apache) groups=48(apache),0(root),486(backuppc),499(nagios)
[apache@probe ~]$
</pre>
</div>

Latest revision as of 09:02, 26 May 2011

Overview

The topics below cover various advanced bash shell usage that can be applied to NST when working at the command line. See the Advanced Bash-Scripting Guide for an excellent document on advanced shell scripting.

Extended Bash Completion

The bash shell's completion functionality can be extended to external programs.

systemctl Bash Completion

The systemd-units RPM package contains a bash completion script ("/etc/bash_completion.d/systemctl-bash-completion.sh") for the systemctl command which allows controlling of the systemd system daemon and service manager. Bash completion extensions for the systemctl command can be quite useful due to its long command arguments. To enable it, source the script in at the bash shell:

[root@probe ~]# . /etc/bash_completion.d/systemctl-bash-completion.sh
[root@probe ~]# 

To use it, lets say to get the status of a serial device (systemctl status serial-getty@ttyS0.service), perform the following:

systemctl stat<tab>

Substitute the "<tab>" for the tab key. This will result in:

systemctl status

Now the next part of the command:

systemctl status seria<tab>

Again substitute the "<tab>" for the tab key. This will result in the full command:

systemctl status serial-getty@ttyS0.service

***Note: If more than one completion value is found, then hitting the tab key will not complete and fill in a value. In this case one may use the <esc> + = key sequence (i.e., The Escape key followed by the Equal Sign key) to find out the possible completion values.

Starting the Bash Shell for a '/sbin/nologin' User

One can use the su command to run the bash shell for a user that has a "/sbin/nologin" login shell. This can be handy to see if a user has specific system permissions. Below is an example for the "apache" user:

[root@probe ~]# cat /etc/passwd | grep apache
apache:x:48:48:Apache:/var/www:/sbin/nologin
[root@probe ~]# su - apache
This account is currently not available.
[root@probe ~]# su - apache -s /bin/bash
[apache@probe ~]$ id -a
uid=48(apache) gid=48(apache) groups=48(apache),0(root),486(backuppc),499(nagios)
[apache@probe ~]$