4774.cc

Introduction to the BASH Shell By Anthony Weitekamp

A short history of Linux Shell Programs

Unix and Linux has enjoyed broad development by many people with different ideas throughout history. This has led to the development of many programs, each with their own names, that do the same or very similar things. The Bourne Shell (sh) is one of many shell programs that allow the user to execute utlilties and administer their systems. The first major UNIX shell was the Bourne Shell (created by Steven Bourne). It has remained essentially unchanged since its initial release and many administrative features and UNIX utilities still depend on it.

As an alternative to the Bash Shell the C Shell (csh), written by Bill Joy, features commands that are very similar to the C programming language. This makes it much easier for programmers on UNIX to learn.

Bash (Bourne Again Shell) was created as part of the original GNU project in tribute to its original creator, Steven Bourne. You can check to see if you have the bash shell installed on your Linux computer by viewing the bash configuration file through typing the following command in a terminal window.
 $ less .bashrc 
To exit the less command, hit the letter q. We will cover "less" and more in the next section.

Basic Linux Commands

No discussion on shell progrmming would be complete without covering some basic Linux commands. Here is a short list that will help you move around and perform some basic administrative tasks on your system.
apropos Search the manual pages and descriptions
apt-get Command line interface for the APT package handling utility
cd change directory
chmod Change file mode bits
clear Clear the terminal screen
less View File Contents
man an interface to the on-line reference manuals
more View File Contents
mv move a file
rm Remove a file
sudo Execute a command as root (administrator)
vim Command line text editor


I will not go very in depth with most of these commands. Their inclusion in this section is intended to give the reader a brief overview of how to use basic commands on the command line and in scripts intended to make administration and system maintenance a bit easier. Let's start with the command apropos. To search your manual database for pages that contain the keyword apropos simply type this on the command line.
 $ apropos apropos 
'apropos' will search the database of man pages and return all instances found. In this case, it returns only one line. Look at the man page for apropos.
 $ man apropos  
Pay particular attention to the options available to you as you scroll through the man page. Now type the following line on the command line.
 $ apropos grep  
Notice that the search returned a very long list of commands that have the keword 'grep' included in it somewhere. Let's clean it up using the -e option which will match our keword more exactly.
 $ apropos -e grep  
The next command that I will cover is apt-get. Get the man page from the command line by typing the following.
 $ man apt-get  
'apt-get' is the command line package handling utility in Debian Linux and its downstream distributions. Other Linux distributions use another pachage handler called yum by default. This discussion covers only concerns Debian and its downstream distrubutions. As you can see from the man page, apt-get is a very powerful tool for updating and maintaining your software lists. Any time you execute this command you will need root or administrator permissions. In Linux, the administraor is called root. You will use the 'sudo' command in front of apt-get every time you execute the command. Type "man sudo" on the command line to read a detailed description of what 'sudo' does. We will not use any of the options for sudo during this lesson.

Let's get ready to make sure our computer's software is up to date using only the command line. First type and execute the following two (2) commands.
 $ sudo apt-get update 
 $ sudo apt-get upgrade 
You may be prompted for a yes or no input if there are updates available for your computer. You can run apt-get wth a default yes (-y option) answer and it will simply install all of the updates wthout asking you about each one.

Now check to see if you have vim installed on your computer by typing vim on the command line. We will use vim to create a script that will perform updates for us. Vim is a command line editor program with navigation features very similar to less. Vim is not part of your default Linux installation so you may have to install it. If you received an error message when you typed vim on the command line, install it using apt-get.
 $ sudo apt-get install -y vim 
You are ready to create your first script from the command line. Type the following commands.
 
$ cd 
$ vim DoUpdates
	
You are creating the script in the top level of your /home/ folder or /home/username/. You guessed it! This will help you update your machine whenever you want to. Look at the bottom of your terminal window. You should see
"DoUpdates" [New File]          0,0-1         All
This is the area that shows you what mode you are in. Right now you are in the command mode. Hit the "insert" key on your keyboard one time to enter --INSERT-- mode. If you hit it twice and get --REPLACE-- mode, don't worry. Hit the insert key again and you will see the mode toggle between insert and replace modes. Now type the following lines into the window taking note of the first line in the script. This is called a "shebang line". Your script may or may not work without it depending on your system's configuration. The shebang line is the absolute path to the interpreter you want to use.
#!/bin/bash
sudo apt-get -y update
sudo apt-get -y upgrade
	
Now hit the "Esc" or escape key to move back into command mode. You should see a blank line where "--INSERT--" was displayed. Now type ":wq". The mnemonic is write quit. There is a lot more to using vim than what is covered here, but that is a lesson completely unto itself. Untill then, I suggest you read the man page. Once you get used to it, you may not want to use anything else. Hit the "Enter" key. Now try typing the following on the command line:
$ ./DoUpdates 
You will get an error something like "bash: ./DoUpdates: Permission denied". You must change the file mode bits to make the script an executable.
$ chmod +x DoUpdates 
Now try running your script again.
$ ./DoUpdates 
You will be prompted for your administrative or root password, and poof.... The script takes off and retrieves the list of updates and installs them for you. You may want to create a desktop icon if you find this handy as a way of doing updates. Desktop shortcuts will be covered in another HowTo topic.

Now it is time to look around at our file and directory structure and maybe do some housekeeping. To do this we will use the cd, ls, less, mv, and rm commands. We will also touch on the more command, but you will find it clunky and hard to use. Type the following on the command line and press enter get a listing of all of the files in the folder that are not hidden.
$ ls
ls command

Now get a listing of all of the files, including their permissions.
$ ls -al 
ls command

Most people have seen hidden files that contain configuration information for various applications in other operating systems. Linux has hidden files as well, however unlike NTFS, all it takes to hide a file from the casual user in Linux is to put a period in front of the filename. Let's hide our DoUpdates script from above.
$ mv DoUpdates .DoUpdates
Now we must use the dot or period in front of the filename in order to execute the command.
$ ./.DoUpdates 
Can you have "doupdates". "doUpdates", and "DoUpdates" in the same folder? Sure, because all file names in Linux are case sensitive. Let's change our filename for the update script.
$ mv .DoUpdates  doUpdates
To rename a file, you move it from one filename to another just as you could move it from one folder to another. Let's move the file to the Documents folder.
$ mv doUpdates Documents/
Now view the contents of your command history by typing the following command.
$ less .bash_history
Now to move around the file and view the contents. Watch what happens when you type each of the commands in the table below.
1gg Go to line 1 in the file
100% Go to the end of the file
50% Go to the middle of the file
/DoUpdates Look for "DoUpdates"
Arrow Up Scroll up one line at a time
Arrow Down Scroll down one line at a time
Page Up Scroll up a page at a time
Page down Scroll down a page at a time
q quit
View the contents of your command history using more.
$ more .bash_history
Now to move around the file and view the contents. Watch what happens when you type each of the commands in the table below.
1gg Nothing happens
100% Nothing Happens
50% Nothing Happens
/DoUpdates Look for "DoUpdates"
Arrow Up Scroll up one line at a time
Arrow Down Scroll down one line at a time
Page Up Scroll up a page at a time
Page down Scroll down a page at a time
q quit


Look for next installment on the bash shell, coming soon.