38
Content • Linux • Shells and Shell Scripts • C-Shell and tcshell • tcsh, enhanced C-Shell • bash, Bourne-Again Shell

Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

  • View
    297

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

Content

• Linux

• Shells and Shell Scripts

• C-Shell and tcshell• tcsh, enhanced C-Shell• bash, Bourne-Again Shell

Page 2: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell
Page 3: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

History of Linux

(Courtesy Prof. Remzi H. Arpaci-Dusseau)(Courtesy Prof. Remzi H. Arpaci-Dusseau)http://www.cs.wisc.edu/~remzi/Linux/

Page 4: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

Linux Features• Multi-tasking (more than one task/program can run

simultaneously).• Multi-user (more than one user can work simultaneously).• Multi-processing (more than one processor is supported

by the OS, SMP support is very stable).• POSIX compliant…….behavior similar to traditional

Unixes (look and feel).• Runs on a variety of architectures (not just ix86 based

PCs): Sparc, Alpha, PowerPC, MIPS, PalmPilot, ... • An Embedded version of Linux exists for hand-held

devices and real-time OS applications.

Page 5: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

X Windows• X is the standard graphical user interface for Unix.• You can have multiple windows, and use a mouse for point and click apps.• Look and feel of Linux machines is very professional and better than Windows

machines, and highly customizable.• For Linux, Xwindows is called Xfree86 (X11R6, XFree86 release 6) .• It used to be a pain to get X working under Linux, since you had to configure the

drivers manually, but now the new releases of Linux do this automatically • Some standard X applications are:

– xterm, xclock, xman, netscape, gnuplot, gimp • There are several different X window managers, which give different look-and-

