Scripts.

Slides:



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

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
CS 497C – Introduction to UNIX Lecture 34: - Shell Programming Chin-Chih Chang
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 Script Examples.
Shell Control Structures CSE 2031 Fall August 2015.
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 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.
An Introduction to Unix Shell Scripting
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.
Linux+ Guide to Linux Certification, Third Edition
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.
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
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.
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.
The kernel considers each program running on your system to be a process A process lives as it executes, with a lifetime that may be short or long A process.
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.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu.
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.
UNIX signals.
CIRC Winter Boot Camp 2017 Baowei Liu
Shell Control Structures
Department of Computer Engineering
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
Lecture 9 Shell Programming – Command substitution
Shell Programming (ch 10)
Agenda Control Flow Statements Purpose test statement
What is Bash Shell Scripting?
Pepper (Help from Dr. Robert Siegfried)
LING 408/508: Computational Techniques for Linguists
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
Linux Shell Script Programming
Presented by, Mr. Satish Pise
Shell Control Structures
Chapter 5 The Bourne Shell
CSC 352– Unix Programming, Fall, 2011
The Bash Shell Bash Programming
Shell Control Structures
Chapter 5 Bourne Shells Scripts
Essential Shell Programming
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Review.
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

Scripts

What are Scripts? Scripts are “programs” characterized by: Ease of use Scripting languages are intended to be very fast to understand and author programs in Implies relatively simple syntax and semantics OS facilities built in with easy to use interfaces Scripting is usually aimed at workstations Limiting the portability needs of the pre-built libraries Typically interpreted from source code Even a slow interpreter is often fast enough for most tasks Non-scripting languages intended for large programs are often precompiled in at least some sense for superior performance Relatively loose structure

How are scripts used? Automate tasks normally manually executed Complex tasks Many and/or complicated commands Setting up a complex environment Repetitive tasks Doing the same basic task with minor variations Creating UIDs from a list Usually for tasks that will be done repeatedly over a span of time

Script language examples: AWK Bash Bourne CLIST KORN shell Perl PHP Python Sed

Resume 1/18

Scripting with BASH Bourne Again SHell

