Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell."— Presentation transcript:

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

2

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

4 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.

5 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)

6 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!VMware

7 Interfacing Linux with Other OS VMWare: Windows XP under Linux

8 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

9 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.

10 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

11 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)

12 A simple C program that call a function to plot sin(X) sine_plot.c (week15) sine (exe)

13

14 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.

15 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)

16 Examples set1 –w..k15 folder #! /bin/tcsh set Y = Green set Z = Red set X = Blue echo $Y, $Z, $X

17

18 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

19 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)

20 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.

21 More Predefined Local Variables $cwd, $echo, $hischars, $history, $home $ignoreeof, $mail, $noclobber, … …

22 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.

23 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

24 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.

25 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.

26 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

27 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).

28 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)

29 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.

30 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

31 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

32 Example Switch2 and sw2 in w15

33 switch.. case.. endsw structure switch ( string ) case str1: breaksw... default: breaksw endsw

34 foreach.. end control structure foreach var ( ) end Example: foreach2 in w15

35 switch.. case.. endsw structure switch ( string ) case str1: breaksw... default: breaksw endsw

36 Example Switch2 and sw2 in w15

37 while while ( ) end

38 File Inquiry Operators: -op file 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


Download ppt "Content Linux Shells and Shell Scripts C-Shell and tcshell tcsh, enhanced C-Shell bash, Bourne-Again Shell."

Similar presentations


Ads by Google