Shell Scripting AFNOG IX Rabat, Morocco May 2008.

Slides:



Advertisements
Similar presentations
Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
Advertisements

Shell Programming Oleh: Idris Winarno. Topik Hello world!!! Variables Functions Conditionals Loops Function.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CSc 352 Shell Scripts Saumya Debray Dept. of Computer Science
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
UNIX Shell Scripting > Echo “A quick how-to”. What is shell scripting? Interpreted (non-compiled) language Slower compared to compiled languages Much.
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.
Lab 8 Shell Script Reference:
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.
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.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Linux Shell Programming Tutorial 3 ENGR 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
GNU Compiler Collection (GCC) and GNU C compiler (gcc) tools used to compile programs in Linux.
Programming With C.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Linux Operations and Administration
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.
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)
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group Module 9 Basic Scripting.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Lecture 2: Advanced UNIX, shell scripts (ol)‏ Programming Tools And Environments.
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.
Writing Scripts Hadi Otrok COEN 346.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
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
Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?
Shell Control Statements and More
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Lab 8 Overview Apache Web Server. SCRIPTS Linux Tricks.
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]
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
 Prepared by: Eng. Maryam Adel Abdel-Hady
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
 Prepared by: Eng. Maryam Adel Abdel-Hady
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu.
Bash Shell Scripting 10 Second Guide.
CIRC Winter Boot Camp 2017 Baowei Liu
Department of Computer Engineering
Chapter 9 Shell Programming
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Shell Scripting March 1st, 2004 Class Meeting 7.
Lecture 9 Shell Programming – Command substitution
CSE 303 Concepts and Tools for Software Development
Agenda Control Flow Statements Purpose test statement
Outline Altering flow of control Boolean expressions
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
C Programming Getting started Variables Basic C operators Conditionals
CSE 303 Concepts and Tools for Software Development
Linux Shell Script Programming
CSC 352– Unix Programming, Fall, 2011
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Basic shell scripting CS 2204 Class meeting 7
Presentation transcript:

Shell Scripting AFNOG IX Rabat, Morocco May 2008

Why Scheduled Tasks Repetitive sequences Boot scripts

When not to use scripting Resource-intensive tasks, especially where speed is a factor Complex applications, where structured programming is a necessity Need direct access to system hardware Proprietary, closed-source applications

Sample repetitive tasks Cleanup Run as root, of course. # cd /var/log # cat /dev/null > messages # cat /dev/null > wtmp # echo "Logs cleaned up.” You can put these commands in a file and say bash filename

she-bang #! and the shell (first line only) chmod a+x (remember the permissions) Example: put the following text in hello.sh #!/bin/bash echo Hello World chmod a+x hello.sh./hello.sh (remember $PATH)

variables Variable is a “container” of data. Some variables already exist in your “environment” like $PATH and $PROMPT Shell substitutes any token that starts with $ with the contents of the variable of that name Variable can be created using VAR=something – some shells require the keyword “set” to make it persist, others need “export”

Sample special variables $ echo $PATH the shell searches PATH for programs if you do not type them with an absolute path $ echo pwd $ echo $( pwd ) the shell runs the command in between “$(“ and “)” and puts the result on the command line $? When a process ends, it can leave an “exit code” which is an integer which you can check. If the exit code is zero then usually it exited successfully. Non zero usually indicates an error.

sample repetitive tasks revisited #!/usr/local/bin/bash # Proper header for a Bash script. # Cleanup, version 2 # Run as root, of course. # Insert code here to print error message and exit if not root. LOG_DIR=/var/log# Variables are better than hard-coded values. cd $LOG_DIR cat /dev/null > messages cat /dev/null > wtmp echo "Logs cleaned up.” exit # The right and proper method of "exiting" from a script.

Conditionals if expression then statement if expression then statement1 else statement2. if expression1 then statement1 else if expression2 then statement2 else statement3

Bash conditional syntax #!/usr/local/bin/bash if [ "foo" = "foo" ]; then echo expression evaluated as true fi #!/usr/local/bin/bash if [ "foo" = "foo" ]; then echo expression evaluated as true else echo expression evaluated as false fi

Loops for loop lets you iterate over a series of 'words' within a string. while executes a piece of code if the control expression is true, and only stops when it is false until loop is almost equal to the while loop, except that the code is executed while the control expression evaluates to false.

Sample syntax #!/usr/local/bin/bash for i in $( ls ); do echo item: $i done #!/usr/local/bin/bash COUNTER=0 while [ $COUNTER -lt 10 ]; do echo The counter is $COUNTER let COUNTER=COUNTER+1 done #!/usr/local/bin/bash COUNTER=20 until [ $COUNTER -lt 10 ]; do echo COUNTER $COUNTER let COUNTER-=1 done

Practice Write a shell script to print the disk usage every 5 seconds. Hint: sleep N is a command which will basically put the prompt/program to sleep for N seconds Hint2: in any conditional, you can say “true” or “false” to force it to always evaluate like that.

Extra Programming (say in C) builds on similar concepts. Source text is COMPILED into binary machine code. Why?

hello world (c style) Edit hello.c and put the following text #include int main(){ printf(“Hello World\n”); return 0; } Type gcc -o hello hello.c Type./hello ; echo $? Change the return 0 to return 42 Compile it again, Run./hello; echo $?