CSE 390a Lecture 5 Intro to shell scripting

Slides:



Advertisements
Similar presentations
Find Command Characteristics –Locate files descending from multiple starting points –Employs regular expressions Examples On entire system: >find / -name.
Advertisements

CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
1 CSE 303 Lecture 6 more Unix commands; bash scripting continued read Linux Pocket Guide pp , 82-88, slides created by Marty Stepp
1 CSE 303 Lecture 4 users/groups; permissions; intro to shell scripting read Linux Pocket Guide pp , 25-27, 61-65, , 176 slides created by.
1 CSE 390a Lecture 5 Intro to shell scripting slides created by Marty Stepp, modified by Jessica Miller
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Linux & Shell Scripting Small Group Lecture 4 How to Learn to Code Workshop group/ Erin.
1 SEEM3460 Tutorial Unix Introduction. 2 Introduction What is Unix? An operation system (OS), similar to Windows, MacOS X Why learn Unix? Greatest Software.
Chapter 15 Introductory Bash Programming
Introduction to Shell Script Programming
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.
An Introduction to Unix Shell Scripting
Unix Tutorial for FreeSurfer Users. Helpful To Know FreeSurfer Tutorial Wiki:
Unix Tutorial for FreeSurfer Users. Helpful To Know FreeSurfer Tutorial Wiki:
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Linux Operations and Administration
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
1 CSE 303 Lecture 5 bash continued: users/groups; permissions; intro to scripting read Linux Pocket Guide pp slides created by Marty Stepp
1 CSE 303 Lecture 3 bash shell continued: processes; multi-user systems; combining commands read Linux Pocket Guide pp , , , 118, 122,
CS252: Systems Programming Ninghui Li Slides by Prof. Gustavo Rodriguez-Rivera Topic 7: Unix Tools and Shell Scripts.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Linux Commands C151 Multi-User Operating Systems.
1 CSE 390a Lecture 3 bash shell continued: processes; multi-user systems; remote login; editors slides created by Marty Stepp, modified by Jessica Miller.
Learning Unix/Linux Based on slides from: Eric Bishop.
Linux Administration Working with the BASH Shell.
1 CSE 391 Lecture 5 Intro to shell scripting slides created by Marty Stepp, modified by Jessica Miller & Ruth Anderson
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu.
Intro to shell scripting
CIRC Winter Boot Camp 2017 Baowei Liu
CSE 374 Programming Concepts & Tools
slides created by Marty Stepp, modified by Josh Goodwin
Intro to shell scripting
Prepared by: Eng. Maryam Adel Abdel-Hady
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
Part 1: Basic Commands/Utilities
CSE 374 Programming Concepts & Tools
Some Linux Commands.
Shell Scripting March 1st, 2004 Class Meeting 7.
The Linux Operating System
CSE 374 Programming Concepts & Tools
Intro to shell scripting
CSE 303 Concepts and Tools for Software Development
INTRODUCTION TO UNIX: The Shell Command Interface
Basic UNIX OLC Training.
Using Linux Commands Lab 3.
CSE 390a Lecture 3 bash shell continued: processes; multi-user systems; remote login; editors slides created by Marty Stepp, modified by Jessica Miller.
slides created by Marty Stepp, modified by Jessica Miller
UNIX Reference Sheets CSE 2031 Fall 2010.
Intro to shell scripting
Persistent shell settings; intro to shell scripting
CSE 390a Lecture 3 bash shell continued: processes; multi-user systems; remote login; editors slides created by Marty Stepp, modified by Jessica Miller.
CSE 303 Concepts and Tools for Software Development
Linux Shell Script Programming
slides created by Marty Stepp, modified by Jessica Miller
Intro to shell scripting
Intro to shell scripting
CSE 303 Concepts and Tools for Software Development
CSE 390a Lecture 3 bash shell continued: processes; multi-user systems; remote login; editors slides created by Marty Stepp, modified by Jessica Miller.
CSE 390a Lecture 3 bash shell continued: processes; multi-user systems; remote login; editors slides created by Marty Stepp, modified by Jessica Miller.
CSE 390a Lecture 3 bash shell continued: processes; multi-user systems; remote login; editors slides created by Marty Stepp, modified by Jessica Miller.
Module 6 Working with Files and Directories
Intro to shell scripting
Intro to shell scripting
Intro to shell scripting
Introduction to Bash Programming, part 3
Intro to shell scripting
Basic shell scripting CS 2204 Class meeting 7
Presentation transcript:

CSE 390a Lecture 5 Intro to shell scripting slides created by Marty Stepp, modified by Josh Goodwin http://www.cs.washington.edu/390a/

Lecture summary basic script syntax and running scripts shell variables and types control statements: the for loop

Shell scripts script: A short program whose purpose is to run other programs. a series of commands combined into one executable file shell script: A script that is executed by a command-line shell. bash (like most shells) has syntax for writing script programs if your script becomes > ~100-150 lines, switch to a real language To write a bash script (in brief): type one or more commands into a file; save it type a special header in the file to identify it as a script (next slide) enable execute permission on the file run it!

Basic script syntax #!interpreter written as the first line of an executable script; causes a file to be treated as a script to be run by the given interpreter (we will use /bin/bash as our interpreter) Example: A script that removes some files and then lists all files: #!/bin/bash rm output*.txt ls -l

Running a shell script by making it executable (most common; recommended): chmod u+x myscript.sh ./myscript.sh by launching a new shell: bash myscript.sh by running it within the current shell: source myscript.sh advantage: any variables defined by the script remain in this shell (seen later)

