Showing posts with label SPR720. Show all posts
Showing posts with label SPR720. Show all posts

Monday, September 22, 2008

SPR720 - BASH Scripting Lab

BASH (Bourne Again SHell) is a command processor for POSIX operating systems such as Linux. Not only does it provide a command line interface, it provides scripting support by letting me place commands into a text file for later execution. BASH and its scripts give access to redirection, pipes, regular expressions and the panoply of Linux command line utilities. This is indeed a very powerful set of tools. As the possibilities of the command line open up to me I'm finding very little I can't find out about or get done inside the shell. But when I try to write scripts I find the language to be non-intuitive and just plain difficult. While there are many things that make BASH difficult for me, two stand out:
  • It's variables are untyped or (for practical purposes) just one type - string. In order to use numeric values stored as strings, there are some cryptic work-arounds.
  • 0=TRUE. In every other programming language I've used, it's the opposite. It reminds me of a scene from a movie called "The Gods Must Be Crazy" where in an African tribe, people nodded their heads to indicate "no" and shook their heads to mean "yes". At least once a script I get tripped up by this. I know it's because the Linux exit status for "job well done" is 0, but it's still a drag.
Sometimes the difficulty is compounded when both of these "gotchas" come together in forming conditionals or when evaluating strings and numbers inside the same complex expression. It's enough to make me chew the inside of my cheek. But thankfully the Internet came to my rescue, again. I found some great resources that made the process of scripting bearable, if not enjoyable. I hate to admit it, but without these resources I would not have finished my lab. So my thanks to their creators and contributors.

Here's a sample script I did for the lab:

#!/bin/bash
#
# lsname.bash # This script prints out the longest and shortest names in /etc/passwd
#


SNAME="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

LNAME=""

while IFS=: read USER PASS USERID GID GECOS HOME SHELL
do
if test ${#USER} -gt ${#LNAME}
then

LNAME=$USER
fi

if test ${#USER} -lt ${#SNAME}
then
SNAME=$USER
fi
done < /etc/passwd


echo "Longest name in /etc/passwd is $LNAME - ${#LNAME} characters"
echo "Shortest name in /etc/passwd is $SNAME - ${#SNAME} character"


---
Bash Resources
http://tldp.org/LDP/abs/html/index.html
http://wooledge.org:8000/BashFAQ
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html

Btw, this post is not meant to be flame bait. I just needed to vent a little.

Wednesday, September 10, 2008

SPR720 Command Lab - Examining core Linux directories and commands

I've spent some time in the last week examining the /bin, /sbin, /usr/bin and /usr/sbin directories on my Ubuntu Linux box and my first conclusion is that a week is not nearly enough time to gain a clear understanding of their contents. Thus I turned to the Internet to get help on the subject and found the following links helpful:

http://www.tuxfiles.org/linuxhelp/linuxdir.html
http://doc.vic.computerbank.org.au/tutorials/linuxdirectorystructure/

/bin - 106 files
The /bin directory contains programs that provide basic text and file manipulation along with basic system information. These programs are so basic that they are used by both end user and system administrator alike. Examples of programs in this directory include ls, pwd, rm, cp, bash, su, mkdir, rmdir and touch. One thing I notice is that Gnome and KDE (among other graphical shells) are evolving to the point where even these basic progams will not be needed by casual users.

/sbin - 153 files
The /sbin directory contains the most basic and most critical system configuration programs. Anyone who's had to struggle with installing Linux will have had to learn some of these commands. They include mkfs, fsck, parted, hdparm for disk manipulation; ifconfig, ifup, ifdown, iptables, route and others for network management; sysctl, lsmod and modprobe for kernel information and control.

/usr/sbin - 236 files
This directory contains many essential tools for system administration. While they are not essential for getting a system up and running, they are essential for getting it set up properly. User and group manipulation commands include: adduser, addgroup, deluser, delgroup. It
also contains programs to control system programs called daemons including: cupsd, cron, ntpd, tcpd.

/usr/bin - 1279 files
This directory contains end-user applications and numerous tools and utilities. To start with, just about every application one can invoke from the Gnome menus can be invoked from the command line in this directory. Many supplemental programs related to the "menu-available" applications but which are not available from the menus are in here too. Finally there are many small but powerful and useful utilities. Here's a small list of some of the programs I found interesting:

Sound Control - alsa, alsamixer, amixer, amidi, pacmd, pactl, paplay

Software Installation - apt-get, aptitude

Batch Command Execution - at, batch

System Settings / System Information - charset, charmap, free, getconf, getkeycodes, locale, lspci, peekfd, pmap, w

CD/DVD Creation - cdrdao, cdrecord

Miscellaneous Tools
bc - a powerful calculator
expand - converts tabs to spaces
find - find files
file - tries to determine a file type
fmt - text formatter
fold - text wrapper
head - display the top of a file
tail - display the end of a file
join - joins files together
ftp, lftp, sftp - file transfer programs
lp - prints files
man - displays system documentation
nice - run a program with modified scheduling priority
passwd - change user password
paste - merge lines of files together
pg - browse pagewise through text files
sort - sorts text files
split - splits a file into pieces
ssh - securely login to another system over the network
watch - runs a program and occasionally displays the output
xxd - make a hex dump of a file
yelp - a graphical help browser that can display and navigate info files


Some final notes:

There are many symbolic links in the directories. These serve to give access to the same program under many different names. The reason for doing this is to preserve compatibility between current and older version of Linux. It also provides some compatibility between the Ubuntu/Debian flavour of Linux and other Linuces and Unices.

The above lists are not meant to be comprehensive. They represent a snapshot of what I now know and am interested in. Please bear this in mind.