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

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Using Linux Commands 2 Lab#5
Introduction to Linux and Shell Scripting Jacob Chan.
Shell Programming. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Shell Script Examples.
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.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
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.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
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 8 Shell.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
Linux+ Guide to Linux Certification, Third Edition
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
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.
CS465 - UNIX The Bourne Shell.
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Shell Programming. An example u tr abcdefghijklmnopqrstuvwxyz \ thequickbrownfxjmpsvalzydg file2 –encrypts file1 into file2 u record this command with.
1 CSE 303 Lecture 5 bash continued: users/groups; permissions; intro to scripting read Linux Pocket Guide pp slides created by Marty Stepp
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
INFO 320 Server Technology I Week 5 Shell environments and scripting 1INFO 320 week 5.
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
CSC 4630 Meeting 5 January 31, Next Time Enhance the steps that you used to clean the Moby Dick chapter to create a shell script that takes any.
Writing Scripts Hadi Otrok COEN 346.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Shell Programming.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
CS252: Systems Programming Ninghui Li Slides by Prof. Gustavo Rodriguez-Rivera Topic 7: Unix Tools and Shell Scripts.
CISC3130 Spring 2013 Fordham Univ. 1 Bash Scripting: control structures.
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Shell Control Statements and More
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1.
Sed. Class Issues vSphere Issues – root only until lab 3.
Linux+ Guide to Linux Certification, Second Edition
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Linux Administration Working with the BASH Shell.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
ULI101 Week 10. Lesson Overview ● Shell Start-up and Configuration Files ● Shell History ● Alias Statement ● Shell Variables ● Introduction to Shell Scripting.
Prepared by: Eng. Maryam Adel Abdel-Hady
Andy Wang Object Oriented Programming in C++ COP 3330
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
CSE 390a Lecture 5 Intro to shell scripting
Some Linux Commands.
Shell Environments.
Writing Shell Scripts ─ part 3
Basic UNIX OLC Training.
UNIX Reference Sheets CSE 2031 Fall 2010.
Linux Shell Script Programming
Intro to shell scripting
CSE 303 Concepts and Tools for Software Development
CSC 352– Unix Programming, Fall, 2011
Introduction to Bash Programming, part 3
Presentation transcript:

find Command Characteristics –Locate files descending from multiple starting points –Employs regular expressions Examples On entire system: >find / -name "cs*" Two starting points: >find /usr /var/html –name "cs*" Case insensitive: >find /usr –iname "*htm*" More than 3 days ago: >find /usr -mtime +3 –name "*htm*" Within the last 3 days: >find /usr –mtime -3 –name "*htm*"

Customizing your prompt To set: set $PS1 variable with a prompt string. Additional prompt string capabilities: \d = todays date \H = host name \T = current time \w = working directory \! = history number of this command Example: export PS1=">" Example: export PS1="\! \w >"

Disk Usage Commands Free Blocks: >df Disk usage –Size of each directory (recursive): >du –Summary of each directory (not recursive): >du –s * –Size of a single directory: >du –s –Sort usage, numeric reverse, page at a time >du | sort –nr | more

Aliases Definition: Replace a string by a word at the start of a command Use: Shorten what needs to be typed by aliasing a command with its arguments Use: Rename a command to something more familiar Examples 1.Display all of the aliases in use: >alias 2.Example (dos user directory list): >alias dir='ls –l' 3.To remove an alias: >unalias dir Replace a word by a string when starting a command

Creating Script Variables create a variable: ="value" –Note: do not use spaces –Example: >TEAM="Yanks" >echo $TEAM –Variables are case sensitive –You can use variables anywhere in commnads >bindir=~bin >echo $bindir To make variables accessible to all sub shells, use export: >export TEAM="yanks"

Command Substitution Setting variables: ` ` or $( ) Examples 1.>dir=`pwd` >echo $dir 2.files=$(ls) echo $files 3.echo there are `grep –ci bush grades` bush\'s From command line: ` ` echo `date` grep "abc" `cat file.txt` The output of one command becomes part of another Note: The back quote is found on the top left of most keyboards Note: echo “$(ls)” preserves the new lines in the output

$PATH Environment Variable Most unix commands are C programs When you execute a command –$PATH contains a series of path names –The shell searches all variables in $PATH –To add program/bin to the $PATH variable export PATH = $PATH:program/bin –To add the current directory to $PATH export PATH = $PATH:.

Shell Scripts Put multiple commands in a file and then execute them by typing the file name –They execute the statements one by one Creating a script –The first line should be #!/bin/bash –Other lines starting with # are comments –Make executable: u+x scriptName –Execute:./scriptName [unless working directory is in the search path] Use script capabilities to –create loops –create decisions –write functions See Bash's Beginners guide See examples on the class web site

Arguments to a Shell Script Arguments to a script –$0 is the name of the shell script (argument 0) –$1 is the first argument –$2 is the second argument –$k is the k th argument –$# is the number of arguments is all of the arguments Example: scriptFile x y z –$0 = scriptFile –$2 = y –$# is 3 is x y z

Shell Script Decisions if [ condition ]; then fi if [ condition ]; then else fi if [ $1 == 3 ] ; then echo "arg equals 3" else echo "arg not 3"; fi Note: space after [ and before ], semi colon after ] Comparators: ! -lt –le –gt –ge –f == -r where -f is file –r for readable file

while and do Loops while [ condition ]; do command1; command2; command3 done i=0 while [ $i –lt 4 ]; do echo "Welcome $i"; i=$[$i+1]; done or i=0 do until [ $i == 4 ]; do echo "Welcome $i"; i=$[$i+1]; done or i=0 while [ $i –lt 4 ]; do echo "Welcome $i" i=$[$i+1] done Semicolons separate parts of the loop on the same line Boolean variables true or false are legal

for Loop for in ; do ; done; Example: for i in `cat list`; do cp "$i" "$i.bak;" done Example: for i in $PATHNAME/*; do rm $PATHNAME; done Example: >for x in *; do echo $x; done Note: the last example is directly from the command line

Calculate the average in a script #!/bin/bash if [ $# -lt 1 ]; then echo "usage: at least one argument needed" exit 1 fi value=0 count=0 for i in do value=$[$value+$i] count=$[$count+1] done average=$((value/count)) echo $average average 1 outputs 1 average outputs 2

Script Functions () { } Example: doSomething doSomething() { ls –l | more; } doSomething Note: Function must be declared before it can be used. Note: Spaces required before and after braces Note: A common error is forgetting the required spaces

Functions with arguments Arguments means all arguments 2.$* means all arguments, but treats quoted argments as one with embedded spaces 3.$0 is the command name 4.$1 first argument 5.$2 second argument 6.$# number of arguments Example: LL() { ls –lR | more; } LL / Example: mycd() { cd pwd; }

Shell Startups.bash_profile –configures the login environment –Most.bash_profiles also execute.bashrc.bashrc: configures subshell environments To customize a users environment, normally edit.bashrc include –Include aliases, variables, functions, search path ($PATH) –Good idea: Make the script in a test file first –Takes effect when logging in or executiong the source command.bash_profile and.bashrc in a user’s home directory