Linux Training : d. bash

bashshell">bash shell

  • command line environment.

    bash is a shell environment. It is literally a place inside a running application called bash that waits for the user to provide commands to execute. bash is the primary shell environment for RHEL systems. It has a rich set of scripting capabilities and thus is a major tool used for sysadmins in Linux. There are default configuration files for all users in /etc/bashrc as well as an individual one in ~/.bashrc. The system-wide bashrc file is read first and the user .bashrc file is last to overwrite any environment variables the user chooses. The see what is in the user environment run env. The root user, when logged in directly at a console, has a different environment. In particular, the ordinary user's PATH does not contain /sbin or /usr/sbin for good reasons.
    [jkinney9@sis-jpk ~]$ echo $PATH
    /usr/lib64/qt-3.3/bin:/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/users/jkinney9/bin
    [jkinney9@sis-jpk ~]$ su jimroot
    Password:
    [jimroot@sis-jpk jkinney9]# echo $PATH
    /usr/kerberos/sbin:/usr/lib64/qt-3.3/bin:/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/users/jkinney9/bin
    [jimroot@sis-jpk jkinney9]# su - root
    [jimroot@sis-jpk ~]# echo $PATH
    /usr/lib64/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
    
    In the above example, the ordinary user, jkinney9, has a PATH that ends with the users bin directory.
    But after the su jimroot, the path was prepended with the /usr/kerberos/sbin. By using the su (switch user) command without the "-" or "-l", the new user inherits the originals user's environment except for a rewritten HOME and SHELL (and USER and LOGNAME if the new user is not root (see info su for more details).
  • init scripts, cron jobs and general automation

    The RHEL init scripts are all bash scripts. They are a good example of pretty decent bash scripting.
    cron jobs are often bash scripts.
  • Links to extensive bash programming guides

    Intro to bash scripting
    Advanced bash scripting guide
Unknown macro: {bgcolor}

Write a bash script to show the files that are different between the runlevels 1 and 5 at startup. Display the data as filename runlevel and sorted in reverse starting order.

Other scripting languages

The other two major languages for the Linux (and others!) environment

  • Python

  • Perl

    While there are many other scripting languages, most of the others are geared for web environments.
    PERL and Python both are excellent for the web environment as well as general purpose scripting and full scale application development.
  • lisp

bash answer

bash script

Write a bash script to show the files that are different between the runlevels 1 and 5 at startup. Display the data to a file as filename runlevel and sorted in reverse starting order.

diff /etc/rc1.d/ /etc/rc5.d/ | sed -e 's|^Only in /etc/rc||' -e 's|\.d/:||' \
| grep " S" | awk '{printf("%-20s %1d\n",$2, $1)}' |sort -r > /tmp/r5list

How this works

  • diff /etc/rc1.d/ /etc/rc5.d/
    compares the files in both directories and sends a list of differences to STDOUT. That list looks like
    --snip--
    Only in /etc/rc1.d/: K99readahead_early
    Only in /etc/rc1.d/: K99readahead_later
    Only in /etc/rc5.d/: S00microcode_ctl
    Only in /etc/rc1.d/: S02lvm2-monitor
    --snip--
    
  • The "|" redirects STDOUT to STDIN for the next section.
  • sed -e 's|^Only in /etc/rc||' -e 's|\.d/:||'
    starts by removing the text in front of the runlevel and then it removes the trailing ".d:" after it. There is only one invocation of sed as each execution (-e) reuses the same instance. Note the "." has to be escaped with the "\". Also note the use of the "|" characters as the sed separators. This is because we are dealing with lines that have "/" characters already and it easier to read this way.
    1 K99readahead_early
    1 K99readahead_later
    5 S00microcode_ctl
    1 S02lvm2-monitor
    
  • The "\" at the end of the line is a "continue" character
  • grep " S" outputs only the lines with a capital "S" preceded by a single space to capture the startup scripts
    5 S00microcode_ctl
    1 S02lvm2-monitor
    5 S04readahead_early
    5 S05kudzu
    
  • awk '{printf("%-20s %1d\n",$2, $1)}'
    uses some formatted printing %20s is a 20 character wide string that is left justified (the "" before the "20s") followed by a single space then a 1 digit wide number followed by a newline character. The values for those fields are, in order, the second word of the space-delimited line and the first word.
  • sort -r performs a reverse order sort on the final STDIN and prints it to STDOUT which is redirected to the file /tmp/r5list.

The sed and grep parts could be combined as

sed 's|.\+rc\([[:digit:]]\)\.d/: \(S.\+\)|\1 \2|'

using the pattern memory ability of sed. If the \1 \2 were changed to be \2 \1, the awk section could be dropped (but it wouldn't print as nicely).


LIN:return