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

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

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

bash Execution Control COMP2101 Winter 2019

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

Scripting. More Shell Scripts. Adapted from Practical Unix and Programming Hunter College

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

Control Structures. CIS 118 Intro to LINUX

COMP 4/6262: Programming UNIX

CSE 391 Editing and Moving Files

CSE 390a Lecture 5. Intro to shell scripting

CSE 391 Lecture 5. Intro to shell scripting

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

Conditional Control Structures. Dr.T.Logeswari

example: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable.

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

CS Unix Tools & Scripting

Shell programming. Introduction to Operating Systems

Title:[ Variables Comparison Operators If Else Statements ]

Utilities. September 8, 2015

Shell Programming (Part 2)

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

Computer Systems and Architecture

Vi & Shell Scripting

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

A shell can be used in one of two ways:

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

Preview. Review. The test, or [, command. Conditions. The test, or [, command (String Comparison) The test, or [, command (String Comparison) 2/5/2019

CSC 2500: Unix Lab Fall 2016

OPERATING SYSTEMS LAB LAB # 6. I/O Redirection and Shell Programming. Shell Programming( I/O Redirection and if-else Statement)

Handling Ordinary Files

CSE 391 Lecture 1. introduction to Linux/Unix environment

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

9.2 Linux Essentials Exam Objectives

Bourne Shell Reference

Useful Unix Commands Cheat Sheet

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

Basic Linux (Bash) Commands

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

Computer Systems and Architecture

Shell Start-up and Configuration Files

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Linux shell programming for Raspberry Pi Users - 2

Shells & Shell Programming (Part B)

CSE 391 Lecture 1. introduction to Linux/Unix environment

Shell Programming (bash)

Assignment 3, Due October 4

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

Bash scripting basics

Lab 1 Introduction to UNIX and C

Prof. Navrati Saxena TA: R. Sachan Bharat Jyoti Ranjan

Intro to Linux & Command Line

Shell Control Structures

Shell Control Structures

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

COMP2100/2500 Lecture 17: Shell Programming II

More Scripting Techniques Scripting Process Example Script

Lab 5a Shell Script Lab 4 Using Arithmetic Operators in shell script

Introduction to remote command line Linux. Research Computing Team University of Birmingham

Cisco IOS Shell. Finding Feature Information. Prerequisites for Cisco IOS.sh. Last Updated: December 14, 2012

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

12.1 UNDERSTANDING UNIX SHELL PROGRAMMING LANGUAGE: AN INTRODUCTION Writing a Simple Script Executing a Script

There are some string operators that can be used in the test statement to perform string comparison.

Essentials for Scientific Computing: Bash Shell Scripting Day 3

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

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

Shell Script Example. Here is a hello world shell script: $ ls -l -rwxr-xr-x 1 horner 48 Feb 19 11:50 hello* $ cat hello #!/bin/sh

Topic 2: More Shell Skills

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

LING 408/508: Computational Techniques for Linguists. Lecture 5

Chapter 5 The Bourne Shell

Fall Lecture 5. Operating Systems: Configuration & Use CIS345. The Linux Utilities. Mostafa Z. Ali.

Lecture 02 The Shell and Shell Scripting

Sub-Topic 1: Quoting. Topic 2: More Shell Skills. Sub-Topic 2: Shell Variables. Referring to Shell Variables: More

Operating Systems. Lab Manual. Compiled by: Mr. Mohammed Al-Qadhi Computer Science Department

Last Time. on the website

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

A Brief Introduction to the Linux Shell for Data Science

EECS2301. Special variables. $* and 3/14/2017. Linux/Unix part 2

Unit: Making a move (using FTP)

Unzip command in unix

DAVE LIDDAMENT INTRODUCTION TO BASH

Grep and Shell Programming

Lab 4: Shell scripting

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

While Statement Examples. While Statement (35.15) Until Statement (35.15) Until Statement Example

Bash shell programming Part II Control statements

UNIX shell scripting

Basic UNIX Commands BASIC UNIX COMMANDS. 1. cat command. This command is used to create a file in unix. Syntax: $ cat filename

INTRODUCTION TO UNIX

Linux Bash Shell Scripting

Manual Shell Script Linux If Not Equal String Comparison

CSE 15L Winter Midterm :) Review

Shell Programming. Introduction to Linux. Peter Ruprecht Research CU Boulder

EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018

Practical 04: UNIX / LINUX Shell Programming 1

Assignment clarifications

7. Archiving and compressing 7.1 Introduction

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash

Transcription:

CSE 390a Lecture 6 bash scripting continued; remote X windows; unix tidbits slides created by Marty Stepp, modified by Jessica Miller and Ruth Anderson http://www.cs.washington.edu/390a/ 1

Lecture summary more shell scripting if/else while/until select/case advanced: arrays and functions Remote editing/gui various new Unix/Linux commands file archiving and compression shell history newlines in Unix vs Windows 2

3 if/else if [ test ]; then commands fi if [ test ]; then commands1 elif [ test ]; then commands2 else commands3 fi # basic if # if / else if / else The [ ] syntax is actually shorthand for a shell command called test (Try: man test ) there MUST be spaces as shown: if space [ space test space ] include the semi-colon after ] (or put then on the next line)

test operators comparison operator if [ $USER = husky13" ]; then echo Woof! Go Huskies! fi description =,!=, <, > compares two string variables -z, -n -lt, -le, -eq, -gt, -ge, -ne -e, -f, -d -r, -w tests if a string is empty (zero-length) or not empty (nonzero-length) compares numbers; equivalent to Java's <, <=, ==, >, >=,!= tests whether a given file or directory exists tests whether a file exists and is read/writable LOGINS=`w -h wc -l` if [ $LOGINS -gt 10 ]; then echo attu is very busy right now! fi *Note: man test will show other operators. 4

More if testing compound comparison operators if [ expr1 -a expr2 ]; then... if [ test1 ] && [ test2 ]; then... if [ expr1 -o expr2 ]; then... if [ test1 ] [ test2 ]; then... if [! expr ]; then... description and or not # alert user if running >= 10 processes when # attu is busy (>= 5 users logged in) LOGINS=`w -h wc -l` PROCESSES=`ps -u $USER wc -l` if [ $LOGINS -ge 5 -a $PROCESSES -gt 10 ]; then echo "Quit hogging the server!" fi 5

6 safecopy Exercise Write a script called safecopy that will mimic the behavior of cp -i: $ cp i from.txt to.txt Do you want to overwrite to.txt? (yes/no) $./safecopy from.txt to.txt Do you want to overwrite to.txt? (yes/no)

safecopy Exercise Solution #!/bin/bash FROM=$1 TO=$2 if [ -e $TO ]; then read -p "Do you want to overwrite $TO?" ANSWER if [ $ANSWER = "yes" ]; then cp $FROM $TO fi else cp $FROM $TO fi 7

8 BMI Exercise Write a program that computes the user's body mass index (BMI) to the nearest integer, as well as the user's weight class: weight BMI 703 2 height $./bmi Usage:./bmi weight height BMI Weight class 18 underweight 18-24 normal 25-29 overweight 30 obese $./bmi 112 72 Your Body Mass Index (BMI) is 15 Here is a sandwich; please eat. $./bmi 208 67 Your Body Mass Index (BMI) is 32 There is more of you to love.

9 BMI Exercise solution #!/bin/bash # Body Mass Index (BMI) calculator if [ $# -lt 2 ]; then echo "Usage: $0 weight height" exit 1 # 1 indicates failure, 0 for success fi let H2= $2 * $2 let BMI="703 * $1 / $H2" echo "Your Body Mass Index (BMI) is $BMI" if [ $BMI -le 18 ]; then echo "Here is a sandwich; please eat." elif [ $BMI -le 24 ]; then echo "You're in normal weight range." elif [ $BMI -le 29 ]; then echo "You could stand to lose a few." else echo "There is more of you to love." fi

Common errors [: -eq: unary operator expected you used an undefined variable in an if test [: too many arguments you tried to use a variable with a large, complex value (such as multiline output from a program) as though it were a simple int or string let: syntax error: operand expected (error token is " ") you used an undefined variable in a let mathematical expression 10

11 while and until loops while [ test ]; do commands done # go while test is true until [ test ]; do commands done # go while test is false

12 While exercise Prompt the user for what they would like to do. While their answer is open the pod bay doors tell them that you cannot do that and prompt for another action.

13 While Exercise solution #!/bin/bash # What would you like to do? read -p "What would you like me to do? " ACTION echo "You said: $ACTION" while [ "$ACTION" = "open the pod bay doors" ]; do echo "I'm sorry Dave, I'm afraid I can't do that." read -p "What would you like me to do? " ACTION echo "You said: $ACTION" done echo "Bye"

select and case Bash Select PS3=prompt # Special* variable for the select prompt select choice in choices; do commands # Break, otherwise endless loop break done Bash Case case EXPRESSION in CASE1) COMMAND-LIST;; CASE2) COMMAND-LIST;;... CASEN) COMMAND-LIST;; esac *see lecture 5 14

15 select/case Exercise Have the user select their favorite kind of music, and output a message based on their choice

select/case Exercise Solution PS3="What is your favorite kind of music? " select CHOICE in "rock" "pop" "dance" "reggae"; do case "$CHOICE" in "rock") echo "Rock on, dude.";; "pop") echo "Top 100 is called that for a reason.";; "dance") echo "Let's lay down the Persian!";; "reggae") echo "Takin' it easy...";; * ) echo "come on...you gotta like something!";; esac break done 16

