CSE 391 Lecture 5. Intro to shell scripting

Similar documents
CSE 390a Lecture 5. Intro to shell scripting

CSE 303 Lecture 4. users/groups; permissions; intro to shell scripting. read Linux Pocket Guide pp , 25-27, 61-65, , 176

CSE 390a Lecture 1. introduction to Linux/Unix environment

CSE 391 Lecture 1. introduction to Linux/Unix environment

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

CSE 391 Lecture 1. introduction to Linux/Unix environment

CSE 390a Lecture 3. bash shell continued: processes; multi-user systems; remote login; editors

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

CSE 391 Lecture 3. bash shell continued: processes; multi-user systems; remote login; editors

Chapter 4. Unix Tutorial. Unix Shell

CENG 334 Computer Networks. Laboratory I Linux Tutorial

Intro to Linux & Command Line

CSE 390a Lecture 6. bash scripting continued; remote X windows; unix tidbits

Shell Start-up and Configuration Files

Introduction: What is Unix?

Essentials for Scientific Computing: Bash Shell Scripting Day 3

CSE 390a Lecture 2. Exploring Shell Commands, Streams, Redirection, and Processes

CSE 390a Lecture 4. Persistent shell settings; users/groups; permissions

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

Practical Session 0 Introduction to Linux

Exploring UNIX: Session 3

CSE 390a Lecture 3. Multi-user systems; remote login; editors; users/groups; permissions

Command-line interpreters

Basic Linux (Bash) Commands

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 4 Shell Variables, More Shell Scripts (Thanks to Hal Perkins)

CSE 390a Lecture 4. Persistent shell settings; users/groups; permissions

CSE 391 Editing and Moving Files

Useful Unix Commands Cheat Sheet

Introduction to Supercomputing

CSE 374: Programming Concepts and Tools. Eric Mullen Spring 2017 Lecture 4: More Shell Scripts

CS Unix Tools & Scripting

Lecture 4. Log into Linux Reminder: Homework 1 due today, 4:30pm Homework 2 out, due next Tuesday Project 1 out, due next Thursday Questions?

Part 1: Basic Commands/U3li3es

Bourne Shell (ch 8) Overview. Bourne Shell. Bourne Shell. Bourne Shell. Bourne Shell. Redirect standard error. Redirect standard error

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Today. Review. Unix as an OS case study Intro to Shell Scripting. What is an Operating System? What are its goals? How do we evaluate it?

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Exercise 1: Basic Tools

CS370 Operating Systems

Introduction to Linux for BlueBEAR. January

Processes. Shell Commands. a Command Line Interface accepts typed (textual) inputs and provides textual outputs. Synonyms:

bash Data Administrative Shell Scripting COMP2101 Fall 2017

Shell Programming (bash)

CSE 15L Winter Midterm :) Review

Shell script/program. Basic shell scripting. Script execution. Resources. Simple example script. Quoting

Output with printf Input. from a file from a command arguments from the command read

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

Shell Scripting. With Applications to HPC. Edmund Sumbar Copyright 2007 University of Alberta. All rights reserved

Virtual Machine. Linux flavor : Debian. Everything (except slides) preinstalled for you.

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 1

Introduction to Linux Part 2b: basic scripting. Brett Milash and Wim Cardoen CHPC User Services 18 January, 2018

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Shell Scripting. Winter 2019

Getting Started With UNIX Lab Exercises

Linux at the Command Line Don Johnson of BU IS&T

CSE 390a Lecture 6. bash scrip'ng con'nued; remote X windows; unix 'dbits

