1 CSE 303 Lecture 5 bash continued: users/groups; permissions; intro to scripting read Linux Pocket Guide pp. 166-178 slides created by Marty Stepp

Slides:



Advertisements
Similar presentations
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Advertisements

Find Command Characteristics –Locate files descending from multiple starting points –Employs regular expressions Examples On entire system: >find / -name.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
1 CSE 390a Lecture 6 bash scripting continued; remote X windows; unix tidbits slides created by Marty Stepp, modified by Jessica Miller
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.
1 CSE 390a Lecture 4 Persistent shell settings; users/groups; permissions slides created by Marty Stepp, modified by Jessica Miller
1 CSE 303 Lecture 6 more Unix commands; bash scripting continued read Linux Pocket Guide pp , 82-88, slides created by Marty Stepp
1 CSE 303 Lecture 4 users/groups; permissions; intro to shell scripting read Linux Pocket Guide pp , 25-27, 61-65, , 176 slides created by.
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.
Guide To UNIX Using Linux Third Edition
1 CSE 390a Lecture 5 Intro to shell scripting slides created by Marty Stepp, modified by Jessica Miller
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
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.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Linux Operations and Administration
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.
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
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. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
1 CSE 390a Lecture 6 bash scripting continued; remote X windows; unix tidbits slides created by Marty Stepp, modified by Jessica Miller and Ruth Anderson.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
1 CSE 303 Lecture 3 bash shell continued: processes; multi-user systems; combining commands read Linux Pocket Guide pp , , , 118, 122,
Writing Scripts Hadi Otrok COEN 346.
1 CSE 390a Lecture 6 bash scripting continued; remote X windows; unix tidbits slides created by Marty Stepp, modified by Jessica Miller and Ruth Anderson.
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.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Linux Administration Working with the BASH Shell.
 Prepared by: Eng. Maryam Adel Abdel-Hady
1 CSE 391 Lecture 6 bash scripting continued; remote X windows; unix tidbits slides created by Marty Stepp, modified by Jessica Miller and Ruth Anderson.
1 CSE 391 Lecture 5 Intro to shell scripting slides created by Marty Stepp, modified by Jessica Miller & Ruth Anderson
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
bash scripting continued; remote X windows; unix tidbits
Intro to shell scripting
Intro to shell scripting
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Prepared by: Eng. Maryam Adel Abdel-Hady
CSC 352– Unix Programming, Fall 2012
CSE 390a Lecture 5 Intro to shell scripting
Intro to shell scripting
CSE 303 Concepts and Tools for Software Development
LING 408/508: Computational Techniques for Linguists
CSE 390a Lecture 6 bash scripting continued; remote X windows; unix tidbits slides created by Marty Stepp, modified by Josh Goodwin
bash scripting continued; remote X windows; unix tidbits
Intro to shell scripting
Persistent shell settings; intro to shell scripting
CSE 303 Concepts and Tools for Software Development
Intro to shell scripting
Intro to shell scripting
bash scripting continued; remote X windows; unix tidbits
CSE 303 Concepts and Tools for Software Development
bash scripting continued; remote X windows; unix tidbits
CSC 352– Unix Programming, Fall, 2011
bash scripting continued; remote X windows; unix tidbits
Intro to shell scripting
Intro to shell scripting
bash scripting continued; remote X windows; unix tidbits
bash scripting continued; remote X windows; unix tidbits
Intro to shell scripting
Introduction to Bash Programming, part 3
bash scripting continued; remote X windows; unix tidbits
Intro to shell scripting
Presentation transcript:

1 CSE 303 Lecture 5 bash continued: users/groups; permissions; intro to scripting read Linux Pocket Guide pp slides created by Marty Stepp

2 Lecture summary basic script syntax and running scripts shell variables and types control statements: if/else, loops

3 Shell scripts script: A short program whose purpose is to run other programs.  a series of commands combined into one executable file shell script: A script that is executed by a command-line shell.  bash (like most shells) has syntax for writing script programs  if your script becomes > ~ lines, switch to a real language To write a bash script (in brief):  type one or more commands into a file; save it  type a special header in the file to identify it as a script (next slide)  enable execute permission on the file  run it!

4 Basic script syntax #! interpreter  written as the first line of an executable script; causes a file to be treated as a script to be run by the given interpreter (we will use /bin/bash as our interpreter) Example: A script that removes some files and then lists all files: #!/bin/bash rm output*.txt ls -l

5 Running a shell script by making it executable (most common; recommended): chmod u+x myscript.sh./myscript.sh by launching a new shell: bash myscript.sh by running it within the current shell: source myscript.sh  advantage: any variables defined by the script remain in this shell (seen later)

6 echo Example: A script that prints the time and your home directory. #!/bin/bash echo "This is my amazing script!" echo "Your home dir is: `pwd`" Exercise : Make it so that whenever I log in to attu, it:  clears the screen  displays the date/time: The time is: 04/06 10:40  shows me an ASCII cow welcoming my user name commanddescription echo produces its parameter(s) as output (the println of shell scripting)

