Bourne shell first argument relationship

Ch 9 -- Bourne Shell

The shell command and any arguments to that command appear as cat myscript #!/bin/bash echo "First arg: $1" echo "Second arg: $2" $. If the first argument (argument 0) to the shell is a dash ('-'), then csh is run as This may be used to pass options to a shell script without confusion or .. and then tested to see if it has the specified relationship to the real user. If the first line of your shell script is blank, most versions of Unix will use parameters you passed 0 parameters the first parameter was the . The following example demonstrates the relationship between the two commands.

Interactive features aren't much use in scripts. There are also some features the Bourne shell has that the C shell doesn't have. Shell Concepts What is a shell, anyway? The UNIX operating system is a complex collection of files and programs. UNIX does not require any single method or interface.

Many different techniques can be used. The oldest interface, which sits between the user and the software, is the shell. Twenty five years ago many users didn't even have a video terminal. Some only had a noisy, large, slow hard-copy terminal. The shell was the interface to the operating system. Shell, layer, interface, these words all describe the same concept.

By convention, a shell is a user program that is ASCII based, that allows the user to specify operations in a certain sequence. There are four important concepts in a UNIX shell: It can be, and is, used to prototype new programs. The last two points are important. DOS does not have a shell that has as many features as the Bourne shell.

Also, it is impossible to write a DOS script that emulates or replaces existing commands. Let me give an example. OLD" will be renamed "A. If you wanted to create a new command, it too, would not understand that an asterisk is used to match filenames.

You see, each DOS program is given the burden of understanding filename expansion. Consequently, many programs do not. UNIX is a different story. The shell is given the burden of expanding filenames. The program that sees the filenames is unaware of the original pattern. This means all programs act consistently, as filename expansion can be used with any command. It also means a shell script can easily replace a C program, as far as the user is concerned.

If you don't like the name of a UNIX utility, it is easy to create a new utility to replace the current program. If you wanted to create a program called "DIR" you could simply create a file containing! It is possible to do software development using the shell as the top level of the program. Not only is it possible, but it is encouraged. The UNIX philosophy of program development is to start with a shell script. Get the functionality you want.

Linux csh command

If the end results has all of the functionality, and is fast enough, then you are done. If it isn't fast enough, consider replacing part or all of the script with a program written in a different language e. Just because a UNIX program is a shell script does not mean it isn't a "real" program.

Verifying which shell you are running Because there are many shells available, it is important to learn to distinguish between the different shells. Typing commands for one shell when you are using another is bound to cause confusion. I know from personal experience. This was aggravated by the fact that many books I used to learn UNIX never mentioned that other shells existed.

Substituting the Results of Commands in a Command Line Sometimes it is useful to pass the output or results of one command as arguments to another command. You use the back quotation marks in pairs. When the shell sees a pair of back quotation marks, it executes the command inside the quotation marks and substitutes the output of that command in the original command line.

You most commonly use this method to store the results of command executions in variables. To store the five-digit Julian date in a variable, for example, you use the following command: Back quotation marks can be extremely useful when you're performing arithmetic on shell variables; see "Shell Programming" later in this chapter. Shell Variables In algebra, variables are symbols that stand for some value.

In computer terminology, variables are symbolic names that stand for some value. Earlier in this chapter, you saw how the variable HOME stood for the name of a user's home directory. If you enter the change directory command, cd, without an argument, cd takes you to your home directory. Does a generic program like cd know the location of every user's home directory? Of course not, it merely knows to look for a variable, in this case HOME, which stands for the home directory. Variables are useful in any computer language because they allow you to define what to do with a piece of information without knowing specifically what the data is.

A program to add two and two is not very useful, but a program that adds two variables can be, especially if the value of the variables can be supplied at execution time by the user of the program. The Bourne shell has four types of variables: Storing Data or User-Defined Variables As the name implies, user-defined variables are whatever you want them to be. Variable names are comprised of alphanumeric characters and the underscore character, with the provision that variable names do not begin with one of the digits 0 through 9.

Like all UNIX names, variables are case sensitive. Because most UNIX commands are lowercase words, shell programs have traditionally used all capital letters in variable names. It is certainly not mandatory to use all capital letters, but using them enables you to identify variables easily within a program.

You can remove the value of a variable using the unset command, as follows: With the Bourne shell, you can cause variable substitution to take place only if certain conditions are met. This is called conditional variable substitution. Substituting Default Values for Variables As you learned earlier, when variables that have not been previously set are referenced, a null value is substituted. Another substitution construct not only substitutes the default value but also assigns the default value to the variable as well.

If the substitution is made in a shell program, the program immediately terminates. If the executable is a shell program, the arguments are passed to the program as positional variables. Notice that the names of the variables are actually the digits 1 through 9; the dollar sign, as always, is the special character that causes variable substitution to occur.

Positional variables are discussed later in this chapter in the section "Shell Programming. This ensures that a variable won't be accidentally changed. Making Variables Available to Subshells with export When a shell executes a program, it sets up a new environment for the program to execute in. This is called a subshell. In the Bourne shell, variables are considered to be local variables; in other words, they are not recognized outside the shell in which they were assigned a value.

You can make a variable available to any subshells you execute by exporting it using the export command.

Your variables can never be made available to other users. Now suppose you start a new shell. When you started a new shell, the default shell prompt appeared. This is because the variable assignment to PS1 was made only in the current shell. To make the new shell prompt active in subshells, you must export it as in the following example.

Bourne Shell Tutorial

Now the variable PS1 is global; that is, it is available to all subshells. When a variable has been made global in this way, it remains available until you log out of the parent shell. You can make an assignment permanent by including it in your. You learn some UNIX commands that are useful mainly in the context of shell programs.

You also learn how to make your program perform functions conditionally based on logical tests that you define, and you learn how to have parts of a program repeat until its function is completed.

In short, you learn how to use the common tools supplied with UNIX to create more powerful tools specific to the tasks you need to perform. What Is a Program?

Shell Scripts with Multiple Arguments

A wide assortment of definitions exist for what is a computer program, but for this discussion, a computer program is an ordered set of instructions causing a computer to perform some useful function. In other words, when you cause a computer to perform some tasks in a specific order so that the result is greater than the individual tasks, you have programmed the computer.

When you enter a formula into a spreadsheet, for example, you are programming.


When you write a macro in a word processor, you are programming. A Simple Program Suppose that daily you back up your data files with the following command: One of the useful things about programs, though, is that they can be placed in a program library and used over and over, without having to do the programming each time.

Shell programs are no exception. Rather than enter the lengthy backup command each time, you can store the program in a file named backup: You can enter the command in a single line, as you did when typing it into the command line, but because the commands in a shell program sometimes called a shell script are executed in sequence, putting each command on a line by itself makes the program easier to read.

Creating easy-to-read programs becomes more important as the size of the programs increase. Now to back up your data files, you need to call up another copy of the shell program known as a subshell and give it the commands found in the file backup.