CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Shell Programming.

Slides:



Advertisements
Similar presentations
Shell Script Assignment 1.
Advertisements

CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CS 497C – Introduction to UNIX Lecture 32: - Shell Programming Chin-Chih Chang
CS 497C – Introduction to UNIX Lecture 22: - The Shell Chin-Chih Chang
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Chapter 15 Mr. Mohammad Smirat SHELL PROGRAMMING (Bourne Shell)
Linux+ Guide to Linux Certification, Second Edition
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.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
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.
Shell Script Examples.
Shell Control Structures CSE 2031 Fall August 2015.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Chapter 15 Introductory Bash Programming
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
Introduction to Shell Script Programming
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
Writing Shell Scripts ─ part 1 CSE 2031 Fall September 2015.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
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 Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
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.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 10 Dr. Jerry Shiao, Silicon Valley University.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
CSC 4630 Meeting 5 January 31, Next Time Enhance the steps that you used to clean the Moby Dick chapter to create a shell script that takes any.
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.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
If condition1 then statements elif condition2 more statements […] else even more statements fi.
Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 12 Dr. Jerry Shiao, Silicon Valley University.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Chapter 15 Introductory Bourne Shell Programming.
Linux Administration Working with the BASH Shell.
Shell Control Structures CSE 2031 Fall June 2016.
CIT 470: Advanced Network and System Administration
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Shell Script Assignment 1.
CIT 470: Advanced Network and System Administration
Agenda Control Flow Statements Purpose test statement
UNIX Reference Sheets CSE 2031 Fall 2010.
Linux Shell Script Programming
Shell Control Structures
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Chapter 5 Bourne Shells Scripts
Essential Shell Programming
Introduction to Bash Programming, part 3
Presentation transcript:

CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Shell Programming

CIT 140: Introduction to ITSlide #2 Topics 1.Shell Programming 2.Shell Variables 3.Command Substitution 4.Command Line Arguments 5.Reading from the User 6.Control Flow 7.if/elif/else 8.for loops 9.while loops 10.case statements

CIT 140: Introduction to ITSlide #3 Introduction Shell script: a shell program, which consists of shell commands to be executed by a shell and is stored in ordinary UNIX file. Shell variable: read/write storage place for users and programmers to use as a scratch pad for completing a task. Program control flow commands (or statements): allow non sequential execution of commands in a shell script and repeated execution of a block of commands.

CIT 140: Introduction to ITSlide #4 Running a Bourne Shell Script 1.Run /bin/sh command with script parameter $ /bin/sh script_file 2.Make the script file runnable directly $ chmod u+x script_file $ vim script_file First line: #!/bin/sh $./script_file

CIT 140: Introduction to ITSlide #5 Shell Variables

CIT 140: Introduction to ITSlide #6 Read-only Shell Variables

CIT 140: Introduction to ITSlide #7 Displaying Shell Variables > printenv PWD=/export/home0/waldenj TZ=US/Michigan PAGER=less HOSTNAME=zappa LD_LIBRARY_PATH=/usr/local/lib:/usr/lib:/usr/ucblib MANPATH=/usr/local/man:/usr/share/man:/usr/openwin/share/man VISUAL=vim USER=waldenj ENV_SET=1 CVS_RSH=ssh EDITOR=vim LOGNAME=waldenj SHLVL=1 TEXMF=/usr/local/share/texmf SHELL=/bin/bash HOSTTYPE=sparc CDPATH=::/export/home0/waldenj HOME=/export/home0/waldenj TERM=xterm PERL_RL=Perl

CIT 140: Introduction to ITSlide #8 Setting Shell variables Assign a value to a variable: varname=value Examples monty=python spam=“spam, spam, spam, spam” PATH=/bin:/usr/ucb:/usr/bin Notes No spaces on either side of equal sign.

CIT 140: Introduction to ITSlide #9 Using Shell Variables > spam=eggs > echo $spam eggs > echo spam spam > echo \$spam $spam > spam=spam and eggs bash: and: command not found > echo $spam eggs > spam="spam and eggs" > echo $spam spam and eggs > spam=c* > echo $spam cit140 csc382 csc501