feel: – KDE– GNOME– Enlightenment– Windowmaker– Other classic WMs: olvwm, twm, fvwm2 (classic MIT window manager, Tom's)

Page 6: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

Interfacing Linux with Other OS• Wine and WABI are windows emulators for Linux

(limited success so far). Wine is GPL, WABI is commercial.

• DOSemu is a very stable MS-DOS emulator.Some of your partitions on your disk can be MS-DOS partitions. You can read MS-DOS floppies too.

• VMware is the best alternative, if you need to run both Linux and MS Windows. It is a commercial emulator that emulates the x86 hardware flawlessly, so any OS that can run on the x86 platform can be installed under VMware as a separate application!

Page 7: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

Interfacing Linux with Other OS

VMWare: Windows XP under Linux

Page 8: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

Shell Commands

• Shell commands are interpreted directly by the shell you specify.

• The commands are similar to the statement in some programming languages, such as C.

• Popular shells include:– Enhanced C-shell tchs (csh+)– Bourne-Again Shell, bash (sh+)– Korn Shell (ksh)

• These lecture will focus on the first two shells

Page 9: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

Shells’ Features• The bash an tcsh shells are similar in the features the

offer. In particular:– Pass arguments to your script– Set and reference variables– Use of control flow– Interact with the user (read user input)– Comments…

• Info on commands a given shell offers can be found in the man pages for that shell.

• There are many Linux/UNIX references that give detailed information and tips.

Page 10: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

Shell Scripts

• What are they for?– To automate certain common activities an user

performs routinely.– They serve the same purpose as batch files in

DOS/Windows.– Example:

• rename 1000 files from upper case to lowercase

Page 11: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

What are Shell Scripts

• Just text/ASCII files with:– a set of standard UNIX/Linux commands (ls, mv, cp, less, cat, etc.) along with

• flow of control– some conditional logic and branching (if-then),– loop structures (foreach, for, while), and

• I/O facilities (echo, print, set, ...).

– They allow use of variables.– They are interpreted by a shell directly.– Some of them (csh, tcsh) share some of C syntax.– DOS/Win equivalent - batch files (.bat)

Page 12: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

A simple C program that call a function to plot sin(X)

• sine_plot.c (week15)

• sine (exe)

Page 13: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell
Page 14: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

Creating and Assigning Simple Variables

• In standard Unix, • set can be used for simple variables.• Example: set Y=Green, set Y = Green will both work here.• In different Linux shells, most of the time we can use the

name directly. For example, as we have been using in bash shell.

• X=123• Color=red • However, in C or tcshell, one cannot use that kind of

command.

Page 15: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

The limitation of set

• set cannot be used to assign the result of an expression to a variable.

• @ -- as the built-in command can be used in• this situation.• Example: set a = 2 * 2 is wrong• @ a = 2 * 2• Example: facto w15 (Notice the upper limit

for signed integer arithmetic calculations. 16 is the limit for this function)

Page 16: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

Examples

• set1 –w..k15 folder

• #! /bin/tcsh

• set Y = Green

• set Z = Red

• set X = Blue

• echo $Y, $Z, $X

Page 17: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell
Page 18: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

The importance of #! “shebang”

• In the factorial script facto, if we change the first statement, the program will not able to run properly.

• facto & facto2 in w15

Page 19: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

Examples

• prime – a shell script. (week15 prime)

• Find the prime numbers from given range of input number.

• Prime.c – a C language file that perform the same function. (Prime – same folder)

Page 20: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

Predefined Local Variables

• $< -- The next line of standard input, fully

• quoted.

• $argv – A list that contains all of the

• positional parameters: $argv[1] = $1

• $cdpath – The list of alternatives that chdir uses for searching purposes.

Page 21: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

More Predefined Local Variables

• $cwd, $echo, $hischars, $history, $home

• $ignoreeof, $mail, $noclobber, … …

Page 22: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

History• C shell support the history mechanism. C shell keeps a record of the commands

you enter from the keyboard so that they may be:

1. edited.2. Re-Executed at a later stage.Example: set prompt=‘\! %’ …include event

num in prompt.

Page 23: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

Examples

• Continue type in Unix commands, for example, type in 5 commands.

• The type history, the shell will list all the commands you have just typed in along with the time you executed them.

• We can also use the alias in C shell.• alias h history• *** Under the standard UNIX, history [ -rh]

[number] format is used and if no parameters are supplied, history command will only list the last $history command

Page 24: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

Command Re-execution

• One of the frequently used one is !!• !! – Replace with the text of the most recent

command.• Example: 1. cal• !! 2004• !number – Replace with the text of the command

with the specified event number.

Page 25: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

Repeat Command in c shell

• It allows you to repeat a single command for any number of specified times.

• For example, repeat 100 echo Hello, world!

• However, anything after the repeat has to be a legal Unix command under the your current c shell.

Page 26: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

while (expr)… end • Example: while2 (week15 )• #! /bin/csh• # This script shows how while... end loop works.• set x = 1• while ($x <= $1)• set y = 1• while ($y <= $1)• @ v = $x * $y # calculate entry• echo -n $v " " # display the entry• @ y ++• end• echo " " # newline• @ x ++• end

Page 27: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

Why not use C/C++ for that?

• C/C++ programming requires compilation and linkage, maybe libraries, which may not be available (production servers).

• For the typical tasks much faster in development, debugging, and maintenance (because they are interpreted and do not require compilation).

Page 28: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

Shell Script Invocation• Specify the shell directly:

– % tcsh myshellscript– % tcsh -v myshellscript

(-v = verbose, useful for debugging)

• Make the shell an executable first and then run is a command (set up an execution permission):– % chmod u+x myshellscript

• Then either this:– % myshellscript

(if the path variable has ‘.’ in it; security issue!)

• Or:– % ./myshellscript

(should always work)

Page 29: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

Shell Script Invocation (2)

• If you get an error:“myshellscrip: command not found”– The probably “.” is not in your path or there’s no

execution bit set.

• When writing scripts, choose unique names, that preferably do not match system commands.– Bad name would be test for example, since there are

many shells with this internal command.

• To disambiguate, always precede the shell with “./” or absolute path in case you have to name your thing not very creatively.

Page 30: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

Start Writing a Shell Script• The very first line, often called 'shebang' (#!) should

precede any other line, to assure that the right shell is invoked.

• Comments start with '#', with the exception of #!, $#, which are a special character sequences.

• Everything on a line after # is ignored if # is not a part of a quoted string or a special character sequence.

#!/bin/tcsh #!/bin/bash# This is for tcsh # For Bourne-Again Shell

#!/bin/sh# This is for Bourne Shell

Page 31: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

The importance of #! “shebang”

• In the factorial script facto, if we change the first statement, the program will not able to run properly.

• facto & facto2 in w15

Page 32: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

Example

• Switch2 and sw2 in w15

Page 33: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

switch .. case .. endsw structure

switch ( string )case str1:

<statements> breaksw

...default:

<statements>breaksw

endsw

Page 34: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

foreach .. end control structure

foreach var ( <list-of-values> )<statements>

end

Example: foreach2 in w15

Page 35: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

switch .. case .. endsw structure

switch ( string )case str1:

<statements> breaksw

...default:

<statements>breaksw

endsw

Page 36: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

Example

• Switch2 and sw2 in w15

Page 37: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

while

while ( <expression> )<statements>

end

Page 38: Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell

File Inquiry Operators:-op file

r Read accessw Write accessx Execute accesse Existenceo Ownershipz Zero sizes Non-zero size

f Plain filed Directoryl Symbolic linkb Block special filec Character special filep Named pipe (FIFO)S Socket special file