LING 408/508: Computational Techniques for Linguists

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.
Introducing the Command Line CMSC 121 Introduction to UNIX Much of the material in these slides was taken from Dan Hood’s CMSC 121 Lecture Notes.
Linux+ Guide to Linux Certification, Second Edition
Using Linux Commands 2 Lab#5
Shell Script Examples.
Linux Commands LINUX COMMANDS.
Using Linux Commands 2 Lab#5. Sort command Sort the lines of text files. $ sort fileName by default it will sort in normal order(alphabetical 0-9 A-Z.
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
Using Macs and Unix Nancy Griffeth January 6, 2014 Funding for this workshop was provided by the program “Computational Modeling and Analysis of Complex.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
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.
Linux+ Guide to Linux Certification, Second Edition
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
UNIX/LINUX Shells Shell is an UNIX/LINUX command interpreter. Shell command can be internal or external. The code to execute an internal command is part.
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.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
1 CSE 303 Lecture 5 bash continued: users/groups; permissions; intro to scripting read Linux Pocket Guide pp slides created by Marty Stepp
INFO 320 Server Technology I Week 5 Shell environments and scripting 1INFO 320 week 5.
Linux+ Guide to Linux Certification, Third Edition
Linux+ Guide to Linux Certification, Third Edition
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
LING 408/508: Programming for Linguists Lecture 8 September 23 rd.
Manipulating Files Refresher. The touch Command touch is used to create a new, empty file. If the file already exists, touch updates the time and date.
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?
Linux Lecture #02. File Related Commands cat --Concatenate and print (display) the content of files. --Also used to create a new file. Syntax cat [Options]
LING 408/508: Programming for Linguists Lecture 5 September 9 th.
LING 408/508: Programming for Linguists Online Lecture 6 September 14 th.
PTA Linux Series Copyright Professional Training Academy, CSIS, University of Limerick, 2006 © Workshop III - Part B Shell Commands Professional Training.
Sed. Class Issues vSphere Issues – root only until lab 3.
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
LING 408/508: Programming for Linguists Online Lecture 7 September 16 th.
File Management commands cat Cat command cat cal.txt cat command displays the contents of a file here cal.txt on screen (or standard out).
Linux Administration Working with the BASH Shell.
Agenda Customizing a Unix/Linux account Environment Introduction to Start-up Files (.bash_profile,.bashrc,.profile,.kshrc) Safe Methods for Changing Start-up.
Laboratory 1.2a1 First steps with Unix - Lab Boris Steipe Department of Biochemistry Department.
Chapter 11 Command-Line Master Class
Commands Basic syntax of shell commands UNIX or shell commands have a basic structure command -options target command comes first (such as cd or ls) any.
Shell Features CSCI N321 – System and Network Administration
Prepared by: Eng. Maryam Adel Abdel-Hady
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
CAP652 Lecture - Shell Programming
Some Linux Commands.
The Command Prompt Commands are the way to “do things” in Unix
Aliases, functions, wrappers, pipelines
CSE 303 Concepts and Tools for Software Development
Introduction to UNIX.
Using Linux Commands Lab 3.
What is Bash Shell Scripting?
CHAPTER 8 (skip , ) CHAPTER 10
LING 408/508: Computational Techniques for Linguists
LING 408/508: Computational Techniques for Linguists
LING 408/508: Computational Techniques for Linguists
LING 408/508: Computational Techniques for Linguists
Persistent shell settings; intro to shell scripting
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
Shell Control Structures
Module 6 Working with Files and Directories
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Lab 2: Terminal Basics.
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
LPI Linux Certification
Presentation transcript:

LING 408/508: Computational Techniques for Linguists Lecture 8

Administrivia I'm supposed to be away next Tuesday (on a trip) Nope, trip canceled because of hurricane warnings Class as usual next Tuesday

Today's Topics Let's practice learning how to write shell scripts …

Exercise 1: print arguments to a script Write a bash shell script that simple accepts command line arguments and prints out the number of arguments, and each argument numbered on a separate line Example: bash args.sh a b c Args: 3 #1: a #2: b #3: c What you need to know to solve this: $# $1 shift store a variable increment a value by 1

Exercise 1: print arguments to a script Many different ways to write the solution…

Exercise 2: deleting files

Exercise 2: deleting files Remove File/Directory rm removes a file (dangerous!) rm –d removes a directory (empty) rm –r recursive remove (extreme danger!) rm –rf forced recursive remove (!!!) Examples: touch file.txt rm file.txt (you have default write permission) touch protected.txt chmod u-w protected.txt (u = user, -w = remove write permission) rm protected.txt override r--r--r-- sandiway/staff for protected.txt? rm –f protected.txt (no interaction: forced removal) rm –i file.txt (ask it to ask you for confirmation) remove file.txt?

Exercise 2: deleting files best used in interactive shell can put alias shortcut in Terminal startup ~/.bash_profile (MacOS) or ~/.bashrc alias rm="rm -i" not recursively expanded (considered dangerous: why?) alias (list defined aliases) unalias rm (remove alias) Aliases don't work in shell scripts: #!/bin/bash if [ $# -ne 1 ]; then echo "usage: filename" exit 1 fi touch $1 rm $1 At least two reasons: another computer shell scripts define a function in ~/.bash_profile (absolute path: otherwise recursive) rm () { /bin/rm -i "$@" } export –f rm rm –i won't be called!

Other commands with -i -i (interactive confirm option) before overwriting a file mv -i rename file cp -i copy file

Exercise 3: double-spacing a text file Write a script that reads each line of a file, then writes the line back out, but with an extra blank line following. This has the effect of double-spacing the file. What you need to know to solve this: read test [[ … ]] while loop

Exercise 3: double-spacing a text file

Exercise 3: double-spacing a text file

Exercise 3: double-spacing a text file

Exercise 3: double-spacing a text file test [[ .. ]]

Exercise 3: double-spacing a text file test [[ .. ]]

Exercise 3: double-spacing a text file test [[ .. ]]

Exercise 3: double-spacing a text file double-spacing the file: also works -n = non-zero read –r If this option is given, backslash does not act as an escape character.

Exercise 3: double-spacing a text file double-spacing the file: whitespace trim problem workaround:

Exercise 4: all except blank lines Changing the line spacing of a text file: write a script to echo all lines of a file except for blank lines.

Useful tidbits Pre-programmed interaction: (here document: inline file) confirm.sh rm () { /bin/rm -i "$@" } export -f rm #!/bin/bash if [ $# -ne 1 ]; then echo "usage: filename" exit 1 fi touch $1 rm $1

Useful tidbits bash confirm.sh <<EOF y EOF

Useful tidbits bash confirm.sh <<<y