CIT 140: Introduction to ITSlide #10 Special echo Characters

CIT 140: Introduction to ITSlide #11 Command Substitution Command Substitution: When a command is enclosed in back quotes, the shell executes the command and substitutes the command (including back quotes) with the output of the command. `command` Purpose:Substitute its output for `command`

CIT 140: Introduction to ITSlide #12 Using Command Substitution > dir=`pwd` > echo $dir /export/home0/waldenj > echo "The current directory is $dir" The current directory is /export/home0/waldenj > echo "The current date and time is `date`" The current date and time is Sun Nov 20 15:47:16 EST 2005

CIT 140: Introduction to ITSlide #13 Exporting Environment export [name-list] Purpose: Export the names and copies of the current values in the ‘name-list’ to every command executed from this point on. Example: > grep PATH.bashrc PATH=/bin:/usr/bin:/usr/local/bin:/usr/ucb MANPATH=/usr/local/man:/usr/man:/usr/X11R6/man export PATH MANPATH

CIT 140: Introduction to ITSlide #14 Resetting Variables unset [name-list] Purpose Reset or remove the variable or function corresponding to the names in ‘name-list’, where ‘name- list’ is a list of names separated by spaces. > food1=spam > food2=eggs > echo "I like $food1 and $food2" I like spam and eggs > unset food1 food2 > echo "I like $food1 and $food2" I like and

CIT 140: Introduction to ITSlide #15 Reading from Standard Input read variable-list Purpose: Read one line from standard input and assign words in the line to variables in ‘name-list’.

CIT 140: Introduction to ITSlide #16 Shell Script Arguments $ cat cmdargs_demo #!/bin/sh echo “The command name is: $0.” echo “The number of command line arguments passed as parameters are $#.” echo “The value of the command line arguments are: $1 $2 $3 $4 $5 $6 $7 $8 $9.” echo “Another way to display values of all of the arguments: echo “Yet another way is: $*.” exit 0 $ cmdargs_demo a b c d e f g h i The command name is: cmdargs_demo. The number of command line arguments passed as parameters are 9. The value of the command line arguments are: a b c d e f g h i. Another way to display values of all of the arguments: a b c d e f g h i. Yet another way is: a b c d e f g h i. $ cmdargs_demo One Two 3 Four 5 6 The command name is: cmdargs_demo. The number of command line arguments passed as parameters are 6. The value of the command line arguments are: One Two 3 Four 5 6. Another way to display values of all of the arguments: One Two 3 Four 5 6. Yet another way is: One Two 3 Four 5 6.

CIT 140: Introduction to ITSlide #17 Command Output as Arguments > date Sun Nov 20 16:23:08 EST 2005 > set `date` > echo Sun Nov 20 16:23:13 EST 2005 > echo $2 $3, $6 Nov 20, 2005

CIT 140: Introduction to ITSlide #18 Comments Put comments in your programs to describe the purpose of a particular set of commands. Use comments to create a program header for your scripts, including: 1.Name of the author 2.Date written 3.Date last modified 4.Purpose of the script

CIT 140: Introduction to ITSlide #19 Control Flow Commands Used to determine the sequence in which statements in a shell script execute. Three types of control flow statements: 1.Two-way branching 2.Multiway branching 3.Repetitive execution of one or more commands

CIT 140: Introduction to ITSlide #20 Program Control Flow Commands

CIT 140: Introduction to ITSlide #21 If Statement

CIT 140: Introduction to ITSlide #22 Operators for the test Command

CIT 140: Introduction to ITSlide #23 Program Control Flow Commands

