Various 2. readonly readonly x=4 x=44 #this will give an error (like what in java?)

Slides:



Advertisements
Similar presentations
CST8177 bash Scripting Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
Advertisements

CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
Chapter 6 Dealing with quotes. Dealing with Quotes and slashes grep Susan phonebook grep Susan Goldberg phonebook  results in an error  grep ‘Susan.
CS 497C – Introduction to UNIX Lecture 22: - The Shell Chin-Chih Chang
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
Compunet Corporation Introduction to Unix (CA263) More on Parameters By Tariq Ibn Aziz Dammam Community College.
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.
Introduction to Linux and Shell Scripting Jacob Chan.
Shell Script Examples.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
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.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
1 Shell Programming – Extra Slides. 2 Counting the number of lines in a file #!/bin/sh #countLines1 filename=$1#Should check if arguments are given count=0.
An Introduction to Unix Shell Scripting
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.
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
The if construct The general format of the if command is: Every command has exit status If the exit status is zero, then the commands that follow between.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
CST8177 bash Scripting Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
OPERATING SYSTEMS DESIGN UNIX BASICS & SHELL SCRIPTING.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
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 Scripting AFNOG IX Rabat, Morocco May 2008.
Shell Programming Any command or a sequence of UNIX commands stored in a text file is called a shell program. It is common to call this file a command.
CS465 - UNIX The Bourne Shell.
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)
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Lecture 2: Advanced UNIX, shell scripts (ol)‏ Programming Tools And Environments.
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.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
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.
Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
Sed. Class Issues vSphere Issues – root only until lab 3.
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
Various 3. Noclobber 1 echo hello > output.txt But what if output is an inportant file and you do not want to make a mistake!!! set –o noclobber Now you.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Batch Files More flow of control Copyright © by Curt Hill.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Chapter 16 Advanced Bourne Shell Programming. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Objectives To discuss numeric data processing.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
CSC 352– Unix Programming, Fall 2012
Part 1: Basic Commands/Utilities
Shell Scripting March 1st, 2004 Class Meeting 7.
Agenda Control Flow Statements Purpose test statement
What is Bash Shell Scripting?
Pepper (Help from Dr. Robert Siegfried)
John Carelli, Instructor Kutztown University
Shell Control Structures
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Presentation transcript:

Various 2

readonly readonly x=4 x=44 #this will give an error (like what in java?)

talking to myself echo echo talking to myself > newScript.sh sh newScript.sh rm newScript.sh #what are the potential problems with this? #how can it be improved?

Command substitution the shell can insert standard output of a command at any point in the command line. There are two ways to do this `…` backquotes $(…)

Example - Command substitution echo date is `date` Notice the backquotes! echo date is $(date) d=$(date) echo date is $d ~]$ d=`date` ~]$ echo $d Tue Apr 7 15:52:35 CST 2009

$(…) or `…` $() is better that `` easier to read! (think of if in java) can nest easier $($(...)) note can nest with backquotes too

quotes '...' removes meaning of all enclosed characters "..." removes meaning of all enclosed characters except $, ` and \ \c removes meaning of special character c ($, ', " newline, and \) `command` or $(command) executes the command (\ is also used for continuing new lines)

A unix philosophy If you have to do it more than twice – write a script to do it. This will take time at the beginning – but you will become faster. The overhead is worth it. If you have to do it more than twice – so has someone else – so you can google for the code

What is the error here? for i in do echo i done

No Error!! It may be what the person wanted. You need to explain what you require. You can write what you want the expected output to be. Or explain.

\ for continuing new lines $ ls | wc Or if it is a long command I can continue it on another line ls \ ? | wc # a better example would be with awk or grep.

PS2 is the prompt here $ PS2=p2 $ echo $PS2 p2 $ ls \ p2 | wc $ Note – this is useful for long chains of commands.

Debugging (tracing) sh -x prog.sh or can do as set -x or can switch on and off in the script turn off with set +x (note that this is counter intuitive!!!) set with no arguments shows the variables. Printing is a good idea to confirm the values of variables, and isolate problems.

Setting a variable to null var= var="" var='‘ #two single quotes. var=`` #two back quotes are these all the same? If not – what are the error messages. Try it out for yourself.

Single and Double Quotes Single quote protect everything echo '$(ls)’ #will print...what? Double quotes filelist=$(ls) echo $filelist echo "$filelist" good for storing contents of a file namelist=$(cat names.txt) (we can do text processing on this)

expr expr expr 1 * 2 (this will give an error – what error?) expr 1 \* 2

${parameter} vs $parameter mv $file $fileBackUp (we want to append the filename) mv ${file} ${file}x Also for arguments to a script. This can also be used for ${1} cannot say $11 need ${11}

Null command : if true then : #we have to put something here else echo false fi #this is good when writing programs.

(…) and {…;} (..) this works in a subshell {…;} works in the current shell x=10 (x=40) echo $x # what will this print? {x=70;} echo $x #what will this print? #see subshell.sh on shared drive.

(…) example (command1; command 2 ) & This will do command1, then command2, but the parent shell can continue working. This is not the same as (command1 &; command2 & ) & command1; command 2 & Why not? What do these do?

{…} example {command1; command2; } 2> errors.txt Here the error output of the two commands is sent to the file errors.txt Is this the same as command1 2> errors.txt; command2 2> errors.txt { sh thisFileDoesNotExist.sh ; sh OrThisOne.sh ;} 2> errors.txt #see share groupingCommands.sh

break for i in do echo $i [ $i -eq 2 ] && break Done #break.sh on share

Break n #this will break out of the loop for i in do echo $i [ $i -eq 2 ] && break $n done This will break out of the n innermost loops #breakN.sh on share

continue #this will continue with the the loop for i in do echo before $i [ $i -eq 2 ] && echo skip && continue echo after $i done

Continue n Just like break has a break n Continue has a continue n See continue.sh and continueN.sh on share.

Loop redirection You can perform redirection on a loop for ((i = 1; i < 5; i++)) do echo $i done > out.txt #on share at loopRedirection.sh

Loop in background #this will execute a loop in the background. for ((i = 1; i < 50000; i++)) do echo $i >> out.txt done &

loopPiping #the output of the loop is piped into wc for ((i = 1; i < 5; i++)) do echo $i done | wc #see loopPiping.sh on share.

Forcing output 1 echo hello This prints to the screen (tellytype) by default OR whereever you direct it echo hello > out.txt

Forcing output 2 #If we want to force the output to the screen in a script #try to run this program as #sh devtty.sh > out.txt echo this will always go to the screen > /dev/tty

Be careful with rm (rm -i) $ ls a.txt b.txt c.txt remove.sh remove.sh~ $ rm a.txt rm: remove regular empty file `a.txt'? y $ sh remove.sh $ ls remove.sh remove.sh~