7 Script example #!/bin/bash clear echo "Today's date is `date`, this is week `date "+%V"`." echo echo "These users are currently connected:" w | grep -v USER | sort echo echo "This is `uname -s` on a `uname -m` processor." echo echo "This is the uptime information:" uptime echo echo "That's all folks!"

8 Comments # comment text  bash has only single-line comments; there is no /*... */ equivalent Example: #!/bin/bash # Leonard's first script ever # by Leonard Linux echo "This is my amazing script!" echo "The time is: `date`" # This is the part where I print my home directory echo "Home dir is: `pwd`"

9.bash_profile when you log in to bash, it runs the script ~/.bash_profile  you can put common startup commands into this file  useful for setting aliases and other defaults  ("non-login" shells use.bashrc instead of.bash_profile ) Exercise : Make it so that whenever you try to delete or overwrite a file during a move/copy, you will be prompted for confirmation first. Exercise : Make it so that when we create new files, we (the owner) will be the only user that can read or write them.

10 Shell variables name = value(declaration)  must be written EXACTLY as shown; no spaces allowed  often given all-uppercase names by convention  variables have global scope by default AGE=14 NAME="Marty Stepp" $ name(usage) echo "$NAME is $AGE" Marty Stepp is 14

11 Common errors if you misspell a variable's name, a new variable is created NAME=Marty... Name=Daniel # oops; meant to change NAME if you use an undeclared variable, an empty value is used echo "Welcome, $name" # Welcome, when storing a multi-word string, must use quotes NAME=Marty Stepp # $NAME is Marty NAME="Marty Stepp" # $NAME is Marty Stepp

12 Capture command output variable=`command`  captures the output of command into the given variable Example: FILE=`ls -1 *.txt | sort | tail -c 1` echo "Your last text file is: $FILE"

13 Types and integers most variables are stored as strings  operations on variables are done as string operations, not numeric to instead treat a variable as an integer: x=42 y=15 let z="$x + $y" # 57 integer operators: + - * / %  bc command can do more complex expressions if a non-numeric variable is used in numeric context, you'll get 0

14 Bash vs. Java x=3  x vs. $x vs. "$x" vs. '$x' JavaBash String s = "hello";s=hello System.out.println("s");echo s System.out.println(s);echo $s s = s + "s"; // "hellos"s=${s}s String s2 = "25"; String s3 = "42"; String s4 = s2 + s3; // "2542" int n = Integer.parseInt(s2) + Integer.parseInt(s3); // 67 s2=25 s3=42 s4=$s2$s3 let n="$s2 + $s3"

15 Special variables  these are automatically defined for you in every bash session Exercise : Change your attu prompt to look like Ubuntu's: variabledescription $DISPLAY where to display graphical X-windows output $HOSTNAME name of computer you are using $HOME your home directory $PATH list of directories holding commands to execute $PS1 the shell's command prompt string $PWD your current directory $SHELL full path to your shell program $USER your user name

16 set, unset, and export  typing set or export with no parameters lists all variables shell commanddescription set sets the value of a variable (not usually needed; can just use x=3 syntax) unset deletes a variable and its value export sets a variable and makes it visible to any programs launched by this shell readonly sets a variable to be read-only (so that programs launched by this shell cannot change its value)

17 Console I/O  variables read from console are stored as strings Example: #!/bin/bash read -p "What is your name? " name read -p "How old are you? " age printf "%10s is %4s years old" $name $age shell commanddescription read reads value from console and stores it into a variable echo prints output to console printf prints complex formatted output to console

18 if/else if [ test ]; then # basic if commands fi if [ test ]; then # if / else if / else commands1 elif [ test ]; then commands2 else commands3 fi  there MUST be a space between if and [ and between [ and test [ is actually a shell command, not just a character

19 Testing commands if [ $USER = "stepp" ]; then echo "Hello there, beautiful!" fi LOGINS=`w | wc -l` if [ $LOGINS -gt 10 ]; then echo "attu is very busy right now!" fi shell commanddescription =, !=, compares two string variables -n, -z tests whether a string is or is not empty (null) -lt, -le, -eq, -gt, -ge, -ne compares numbers; equivalent to Java's, >=, != -e, -d tests whether a given file or directory exists -r, -w tests whether a file exists and is read/writable

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

21 Command-line arguments if [ "$1" = "-r" ]; then echo "Running in special reverse format." fi if [ $# -lt 2 ]; then echo "Usage: $0 source destination" exit 1 # exit the script, error code 1 fi variabledescription $0 name of this script $1, $2, $3,... command-line arguments $# number of arguments array of all arguments

22 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: $./bmi Usage:./bmi weight height $./bmi Your Body Mass Index (BMI) is 15 Here is a sandwich; please eat. $./bmi Your Body Mass Index (BMI) is 32 There is more of you to love. BMIWeight class  18 underweight normal overweight  30 obese

23 Exercise solution #!/bin/bash # Body Mass Index (BMI) calculator if [ $# -lt 2 ]; then echo "Usage: $0 weight height" exit 1 fi let BMI="703 * $1 / $2 / $2" 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