380 likes | 524 Vues
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) http://www.cs.wisc.edu/~remzi/Linux/. Linux Features.
E N D
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) http://www.cs.wisc.edu/~remzi/Linux/
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.
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)
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!
Interfacing Linux with Other OS VMWare: Windows XP under Linux
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
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.
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
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)
A simple C program that call a function to plot sin(X) • sine_plot.c (week15) • sine (exe)
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.
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)
Examples • set1 –w..k15 folder • #! /bin/tcsh • set Y = Green • set Z = Red • set X = Blue • echo $Y, $Z, $X
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
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)
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.
More Predefined Local Variables • $cwd, $echo, $hischars, $history, $home • $ignoreeof, $mail, $noclobber, … …
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: • edited. • Re-Executed at a later stage. Example: set prompt=‘\! %’ …include event num in prompt.
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
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.
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.
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
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).
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)
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.
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
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
Example • Switch2 and sw2 in w15
switch .. case .. endsw structure switch ( string ) case str1: <statements> breaksw ... default: <statements> breaksw endsw
foreach .. end control structure foreach var ( <list-of-values> ) <statements> end Example: foreach2 in w15
switch .. case .. endsw structure switch ( string ) case str1: <statements> breaksw ... default: <statements> breaksw endsw
Example • Switch2 and sw2 in w15
while while ( <expression> ) <statements> end
r Read access w Write access x Execute access e Existence o Ownership z Zero size s Non-zero size f Plain file d Directory l Symbolic link b Block special file c Character special file p Named pipe (FIFO) S Socket special file File Inquiry Operators:-op file