Bash Basics First line of any program should tell where to find the interpreter Starts with the shebang (#!) Follows with the location and name of the interpreter Not needed if it is the default (e.g. bash on Debian) Should put in regardless which Command to locate a specified command which bash Typical return: /bin/bash Trick: which bash > myshell.sh Will put the bash location in a file called myshell.sh Edit the line to include the shebang at the start of the line #!/bin/bash

Bash Variables Loosely typed Do not need to be formally declared Case sensitive Can be local or global (environmental) By convention environmental variables are UPPER CASE All others lower case Help avoid accidentally overwriting

read Helps make the script interactive Syntax Example Reads from std in Syntax read var_name Example #!/bin/sh echo "Enter your name: \c" read yourname echo "Hello $yourname!" Notes: This is runs the Bourne shell \c gives no newline on terminal Sometimes –e is need to tell the shell to interpret escapes

Command line arguments Scripts can use redirection and pipelines Can pass data through arguments Arguments are referenced by: $1, $2, $3, etc for the first and subsequent args $* for all arguments as a single string $# for the number of arguments $0 for the script filename $? exit status of last command $$ PID of current shell $! PID of last background job

Command line arguments Example testargs.sh In ~/itis3110/bashscripts #!/bin/sh echo "You entered $# arguments." echo "Arg 1: $1" echo "Arg 2: $2"

Exit and $? exit leaves (stops) the script Sets a status: every thing worked as anticipated exit 1 a problem occurred exit same as exit 0 Status stored in $? Example: badexit.sh Should return 55 Note: Zero is a success Any non-zero value is a failure When checking status for a proceeding process check the documentation e.g. grep returns a 1 if a search found nothing sed return a 0 for the same result

Logical operators Conditional execution: cmd1 && cmd2 cmd2 executed only if cmd1 succeeds cmd1 || cmd2 cmd2 executed only if cmd1 fails Works well with the exit statement Notes: If cmd2 needs to be 2 or more statements Enclose them with curly braces {} Separate them with semicolons ; Good for making simple choices

Logical operators Example: logicalop.sh Note problem if no parameter #!/bin/bash # needs one parameter echo Listing filelist cat filelist echo ===done=== echo looking for $1 grep $1 filelist > /dev/null && echo "string $1 found" grep $1 filelist || echo "string $1 not found"

if Used to make more complex decisions General Syntax: if command(is good) then do some commands elif command(is good) then do some commands else (all the above fails) do some commands fi The elif (purple) can be repeated 0 or more times The else (green) can be used 0 or 1 times command returns 0 (good) or 1 (or any non-0) command is a script or Linux command NOTE: No () or {} are needed

if Example ./ifvmv.sh ifmv.sh: Testing: #./ifmv x y move worked #./ifmv x y mv: cannot stat “x' No such file or directory move failed Notes: First time file x existed and was moved Second time file x was gone std error message file not found from system std out message from ifmv.sh ./ifvmv.sh Create a file beforehand with touch x Check with ls Test with ./ifmv.sh x y Check to see if the move worked, then try again if mv $1 $2 then echo move worked else echo move failed fi

test and [] if only looks at the results (exit code) of a command cannot handle relational tests directly test does conditionals Typical Syntax: test val1 -conditional val2 test –conditional val [ ] Shorthand for test The following are equivalent test $1 –eq $2 [ $1 –eq $2 ] Note: the white space is required Has comparators for: Numerical relations String comparisons File status

test and [] Numeric comparisons test val1 –op val2 Examples: test $# –eq 2 Checks if there are 2 arguments [$1 –ne 5] Is the first argument value not 5 Numeric Comparators Op Meaning -eq Equal to -ne Not equal -gt Greater than -ge Greater or equal -lt Less than -le Less or equal

test Example if test $# -ne 2 then echo "This program requires 2 args" exit 1 fi A quick test to see if enough arguments were passed to this script

test for strings and files File Attribute Tests Test True if -f file file exists and is a regular file -r file file exists and is readable -w file file exists and is writable -x file file exists and is executable -d file file exists and is a directory -s file file exists and is > 0 -u file file exists and has SUID set -k file file exists and has sticky set -e file file exists (Korn and Bash) -L file file exists as symbolic link (KandB) f1 –nt f2 Newer (K and B) f1 –ot f2 Older (K and B) f1 –ef f2 Linked (K and B) String Tests Test True if s1 = s2 Strings equal s1 != s2 Strings not equal -n str str is not null -z str str is null str str is assigned and not null s1 == s2 s1 = s2 (Korn and Bash)

case Useful when there are multiple related choices Syntax easier than using if…elif…fi at times Syntax case expression in pat1) commands ;; pat2) commands ;; … esac

case Example echo; echo "Hit a key, then hit return." read Keypress case "$Keypress" in [[:lower:]] ) echo "Lower" ;; [[:upper:]] ) echo "Upper" ;; [0-9] ) echo "Digit" ;; * ) echo “Other" ;; esac ./keypress.sh

Computation and string handling expr Does the 4 basic arithmetic operations + modulus Some logic Integers only Options to work with strings bc Does floating point basename Return only the file name From an absolute filename From a relative filename Can also strip a string from the end

expr Example add: # expr 3 + 5 8 # x=5 y=3 # expr $x + $y 8 Space is required around operand + # expr 3+5 3+5 happens otherwise # x=5 y=3 # expr $x + $y 8 can use variables Note: a ; wasn't needed between assignments in this case

expr Example multiply: Example divide and modulus: the * needs to be escaped, otherwise get a syntax error Example divide and modulus: # expr 5 / 3 1 # expr 5 % 3 2 Example add and assign to a variable: # x=6 ; y=2 # z=`expr $x + $y` ; echo $z 8 use command substitution to get result assigned to a variable

