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

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Chapter One The Essence of UNIX.
A Guide to Unix Using Linux Fourth Edition
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
X-Windows Free-Windows. A brief history The X Window system, developed at MIT in the late 1980s, is rapidly becoming the industry standard windowing system.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Linux+ Guide to Linux Certification, Second Edition
Scripting Languages and C-Shell. What is a scripting language ? Script is a sequence of commands written as plain text and run by an interpreter (shell).
CSc 352 Shell Scripts Saumya Debray Dept. of Computer Science
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Introduction to Linux and Shell Scripting Jacob Chan.
Shell Programming. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Linux Shell. 2 Linux Command-Line Interface ■ Linux shells: A shell is a command interpreter that allows you to type commands from the keyboard to interact.
Nic Shulver, Scripts and Batch files Scripting in Windows and Linux What is scripting? What is it for? DOS/Windows batch files.
TOPIC 5.0 LINUX SHELLS.
Using the Bash Shell. Linux Shell Options Linux provides a range of options for shells. bash –The GNU Bourne Again Shell (bash) bsh – The Bourne Shell.
Overview of Linux CS3530 Spring 2014 Dr. José M. Garrido Department of Computer Science.
Introduction to Shell Script Programming
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Shell The Shell The agency that.
Chapter 6: Shell Programming
CENT 305 Information Systems Security Linux Introduction.
An Introduction to Unix Shell Scripting
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
Shell script
Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: # Purpose: display command and parameters echo $0 echo $argv[*]
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Session 2 Wharton Summer Tech Camp Basic Unix. Agenda Cover basic UNIX commands and useful functions.
1 Shell Scripting (C shell) SungHo Maeung 10/27/2000 Tutorial section Computer Science Lab.
LINUX System : Lecture 6 Shell Programming
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 11: Shell.
CS465 - UNIX The Bourne Shell.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
Unix/Linux cs3353. The Shell The shell is a program that acts as the interface between the user and the kernel. –The shell is fully programmable and will.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Introduction to Programming Using C An Introduction to Operating Systems.
Chapter Six Introduction to Shell Script Programming.
Unix Advanced Shells Chapter 10. Unix Shells u Command Line Interpreter –once logged in, login gives control to a shell –it prompts for input, then parses,
Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems.
Lecture 02 File and File system. Topics Describe the layout of a Linux file system Display and set paths Describe the most important files, including.
The Kernel At a high level, the kernel in an operating system serves as the bridge between applications and the actual data processing of the hardware.
1 CS3695 – Network Vulnerability Assessment & Risk Mitigation – Introduction to Unix & Linux.
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
1 Week 8 Creating Simple Shell Scripts. 2 Chapter Objectives  In this chapter, you will :  Learn how to create Shell Scripts  Commenting / Making Portable.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Linux Administration Working with the BASH Shell.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu.
CS 350 Lecture 3 UNIX/Linux Shells by İlker Korkmaz and Kaya Oğuz.
Overview of Linux Fall 2016 Dr. Donghyun Kim
CIRC Winter Boot Camp 2017 Baowei Liu
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Shell Features CSCI N321 – System and Network Administration
System Programming and administration CS 308
Unix Shells.
LINUX System : Lecture 5 (English-Only Lecture)
The Linux Operating System
Tutorial of shell script
Shells, Help, and Paths.
Shell Programming.
Linux Shell Script Programming
Linux Professor Sabol.
Bash Scripting CS 580U - Fall 2018.
Presentation transcript:

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)

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

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 -- as the built-in command can be used in this situation. Example: set a = 2 * 2 is 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: 1.edited. 2.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 <= v = $x * $y # calculate entry echo -n $v " " # display the y ++ end echo " " # 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: breaksw... default: breaksw endsw

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

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

Example Switch2 and sw2 in w15

while while ( ) end

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