Interprets Commands
Linux Commands - Shells

When you type a command in the shell it interprets the command and then executes it.  This is a powerful way of making Bash work for you.  All of the features of Bash are at your fingertips because you are able to control it with your commands.  It is important to have an understanding on how to implement the commands correctly.

Lesson 1 / Lesson 3

 

Structure of a Command
Commands can consist of up to three parts, and there's a certain order for the parts.  The parts--and the order that you'll place them in are:

command options arguments

Note: Bash does not require a suffix to filenames, though they are often added for clarification.

The shell interprets commands, not the command.  In the illustration you can see that as the command is interpreted by the shell results are pushed to the screen.

shell

There are some exceptions to the ordering rule.  For certain commands, in certain cases, you'll be able to place an option at the end of the command, after the argument.  Look at the humble ls utility.  Options and arguments are optional for this utility, so we'll get to see the different configurations for the command.

First,  issue the naked ls command in order to see the files that are in our current directory.

    ls
     data                              newshare              share
    

Add an option.  Use the -l option to show the files with some of their characteristics.

    ls -l
total 1328
drwxr-xr-x  3 root root     4096 2009-05-22 06:21 acpi
-rw-r--r--  1 root root     2986 2009-03-27 18:16 adduser.conf
-rw-r--r--  1 root root       45 2009-07-15 21:00 adjtime
-rw-r--r--  1 root root      194 2009-03-27 19:23 aliases
-rw-r--r--  1 root root    12288 2009-07-13 16:34 aliases.db
drwxr-xr-x  3 root root     4096 2009-03-27 19:19 alsa
drwxr-xr-x  2 root root     4096 2009-07-02 06:26 alternatives
-rw-r--r--  1 root root      395 2008-03-09 13:58 anacrontab
   ---cut---

Use the ls command with the -a option to see any hidden files or directories.  (That is, files or directories whose names begin with a period.)  

    ls -a
    .                                 .local
    ..                                mbox
    .bash_history                     .mcop
    .bash_logout                      .mcoprc
    .bash_profile                     .metacity
    .bashrc                           .mozilla
---cut---

You can also combine the options.

         ls -la
   rw-------  1 z4   z4      11571 2009-07-15 20:59 .bash_history
-rw-r--r--  1 z4   z4        220 2009-03-27 19:48 .bash_logout
-rw-r--r--  1 z4   z4       3293 2009-03-28 06:32 .bashrc
drwxr-xr-x  5 z4   z4       4096 2009-03-27 16:51 bk_usb
drwxr-xr-x  2 z4   z4       4096 2009-05-22 21:50 .bluefish
drwxr-xr-x  2 z4   z4       4096 2009-06-25 15:21 bridge
-rw-r--r--  1 z4   z4      10214 2008-09-25 11:03 build.odt
---cut---

Now, suppose you only want to see certain types of files.  In this case, you only want to see files that end with a ".txt" extension.  This is where you add the argument.

        ls -l *.txt
-rw-r--r-- 1 z4 z4 297 2009-04-28 22:46 class.txt
    

Even though you want to remember the preceding rule for certification exam purposes, there are exceptions to it.  For example, with the ls utility, you can divide the options so that some appear before the argument, and some appear afterward.  You'll still get the same output as if you had entered the command "correctly".

        ls -a s* -l
-r--r--r-- 1 z4 z4    12374 2008-05-09 13:20 selinux_2.odt
-rw-r--r-- 1 z4 z4    35299 2008-08-14 19:13 sendmail.cf
-r--r--r-- 1 z4 z4    31449 2008-05-09 13:20 shoutcast.pdf
-rw-r--r-- 1 z4 z4      997 2008-05-27 16:24 squid.rtf
-rw-r--r-- 1 z4 z4    19569 2008-12-30 20:44 ssl.odt
-r--r--r-- 1 z4 z4 76006604 2008-05-09 13:22 student.pdf
 
scripts:
total 72
drwxr-xr-x  2 z4 z4 4096 2009-07-13 19:03 .
drwxr-xr-x 91 z4 z4 4096 2009-07-16 15:18 ..
-rw-r--r--  1 z4 z4 2865 2009-04-12 18:40 bk2
 