expr Can also process strings Uses two expressions separated by a : String on the left Regular expression on the right

expr – string length Return the length of a string Examples: # str="This is a string" # expr "$str" : '.*' 16 Note the " around the string variable # str="This is a string" # if [ `expr "$str" : '.*'` -gt 10 ] > then echo "too long" > fi too long Can be used to check if a string is the right size Note: The if can be done on one line - if [ `expr "$str" : '.*'` -gt 10 ] ; then echo "too long"; fi

expr - substring Substrings Uses tagged regular expression (TRE) Example: # str=1234 # expr "$str" : '.\(..\)' 23 # str=abcde # expr "$str" : '..\(.\)' c #

Resume 1/23

bc Floating point computation Can be used interactively Can be used "stand-alone"

bc – interactive Example 1: Example 2: # x=5.5 y=6 # echo "$x + $y" | bc 11.5 Note the pipe must be used Example 2: # x=5.5 y=4 # z=`echo "$x + $y" | bc` # echo $z 9.5 Note the use of command substitution to assign value to z

bc - standalone Used a calculator: # bc bc 1.06.95 Copyright 1991…2006 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. scale=3 5 / 3 1.666 5 * 3 15 scale=7 22 / 7 3.1428571 quit # Note: scale sets the number of decimal places

basename Getting base name: Stripping a string Example: # pwd /home/ajkombol/bashexamples # ls case.sh # cd .. # basename /home/ajkombol/bashexamples/case.sh case.sh # basename bashexamples/case.sh case.sh Stripping a string # basename abc.xyz .xyz abc # basename abc.xyz .xy abc.xyz # basename abc.xyz yz abc.x # Notice it only strips off at the end

for Loop using a list Syntax: for var in list do commands done Note: do is ended with done instead of od There is an od command to dump files in octal

for Example1: # ls file* file1 file3 file5 # for file in file1 file3 file5 > do cp $file ${file}.bak > done # ls file* file1 file1.bak file3 file3.bak file5 file5.bak Quick backup for files in list Note use of curly braces {} Example2: # ls x* xfile1 xfile3 xfile5 xfilelist # cat xfilelist xfile1 xfile3 xfile5 xfile7 # for file in `cat xfilelist` > do cp $file ${file}.bak > done cp: cannot stat `xfile7': No such file or directory # ls x* xfile1 xfile1.bak xfile3 xfile3.bak xfile5 xfile5.bak xfilelist xfilelist contains a list of files to back up Note the std err for xfile7, but all else backed up

while Loop while a condition is true Syntax: while condition do commands done

while Example # x=3 # while [ $x -gt 0 ] > do date ; sleep 3 > x=`expr $x - 1` > done Fri Mar 29 23:28:26 EDT 2013 Fri Mar 29 23:28:29 EDT 2013 Fri Mar 29 23:28:32 EDT 2013 #

break and continue Alters the normal flow of a looping structure break stops the processing at that point continues after the loop structure E.g. don’t loop any more… continue continues at the next iteration of the loop Continue looping wit the next iteration with next values

break cat stopwhenreadable.sh while true ; do [ -r $1 ] && break sleep 1 done Example # touch noread # chmod 000 noread # ls -l noread ---------- 1 ajkombol ajkombol 0 Mar 29 23:37 noread # ./stopwhenreadable.sh & # ps PID TTY TIME CMD 14668 pts/4 00:00:00 bash 23672 pts/4 00:00:00 ps # ./stopwhenreadable.sh noread & [1] 23712 # ps PID TTY TIME CMD 14668 pts/4 00:00:00 bash 23712 pts/4 00:00:00 bash 23726 pts/4 00:00:00 sleep 23727 pts/4 00:00:00 ps # set -o notify …… # chmod +r noread # [1]+ Done ./stopwhenreadable.sh noread Notes: & above starts the program as a background job and returns control to the terminal the last line does not appear until the program is halted first test did not have a parm, so –r $1 passed, interesting… second test was passed noread which was not readable, it looped until it was made readable ???? Odd behavior with no parm Works better with the parm Notify when a background job ends

