Log into Linux Reminders: Homework 1 due today; Homework 2 due on Thursday Questions?

Similar documents
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?

Chapter 9. Shell and Kernel

Topic 2: More Shell Skills

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

Topic 2: More Shell Skills. Sub-Topic 1: Quoting. Sub-Topic 2: Shell Variables. Difference Between Single & Double Quotes

UNIX Shell Programming

More Raspian. An editor Configuration files Shell scripts Shell variables System admin

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

CS Unix Tools. Lecture 3 Making Bash Work For You Fall Hussam Abu-Libdeh based on slides by David Slater. September 13, 2010

COMP 4/6262: Programming UNIX

Environment Variables

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

Unix as a Platform Exercises. Course Code: OS-01-UNXPLAT

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

Last Time. on the website

Useful Unix Commands Cheat Sheet

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

Bourne Shell Programming Topics Covered

The UNIX Shells. Computer Center, CS, NCTU. How shell works. Unix shells. Fetch command Analyze Execute

LING 408/508: Programming for Linguists. Lecture 8 September 23 rd

Bourne Shell Reference

Unix Shell Environments. February 23rd, 2004 Class Meeting 6

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

bash, part 3 Chris GauthierDickey

UNIX System Programming Lecture 3: BASH Programming

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

Using bash. Administrative Shell Scripting COMP2101 Fall 2017

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 2

Topic 2: More Shell Skills

Environment Variables

Practical 02. Bash & shell scripting

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Introduction to the Shell

Introduction to Bash Programming. Dr. Xiaolan Zhang Spring 2013 Dept. of Computer & Information Sciences Fordham University

Linux shell programming for Raspberry Pi Users - 2

Review of Fundamentals

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

UNIX COMMANDS AND SHELLS. UNIX Programming 2015 Fall by Euiseong Seo

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

Assignment clarifications

Here redirection. Case statement. Advanced Unix Tools Lecture 6 CS214 Spring 2004 Friday March 5, 2004

Unix Scripts and Job Scheduling. Overview. Running a Shell Script

85321, Systems Administration Chapter 6: The shell

Vi & Shell Scripting

Lab 2: Linux/Unix shell

bash startup files Linux/Unix files stty Todd Kelley CST8207 Todd Kelley 1

More Scripting and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Shells and Shell Programming

System Programming. Unix Shells

Scripting. COSC 301 Lecture 4 Zhiyi Huang Computer Science University of Otago

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

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

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

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

The Command Line. Matthew Bender. Friday 18 th September, CMSC Command Line Workshop

Shells and Shell Programming

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

Fall 2006 Shell programming, part 3. touch

Computer Systems and Architecture

Subcontractors. bc math help for the shell. interactive or programatic can accept its commands from stdin can accept an entire bc program s worth

Shell Start-up and Configuration Files

UNIX files searching, and other interrogation techniques

LINUX Scripts de Bash Shell. Exercise 1 - Working with Files

Linux Bash Shell Scripting

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

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

CS197U: A Hands on Introduction to Unix

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

CENG 334 Computer Networks. Laboratory I Linux Tutorial

CS246-Assign01 V1.1: Winter 2013

CSC UNIX System, Spring 2015

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

Understanding bash. Prof. Chris GauthierDickey COMP 2400, Fall 2008

Environment Variables

${Unix_Tools} exercises and solution notes

Standard. Shells. tcsh. A shell script is a file that contains shell commands that perform a useful function. It is also known as shell program.

Linux Shell Script. J. K. Mandal

22-Sep CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control. Faculty of Computer Science, Dalhousie University

Lecture # 2 Introduction to UNIX (Part 2)

Unix Introduction to UNIX

More Scripting Techniques Scripting Process Example Script

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

A shell can be used in one of two ways:

CST Algonquin College 2

Chapter 4. Unix Tutorial. Unix Shell

Bash command shell language interpreter

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

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

Introduction to Unix Week 3

CS Unix Tools & Scripting

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

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

More Linux Shell Scripts

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

3/8/2017. Unix/Linux Introduction. In this part, we introduce. What does an OS do? Examples

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

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