servers:
total 40
drwxr-xr-x  4 z4 z4  4096 2009-03-27 16:58 .
drwxr-xr-x 91 z4 z4  4096 2009-07-16 15:18 ..
-rw-r--r--  1 z4 z4 13193 2008-08-02 05:59 gateway.png
-rw-r--r--  1 z4 z4  5770 2008-12-14 10:37 htaccess
    

Multiple Arguments
Sometimes, you may have to issue commands with multiple arguments, as well as with multiple options. If  you need to use the tar utility to create an archive file that you want to name "myfile.tar".  You'll be adding to it the files myfile_1, myfile_2, and myfile_3.  So, you'll have a command with four arguments.  How does bash determine which argument is the archive file, and which arguments are the files to be added?  You'll use the "f" switch just before the argument for the archive file that you want to create.  

    tar cf myfile.tar myfile_1 myfile_2 myfile_3
    tar -cf myfile.tar myfile_1 myfile_2 myfile_3

Note that in this case, it doesn't matter whether you place a "-" in front of the switches.  It's optional here because the tar utility expects that a switch will be used in the command.  With ls, switches are optional, so you have to use the "-" in order for ls to know that you really are including a switch.

There are several ways that you can execute more than one command at once.

Interactively
This is a form of shell-script programming, except that you're just executing all commands from the command-line, instead of actually writing, saving, and executing a script.    Here, we're creating a "for" loop--with each command on its own separate line--to perform three directory listings.

    for var in arg1 arg2 arg3
    > do
    > echo $var
    > ls
    > done

You don't have to put each command on its own line.  You can also put them all on one line, and separate them with semi-colons.

     for var in arg1 arg2 arg3; do echo $var; ls; done


Command Sequences

In addition to creating programming structures, you can also use the semi-colon to separate stand-alone commands that you want to execute from the same command entry.  If you wanted to cd to a certain directory and then look at its contents, you could enter each command on its own line.  Or, you could enter them both on the same line.

    cd /var;ls
    backups  crash  lib    lock  mail  run    tmp  yp
    cache    games  local  log   opt   spool  www
    

You can also instruct bash to only execute the second command if the first command successfully completes.  Just separate the commands with an "&&" instead of with a semi-colon.

    cd /var && ls
    backups  crash  lib    lock  mail  run    tmp  yp
    cache    games  local  log   opt   spool  www
    


What if the first command doesn't run successfully?  Note here how the second command doesn't execute.

    cd /far && ls
    -bash: cd: /far: No such file or directory
    
If you want bash to execute the second command only if the first command doesn't run successfully, just separate the commands with a "||".  To illustrate, let's make a slight typo while trying to change directories.

    ce /var || echo "This command didn't work."
    -bash: ce: command not found
    This command didn't work.

Now, correct our typo.

    cd /var || echo "This command didn't work."   


This time, the second command didn't execute.

For a more practical example, try changing to a directory, creating it if it doesn't exist, and then changing to it after it's been successfully created.

    cd mydirectory || mkdir mydirectory && cd mydirectory
    -bash: cd: mydirectory: No such file or directory

You'll still get a bash error message saying that the directory you tried to change to doesn't exist.  But, look at the command prompt, and you'll see that the directory has been created, and that you're now in it.


You can access bash through a  terminal emulator which is a graphical-type window that you can open and use to access the bash command-line.  It is built with scroll bars, drop-down menus to customize your view, and the ability to copy screen output to the system clipboard.  There are several different terminal emulators available.  Which one you use will probably depend on which desktop environment you choose.  If you use a distro that runs with the Gnome desktop environment, you'll probably be using the "gnome-terminal".  

Open a terminal and look at the prompt, it should look something like this:

mike@wk:~>

It will list the user's name and the machine that they are on.  It is ready to accept commands and pass them to the kernel.

 


Copyright CyberMontana Inc. and BeginLinux.com
All rights reserved. Cannot be reproduced without written permission. Box 1262 Trout Creek, MT 59874