Lab 8 Shell Script Reference:

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.
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
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.
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.
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 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)
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths Corporate Information and Computing Services The University.
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.
IDL Tutorials: Day 4 Goal: Learn some programming techniques: Relational operators, conditional statements, boolean operators, loops Maria Kazachenko
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
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.
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
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.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
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.
Shell Control Structures
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
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.
REPETITION Why Repetition?
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 http://www.freeos.com/guides/lsst/index.html

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

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 command1... fi -cat command: Display text files on screen. cat file-name

test expression or [ expr ] - -True  return zero(0) Is used to see if an expression is true - -False returns nonzero 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 expression 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 expression or [ expr ] String Comparisons Meaning Operator            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 expression or [ expr ] Logical Operators Meaning Operator            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 then command1 else if condition is not true then execute all commands up to fi fi

Exercise ( if..else with parameters) if [ $# -eq 0 ] then echo " Enter one argument “ exit 1 else   echo " The first argument is $1 " fi

if …else…fi $ chmod 755 isnump_n $ ./isnump_n 5 $ 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

if …else…fi Syntax: if condition Then then ..... .. do this else ....

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 elif condition1 then condition1 is zero (true - 0) elif condition2 condition2 is zero (true - 0) else None of the above condtion,condtion1,condtion2 are true (i.e. all of the above nonzero or false) execute all commands up to fi fi

$ 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 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) done $ vi testfor for NUMBER in 1 2 3 4 5 do echo "Welcome $NUMBER times" 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

while loop Syntax: i=$1 while test $i != 0 do echo –n "$i ," while [ condition ] do command1 command2 command3 .. .... done Write script to print numbers as 5,4,3,2,1, using while loop. The biggest number is supplied as command line argument. i=$1 while test $i != 0 do echo –n "$i ," i=`expr $i - 1` done echo " "

for (( i=1; i<=5; i++ )) do for (( j=1; j<=i; j++ )) Exercise (1) Write shell script using for loop to print the following patterns . 1 22 333 4444 55555 for (( i=1; i<=5; i++ )) do for (( j=1; j<=i; j++ )) echo -n "$i" done echo ”“