CIT 140: Introduction to ITSlide #24 Example: fileinfo.sh #!/bin/sh if [ $# -ne 1 ]; then echo "Usage: fileinfo filename" fi file=$1 if [ -f $file ]; then set -- `ls -lg $file` owner=$3 group=$4 echo "Owner: $3" echo "Group: $4" echo -n "Permissions: " if [ -r $file ]; then echo -n "Read " fi if [ -w $file ]; then echo -n "Write " fi if [ -x $file ]; then echo -n "Execute " fi echo else echo "File $file is not a regular file." fi >./fileinfo.sh fileinfo.sh Owner: waldenj Group: students Permissions: Read Write Execute

CIT 140: Introduction to ITSlide #25 Program Control Flow Commands

CIT 140: Introduction to ITSlide #26 Shell Example: Filetype #!/bin/sh if [ $# -ne 1 ]; then echo "Usage: filetype filename" fi filename=$1 if [ -L $filename ]; then echo "File $filename is a symbolic link." elif [ -f $filename ]; then echo "File $filename is a regular file." elif [ -d $filename ]; then echo "File $filename is a directory." else echo "I don't know what file $filename is." fi

CIT 140: Introduction to ITSlide #27 Shell Example: Filetype >./filetype.sh filetype.sh File filetype.sh is a regular file. >./filetype.sh /bin File /bin is a symbolic link. >./filetype.sh / File / is a directory.

CIT 140: Introduction to ITSlide #28 The for Statement

CIT 140: Introduction to ITSlide #29 Shell Example: for > ls *.man bash.man cat.man tcsh.man > for file in *.man > do > bzip2 -v $file > done bash.man: 4.821:1, bits/byte, 79.26% saved, in, out. cat.man: 2.684:1, bits/byte, 62.75% saved, 5366 in, 1999 out. tcsh.man: 4.259:1, bits/byte, 76.52% saved, in, out. > ls *.man.bz2 bash.man.bz2 cat.man.bz2 tcsh.man.bz2

CIT 140: Introduction to ITSlide #30 Shell Script Example: for #!/bin/sh for user in do grep "^"$user /etc/passwd >/dev/null 2>&1 if [ $? -eq 0 ]; then homedir=`grep "^"$user /etc/passwd | cut -d: -f6` echo "User $user has home directory $homedir" else echo "No such user $user" fi done >./userinfo.sh waldenj newellg spam User waldenj has home directory /export/home0/waldenj User newellg has home directory /export/home1/newellg No such user spam

CIT 140: Introduction to ITSlide #31 The while statement

CIT 140: Introduction to ITSlide #32 Shell Script Example: while #!/bin/sh secret=agent007 guess=none echo "Guess the secret word." while [ $secret != $guess ] do echo -n "What's your guess? " read guess done echo "You guessed the secret word!"

CIT 140: Introduction to ITSlide #33 Shell Script Example: while >./guessgame.sh Guess the secret word. What's your guess? spam What's your guess? eggs What's your guess? agent007 You guessed the secret word!

CIT 140: Introduction to ITSlide #34 The case Statement

CIT 140: Introduction to ITSlide #35 The case Statement

CIT 140: Introduction to ITSlide #36 Case Example: LittleSh #!/bin/sh while [ 1 ] do echo -n "littleshell> " read command case $command in 'dir' ) ls -lg ;; 'users' ) who ;; 'quit' ) exit ;; * ) echo "I didn't understand that command" ;; esac done

CIT 140: Introduction to ITSlide #37 LittleSh Example >./littlesh.sh littleshell> ls I didn't understand that command littleshell> dir total 113 -rw-r--r-- 1 waldenj students Nov 20 17:30 bash.man.bz2 -rw-r--r-- 1 waldenj students 1999 Nov 20 17:30 cat.man.bz2 -rwxr-xr-x 1 waldenj students 247 Nov 20 17:31 littlesh.sh -rw-r--r-- 1 waldenj students Nov 20 17:30 tcsh.man.bz2 littleshell> users longa pts/12 Nov 8 14:36 waldenj pts/3 Nov 17 12:52 newellg pts/4 Nov 14 11:21 waldenj pts/2 Nov 20 15:19 partons pts/16 Nov 20 16:50