echo Example: A script that prints the time and your home directory. #!/bin/bash echo "This is my amazing script!" echo "Your home dir is: `pwd`" Exercise : Write a script that when run on attu does the following: clears the screen displays the date/time: Today’s date is Mon Apr 27 12:00:00 PDT 2010 shows me an ASCII cow welcoming my user name command description echo produces its parameter(s) as output (the println of shell scripting) -n flag to remove newline (print vs println)

Script example #!/bin/bash clear echo "Today's date is `date`" echo ~stepp/cowsay `whoami` echo "These users are currently connected:" w | grep -v USER | sort echo "This is `uname -s` on a `uname -m` processor." echo "This is the uptime information:" uptime echo "That's all folks!"

Comments # comment text Example: bash has only single-line comments; there is no /* ... */ equivalent Example: #!/bin/bash # Leonard's first script ever # by Leonard Linux echo "This is my amazing script!" echo "The time is: `date`" # This is the part where I print my home directory echo "Home dir is: `pwd`"

Shell variables name=value (declaration) $name (usage) must be written EXACTLY as shown; no spaces allowed often given all-uppercase names by convention once set, the variable is in scope until unset (within the current shell) AGE=14 NAME="Marty Stepp" $name (usage) echo "$NAME is $AGE" Marty Stepp is 14

Common errors if you misspell a variable's name, a new variable is created NAME=Marty ... Name=Daniel # oops; meant to change NAME if you use an undeclared variable, an empty value is used echo "Welcome, $name" # Welcome, when storing a multi-word string, must use quotes NAME=Marty Stepp # $NAME is Marty NAME="Marty Stepp" # $NAME is Marty Stepp

More Errors… Using $ during assignment or reassignment $mystring=“Hi there” # error mystring2=“Hello” … $mystring2=“Goodbye” # error

Capture command output variable=`command` captures the output of command into the given variable Example: FILE=`ls -1 *.txt | sort | tail -c 1` echo "Your last text file is: $FILE" What if we leave off the last backtick? What if we use quotes instead?

Types and integers most variables are stored as strings operations on variables are done as string operations, not numeric to instead perform integer operations: x=42 y=15 let z="$x + $y" # 57 integer operators: + - * / % bc command can do more complex expressions if a non-numeric variable is used in numeric context, you'll get 0

Bash vs. Java x=3 x vs. $x vs. "$x" vs. '$x' vs. \'$x\' vs. 'x' Java String s = "hello"; s=hello System.out.println("s"); echo s System.out.println(s); echo $s s = s + "s"; // "hellos" s=${s}s String s2 = "25"; String s3 = "42"; String s4 = s2 + s3; // "2542" int n = Integer.parseInt(s2) + Integer.parseInt(s3); // 67 s2=25 s3=42 s4=$s2$s3 let n="$s2 + $s3"

Special variables these are automatically defined for you in every bash session Exercise : Change your attu prompt to look like this: jimmy@mylaptop:$ variable description $DISPLAY where to display graphical X-windows output $HOSTNAME name of computer you are using $HOME your home directory $PATH list of directories holding commands to execute $PS1 the shell's command prompt string $PWD your current directory $SHELL full path to your shell program $USER your user name

$PATH When you run a command, the shell looks for that program in all the directories defined in $PATH Useful to add commonly used programs to the $PATH Exercise: modify the $PATH so that we can directly run our shell script from anywhere echo $PATH PATH=$PATH:/homes/iws/dravir What happens if we clear the $PATH variable?

set, unset, and export typing set or export with no parameters lists all variables Exercise: set a local variable, and launch a new bash shell Can the new shell see the variable? Now go back and export. Result? shell command description set sets the value of a variable (not usually needed; can just use x=3 syntax) unset deletes a variable and its value export sets a variable and makes it visible to any programs launched by this shell readonly sets a variable to be read-only (so that programs launched by this shell cannot change its value)

Console I/O Example: variables read from console are stored as strings #!/bin/bash read -p "What is your name? " name read -p "How old are you? " age printf "%10s is %4s years old" $name $age shell command description read reads value from console and stores it into a variable echo prints output to console printf prints complex formatted output to console

Command-line arguments Example.sh: #!/bin/bash echo “Name of script is $0” echo “Command line argument 1 is $1” echo “there are $# command line arguments: $@” Example.sh argument1 argument2 argument3 variable description $0 name of this script $1, $2, $3, ... command-line arguments $# number of arguments $@ array of all arguments

for loops for name in value1 value2 ... valueN; do commands done Note the semi-colon after the values! the pattern after in can be: a hard-coded set of values you write in the script a set of file names produced as output from some command command line arguments: $@ Exercise: create a script that loops over every .txt file in the directory, renaming the file to .txt2 for file in *.txt; do mv $file ${file}2

Exercise Write a script createhw.sh that creates directories named hw1, hw2, ... up to a maximum passed as a command-line argument. $ ./createhw.sh 8 Copy criteria.txt into each assignment i as criteria(2*i).txt Copy script.sh into each, and run it. output: Script running on hw3 with criteria6.txt ... The following command may be helpful: command description seq outputs a sequence of numbers

Exercise solution #!/bin/bash # Creates directories for a given number of assignments. for num in `seq $1`; do let CNUM="2 * $num" mkdir "hw$num" cp script.sh "hw$num/" cp criteria.txt "hw$num/criteria$CNUM.txt" echo "Created hw$num." cd "hw$num/" bash ./script.sh cd .. done