Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook

Slides:



Advertisements
Similar presentations
IF statement (i) Single statement. IF ( logical expression ) statement Example: read(*,*) a if (a. lt. 0) a = -a write(*,*) a Or read(*,*) a if (a < 0)
Advertisements

CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Information Networking Security and Assurance Lab National Chung Cheng University 1 if condition Condition is defined as: "Condition is nothing but comparison.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
Further Shell Scripting Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
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.
Lab 8 Shell Script Reference:
Shell Control Structures CSE 2031 Fall August 2015.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
LINUX Shell Scripting Advanced Issues
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
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.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths November 2005 Corporate Information and Computing Services The University.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
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.
Shell Scripting AFNOG IX Rabat, Morocco May 2008.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
Shell scripting. Number check vi isnump_n #!/bin/sh # # Script to see whether argument is positive or negative # if [ $# -eq 0 ] then echo "$0 : You must.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
Writing Scripts Hadi Otrok COEN 346.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
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.
Week Five Agenda Link of the week Review week four lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Flow Control. The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
 Prepared by: Eng. Maryam Adel Abdel-Hady
 Prepared by: Eng. Maryam Adel Abdel-Hady
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
COMP075 OS2 Bash Scripting. Scripting? BASH provides access to OS functions, like any OS shell Also like any OS shell, BASH includes the ability to write.
Bash Shell Scripting 10 Second Guide.
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Shell Programming (ch 10)
Agenda Control Flow Statements Purpose test statement
Pepper (Help from Dr. Robert Siegfried)
Control Structures: if Conditional
Control Structures: for & while Loops
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
Presented by, Mr. Satish Pise
Shell Control Structures
Chapter 5 The Bourne Shell
bash scripting continued; remote X windows; unix tidbits
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook

if condition if condition which is used for decision making in shell script, If given condition is true then command1 is executed. Syntax: if condition then then command1... command1... fi fi $ vi showfile #Script to print file # if cat $1 then echo -e "\n\nFile $1, found and successfully echoed" fi $ chmod 755 showfile $./showfile file-name

if condition $ vi trmif # Script to test rm command and exist status # if rm $1 then echo "$1 file deleted" fi $ chmod 755 trmif $./trmif file-name

test command or [ expr ] - -True  return zero(0) Is used to see if an expression is true - -False  returns nonzero test expression OR [ expression ] Syntax: test expression OR [ expression ] $ vi ispositive # Script to see whether argument is positive # if test $1 -gt 0 then echo "$1 number is positive" fi Run it as follows: $ chmod 755 ispositive $./ispositive 5

test command or [ expr ] For test statement with if commandFor [ expr ] statement with if command Mathematical Operator in Shell Script Meaning if [ 5 -eq 6 ]if test 5 -eq 6 is equal to -eq if [ 5 -ne 6 ]if test 5 -ne 6 is not equal to -ne if [ 5 -lt 6 ]if test 5 -lt 6 is less than -lt if [ 5 -le 6 ]if test 5 -le 6 is less than or equal to -le if [ 5 -gt 6 ]if test 5 -gt 6 is greater than -gt if [ 5 -ge 6 ]if test 5 -ge 6 is greater than or equal to -ge

test command or [ expr ] String Comparisons MeaningOperator string1 is equal to string2 string1 = string2 string1 is NOT equal to string2 string1 != string2 string1 is NOT NULL or not defined string1 string1 is NOT NULL and does exist -n string1 string1 is NULL and does exist -z string1

test command or [ expr ] Logical Operators MeaningOperator Logical NOT ! expression Logical AND expression1 -a expression2 Logical OR expression1 -o expression2

if …else…fi If given condition is true then command1 is executed otherwise command2 is executed. Syntax: if condition thenconditionelse if condition is not true then execute all commands up to fi fi

if …else…fi $ vi isnump_n # Script to see whether argument is positive or negative if [ $# -eq 0 ] then echo "$0 : You must give/supply one integers" exit 1 fi if test $1 -ge 0 then echo "$1 number is positive" else echo "$1 number is negative" fi $ chmod 755 isnump_n $./isnump_n 5

myscript $0 foo $1 Bar $2 Special shell variables $0…$9 $# tells you how many parameter your script was given Positional parameters or command line arguments myscript foo bar

if …else…fi Syntax: if condition Then if condition if conditionthen do this else fielse do this fi

Nested if-else-fi $ vi nestedif osch=0 echo "1. Unix (Sun Os)" echo "2. Linux (Red Hat)" echo -n "Select your os choice [1 or 2]? " read osch if [ $osch -eq 1 ] ; then echo "You Pick up Unix (Sun Os)" else if [ $osch -eq 2 ] ; then echo "You Pick up Linux (Red Hat)" else echo "error only 1 or 2" fi fi $ chmod 755 nestedif $./ nestedif

Multilevel if-then-else Syntax: if condition then condition is zero (true - 0) execute all commands up to elif statement execute all commands up to elif statement elif condition1 then condition1 is zero (true - 0) execute all commands up to elif statement elif condition2 then condition2 is zero (true - 0) execute all commands up to elif statement execute all commands up to elif statement else else None of the above condtion,condtion1,condtion2 are true (i.e. all of the above nonzero or false) (i.e. all of the above nonzero or false) execute all commands up to fi execute all commands up to fifi

$ vi elf # Script to test if..elif...else # if [ $1 -gt 0 ]; then echo "$1 is positive" elif [ $1 -lt 0 ] then echo "$1 is negative" elif [ $1 -eq 0 ] then echo "$1 is zero" else echo "Opps! $1 is not number, give number" fi $ chmod 755 elf $./ elf 8 $./ elf -3 $./ elf 0 $./ elf a

for Loop $ vi testfor for NUMBER in do echo "Welcome $NUMBER times" done Syntax: for { variable name } in { list } Do execute one for each item in the list until the list is not finished (And repeat all statement between do and done) execute one for each item in the list until the list is not finished (And repeat all statement between do and done)done $ vi for2 LIMIT = 10 for (( i = 1 ; i <= LIMIT; i++ )) do echo "Welcome $i times" done

for Loop $ vi nestedfor for (( i = 1; i <= 5; i++ )) ### Outer for loop ### do for (( j = 1 ; j <= 5; j++ )) ### Inner for loop ### do echo -n "$i " done echo "" #### print the new line ### done