continue Use example: Only backup a file in a list if it is older than 3 weeks Use the option to get time in seconds from epoch date +%s stat –c%Y Logic: get current date calculate 3 weeks back set up for loop for list of files get the file date if less than 3 weeks Continue backup end of loop

set and shift set shift Example: ./setshift.sh sets values to arguments $1, $2, … and $#, $* and $@ usually used with a command substitution e.g. set `date` shift removes the first argument and renumbers the remainders shift n will get rid of the first n entries Example: # set cat dog # echo $1 $2 cat dog # echo $# $1 $2 2 cat dog # shift # echo $# $1 $2 1 dog # Notes: $# is the number of arguments shift removed cat ./setshift.sh cat setshift.txt set `date` echo $@ echo getting rid of the first with shift shift

Here Document (<<) Use a following block of data as input Syntax: someprog << BLOCK > data1 > data2 > BLOCK Notes: << starts the Here Document The next word is the start delimiter Traditionally in uppercase Doesn't need to be In the example above it is BLOCK, but can be any unique word not in the data stream Followed by data Ended by repeating the start delimiter Data between the delimiter is used as input for someprog

here docs Example Wall sends a message (from a file) to all users: #wall < msg.txt Broadcast Message from tkombol@cciwd333 (/dev/pts/3) at 10:09 ... Going down soon. Save your work! #wall << ZZZZ > This > is > a > test > ZZZZ (/dev/pts/3) at 10:15 ... This is a test #cat msg.txt Going down soon. Save your work!

here docs Example # tr A-Z a-z <<-ZZZ > Test > ZZZ test # The – signals to remove leading tabs Notes: No space between << and – tr translates

here string (<<<) Examples: # tr a-z A-Z <<< "This Is A String" THIS IS A STRING # # set This Is A Test # echo $@ This Is A Test # tr a-z A-Z <<< $@ THIS IS A TEST # Notes: set set 4 parms tr took the 4 parms ($@) and translated, output to std out

Shell functions Create your own little routines Syntax: function_name() { statements return value } The italisized parts are supplied by the coder () is always empty but "required" { } surrounds the code

Shell Functions Example: # ./func1.sh Dog Cat Bye! # Notes: # cat func1.sh function quit() { echo Bye! exit } function e1 { echo $1 e1 Dog e1 Cat quit echo here? # Example: # ./func1.sh Dog Cat Bye! # Notes: () not really required in this shell Parms used with $n

trap Scripts terminate when the interrupt key is pressed (a signal) Typically <ctrl>-c SIGINT There are other interrupts also Traps specify an alternate action Typical use is to clean up on termination

trap # cat trapexample.sh trap "echo I am done" SIGINT echo "pid is $$" while : do sleep 5 done # Example: # ./trapexample.sh pid is 18230 ^CI am done ^CI am done ^CI am done Terminated # Notes <ctrl>-c will no longer kill the process It has been replaced with the echo An external process needed to stop it kill 18230 the terminated above is not seen until the kill is executed SIGINT is <ctrl>-c

Signals Software interrupts to indicate an important event has occurred kill –l will list the signals on your system # kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX #

eval Evaluates the values before trying to process See following example

eval Example: # cmd="ls | more" # $cmd ls: cannot access |: No such file or directory ls: cannot access more: No such file or directory # eval $cmd bigfile1 bigfile2 xfile1 xfile1.bak xfile3 xfile3.bak xfilelist # $cmd interprets ls | more as a command with 2 arguments eval $cmd evaluates $cmd then executes it properly

exec execute a new shell within this process when the shell is done you are usually logged out useful to restrict access by escaping can also be used to redirect std in or std out