Arrays name=(element1 element2... elementn) name[index]=value $name ${name[index]} ${name[*]} ${#name[*]} # set an element # get first element # get an element # elements sep.by spaces # array's length arrays don't have a fixed length; they can grow as necessary if you go out of bounds, shell will silently give you an empty string you don't need to use arrays in assignments in this course 17

18 Functions function name() { commands } name # declaration # () s are optional # call functions are called simply by writing their name (no parens) parameters can be passed and accessed as $1, $2, etc. (icky) you don't need to use functions in assignments in this course

19 Remote editing Gnome's file browser and gedit text editor are capable of opening files on a remote server and editing them from your computer press Ctrl-L to type in a network location to open

Remote X display normally, you cannot run graphical programs on a remote server however, if you connect your SSH with the -X parameter, you can! the X-Windows protocol is capable of displaying programs remotely ssh -X attu.cs.washington.edu Other options (-Y for Trusted mode, -C for compressed, see online) 20

Compressed files command zip, unzip tar gzip, gunzip bzip2, bunzip2 description create or extract.zip compressed archives create or extract.tar archives (combine multiple files) GNU free compression programs (single-file) slower, optimized compression program (single-file) many Linux programs are distributed as.tar.gz archives first, multiple files are grouped into a.tar file (not compressed) next, the.tar is compressed via gzip into a.tar.gz or.tgz to decompress a.tar.gz archive: $ tar -xzf filename.tar.gz 21

tar examples $ tar -cvf filename.tar stuff_to_archive -c create an archive -v verbosely list the files processed -f read to/from a file (as opposed to a tape archive) stuff_to_archive - can be filenames or a directory $ tar -xzf filename.tar.gz -x extract from an archive -z filter the archive through gzip (compress/uncompress it) -f read to/from a file (as opposed to a tape archive) 22

Other useful tidbits Single quotes vs double quotes Quotes tell the shell to treat the enclosed characters as a string Variable names are not expanded in single quotes STAR=* Shell History echo $STAR echo $STAR echo $STAR The shell remembers all the commands you ve entered Can access them with the history command Can execute the most recent matching command with! Ex:!less will search backwards until it finds a command that starts with less, and re-execute the entire command line 23

Newlines in Windows/Unix Early printers had two different command characters: Carriage return (\r) move the print head back to the left margin Line feed (\n) move the paper to the next line Both occurred when you wanted a newline As time went on, both (\r\n) and just (\n) were used to signify a newline Windows typically uses the (\r\n) version, while Unix uses (\n) Can cause problems when displaying text files created on one system on another system Most modern text editors recognize both and do the right thing Can convert if needed: dos2unix and unix2dos commands 24