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 runenv
. 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.In the above example, the ordinary user, jkinney9, has a PATH that ends with the users bin directory.[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
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 (seeinfo 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
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
- Lisp is a major player in the EMACS editing tool. It is often poked fun at
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
|
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