Computer Systems and Architecture

Transcription:

Lecture 4 Log into Linux Reminders: Homework 1 due today; Homework 2 due on Thursday Questions? Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 1

Outline Exercise from last lecture More BASH programming Command Substitution Arithmetic Substitution Additional Useful Commands Grouping Exercises Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 2

Command Substitution Command substitution allows the standard output of a command to replace the command name. There are two forms: `COMMAND` or $ (COMMAND). rm `cat filelist` # rm files in a list txtfiles=$(ls *.txt) # capture ls display userpass=$(grep i '^hwang:' /etc/passwd) passwd=$(cat /etc/passwd) # capture file Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 3

Arithmetic Substitution Arithmetic evaluation uses either let expr or ((expr)). Substitution uses $((expr)). b=12 let a=$b*3 # a is 36 ((a = $b * 3)) # alternative to let val=$(($b**2)) # val is 144 count=0 while [ $count le 10 ] do # do stuff here count=$(($count + 1)) # or ((count++)) done Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 4

HERE Documents HERE documents are a special form of input redirection that reads input from the script. cat <<EOTEXT Hi there! How are you? EOTEXT # Display multiline message # Example with variable substitution sftp $user@csserver <<SFTPINPUT cd $remotedir get $file bye SFTPINPUT Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 5

Functions Functions only have local scope by default, but can be exported. error() { echo $* > /dev/stderr ; } Parameters are accessed using $*, $1, $2, etc. func() { if [ "$1" ]; then echo "Parameter 1 is \"$1\" fi } Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 6

Aliases You can create aliases for commands too: alias ls='ls color=tty' # alias for ls alias ll='ls l color=tty' alias rm='rm i' # force rm to prompt alias # display aliases \ls # run unaliased ls Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 7

BASH Startup Bash shells can be interactive or non-interactive a login or non-login shell. A login shell reads and executes commands from /etc/profile and then one of (in order) either ~/.bash_profile, ~/.bash_login, or ~/.profile. A non-login interactive shell reads commands only from ~/.bashrc. Set exported environment variables in ~/.bash_profile. Define functions and aliases in ~/.bashrc. Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 8

Useful Commands xargs takes filenames from standard input and feeds the names to a command. ls xargs p l gzip expr is an all-purpose expression evaluator a=$(expr 5 + 3) # arithmetic b=$(expr length $filename) # string length bc is an arbitrary precision calculator. # get pi to 40 places pi=$(echo 'scale=40; 4*a(1)' bc l) Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 9

Useful Commands export makes a shell variable available in subshells. export myhome=/home/hwang read will read a line of input from standard input. read line read # user input is $line # read line and throw away printf can give fancier output than echo. printf %8.4f\t\t%s\n $val $myhome Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 10

Useful Commands exec replaces the current process with a new one. It is often used at the end of a script. You can also use it from within a script to redirect subsequent standard output exec vi # "become" vi exec > output.txt echo hello there # sent to output.txt Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 11

Useful Commands eval evaluates its argument(s) as if they were typed on the command line, then the result is evaluated. set one two three # sets $1 $2 $3 n=1 # use value of n to determine which var eval echo \${$n} # evals to echo ${1} Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 12

Grouping Use the following code to read lines of input from a file: while read line do # Do something with $line done < inputfile > outputfile Input (and output) is redirected for all commands inside the body of the while-loop. Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 13

Grouping You can also redirect input and/or output by using braces to group commands: { read x; read y; } < input Use space between the command group and the braces. The final semicolon is required. Alternatively use this form: { read x read y } < input Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 14

Grouping You can use ( ) for grouping too. With ( ), the command group is executed in a subshell (another bash process), so if you set or change any variables those changes will not be seen in the parent process. myvar=abc { myvar=xyz; } echo $myvar (myvar=mno) echo $myvar # Will display xyz # Will display xyz Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 15

In-class Exercises Write a shell script named sortusers1 that displays every other username (from /etc/passwd) in alphabetical order. Display the username only. Write sortusers2 that displays every other username sorted by user id. Display the username only. Write a shell script named revargs that displays its arguments in reverse order (eval may be useful here). Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 16