EECS2301. Example. Testing 3/22/2017. Linux/Unix Part 3. for SCRIPT in /path/to/scripts/dir/* do if [ -f $SCRIPT -a -x $SCRIPT ] then $SCRIPT fi done

CSE 390a Lecture 2. Exploring Shell Commands, Streams, and Redirection

Linux File System and Basic Commands

1. Hello World Bash Shell Script. Last Updated on Wednesday, 13 April :03

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

CS101 Linux Shell Handout

Bash scripting Tutorial. Hello World Bash Shell Script. Super User Programming & Scripting 22 March 2013

Lecture 8. Introduction to Shell Programming. COP 3353 Introduction to UNIX

Unix Introduction to UNIX

28-Nov CSCI 2132 Software Development Lecture 33: Shell Scripting. 26 Shell Scripting. Faculty of Computer Science, Dalhousie University

COMP 2718: Shell Scripts: Part 1. By: Dr. Andrew Vardy

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

Getting started with Hugs on Linux

Basics. proper programmers write commands

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Shell scripting and system variables. HORT Lecture 5 Instructor: Kranthi Varala

Linux Shell Script. J. K. Mandal

Week 2 Lecture 3. Unix

Linux Systems Administration Shell Scripting Basics. Mike Jager Network Startup Resource Center

CSE 390a Lecture 6. bash scripting continued; remote X windows; unix tidbits

Operating Systems. Copyleft 2005, Binnur Kurt

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

Shells & Shell Programming (Part B)

Introduction of Linux

Session 1: Accessing MUGrid and Command Line Basics

Unix Shell scripting. Dr Alun Moon 7th October Introduction. Notation. Spaces

Linux Operating System Environment Computadors Grau en Ciència i Enginyeria de Dades Q2

Getting started with Hugs on Linux

Essential Linux Shell Commands

Linux Training. for New Users of Cluster. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

CS246-Assign01 V1.1: Winter 2013

This lab exercise is to be submitted at the end of the lab session! passwd [That is the command to change your current password to a new one]

Introduction to UNIX Command Line

Systems Programming/ C and UNIX

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

Introduction: The Unix shell and C programming

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Introduction to Unix. University of Massachusetts Medical School. October, 2014

The Unix Shell & Shell Scripts

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

Basic Unix Command. It is used to see the manual of the various command. It helps in selecting the correct options

Guided Tour (Version 3.3) By Steven Castellucci as Modified by Brandon Haworth

Intro to Linux. this will open up a new terminal window for you is super convenient on the computers in the lab

Transcription:

CSE 391 Lecture 5 Intro to shell scripting slides created by Marty Stepp, modified by Jessica Miller & Ruth Anderson http://www.cs.washington.edu/391/ 1

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

Shell scripts script: A short program meant to perform a targeted task. 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! 3

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 4

Running a shell script by making it executable (most common; recommended): chmod u+x myscript.sh./myscript.sh fork a process and run commands in myscript.sh and exit by launching a new shell : bash myscript.sh advantage: can run without execute permission (still need read permission) by running it within the current shell: source myscript.sh advantage: any variables defined by the script remain in this shell (more on variables later) 5

echo command echo description produces its parameter(s) as output (the println of shell scripting) -n flag to remove newline (print vs println) Example: A script that prints your current directory. #!/bin/bash echo "This is my amazing script!" echo "Your current dir is: $(pwd)" Exercise : Write a script that when run on attu does the following: clears the screen displays the current date/time Shows who is currently logged on & info about processor 6

7 Script example #!/bin/bash clear # please do not use clear in your hw scripts! echo "Today's date is $(date)" echo echo "These users are currently connected:" w -h sort echo echo "This is $(uname s) on a $(uname m) processor." echo echo "This is the uptime information:" uptime echo echo "That's all folks!"

8 Comments # comment text 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 current directory echo Current dir is: $(pwd)"

Shell variables name=value (declaration) 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=64 NAME="Michael Young" $name (usage) echo "$NAME is $AGE years old" Produces: Michael Young is 64 years old 9

10 Common errors if you misspell a variable's name, a new variable is created NAME=Ruth... Name=Rob # 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=Ruth Anderson # Won t work NAME= Ruth Anderson" # $NAME is Ruth Anderson

More Errors Using $ during assignment or reassignment $mystring= Hi there # error mystring2= Hello $mystring2= Goodbye # error Forgetting echo to display a variable $name echo $name 11

12 Capture command output variable=$(command) captures the output of command into the given variable Simple Example: FILE=$(ls *.txt) echo $FILE More Complex Example: FILE=$(ls -1 *.txt sort tail n 1) echo "Your last text file is: $FILE" What if we use double quotes instead?

Double vs. Single quotes Double quotes - Variable names are expanded & $() work NAME="Bugs Bunny" echo "Hi $NAME! Today is $(date)" Produces: Hi Bugs Bunny! Today is Tues Apr 25 13:37:45 PDT 2017 Single quotes don t expand variables or execute commands in $() echo 'Hi $NAME! Today is $(date)' Produces: Hi $NAME! Today is $(date) Tricky Example: STAR=* echo "You are a $STAR" echo 'You are a $STAR' echo You are a $STAR Lesson: When referencing a variable, it is good practice to put it in double quotes. 13

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 14

15 Bash vs. Java Java Bash 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 x=3 x vs. $x vs. "$x" vs. '$x' vs. \'$x\' vs. 'x' s2=25 s3=42 s4=$s2$s3 let n="$s2 + $s3"

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

$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/rea What happens if we clear the $PATH variable? 17

set, unset, and export shell command set unset export readonly description sets the value of a variable (not usually needed; can just use x=3 syntax) deletes a variable and its value sets a variable and makes it visible to any programs launched by this shell sets a variable to be read-only (so that programs launched by this shell cannot change its value) 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 and launch a shell again. Can you see it now? 18

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

Command-line arguments variable description $0 name of this script $1, $2, $3,... command-line arguments $# number of arguments $@ array of all 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 20

21 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 done

22 for loop examples for val in red blue green; do echo "val is: $val" done for val in $@; do echo "val is: $val" done for val in $(seq 4); do echo "val is: $val" done command seq description outputs a sequence of numbers

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... 23

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