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.

Slides:



Advertisements
Similar presentations
Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
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.
1 CSE 390a Lecture 6 bash scripting continued; remote X windows; unix tidbits slides created by Marty Stepp, modified by Jessica Miller
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
1 CSE 303 Lecture 6 more Unix commands; bash scripting continued read Linux Pocket Guide pp , 82-88, slides created by Marty Stepp
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Lab 8 Shell Script Reference:
Shell Control Structures CSE 2031 Fall August 2015.
CSE 390a Editing and Moving Files
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.
Linux Operations and Administration
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
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.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
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 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.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
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 Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
1 CSE 390a Lecture 3 bash shell continued: processes; multi-user systems; remote login; editors slides created by Marty Stepp, modified by Jessica Miller.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Linux Administration Working with the BASH Shell.
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.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
bash scripting continued; remote X windows; unix tidbits
bash scripting continued; remote X windows; unix tidbits
Shell Control Structures
Department of Computer Engineering
slides created by Marty Stepp, modified by Josh Goodwin
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
bash scripting continued; remote X windows; unix tidbits
bash scripting continued; remote X windows; unix tidbits
Agenda Control Flow Statements Purpose test statement
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
CSE 390a Lecture 3 bash shell continued: processes; multi-user systems; remote login; editors slides created by Marty Stepp, modified by Jessica Miller.
slides created by Marty Stepp, modified by Jessica Miller
bash scripting continued; remote X windows; unix tidbits
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
bash scripting continued; remote X windows; unix tidbits
bash scripting continued; remote X windows; unix tidbits
CSE 390a Lecture 3 bash shell continued: processes; multi-user systems; remote login; editors slides created by Marty Stepp, modified by Jessica Miller.
bash scripting continued; remote X windows; unix tidbits
Presented by, Mr. Satish Pise
bash scripting continued; remote X windows; unix tidbits
CSE 390a Lecture 3 bash shell continued: processes; multi-user systems; remote login; editors slides created by Marty Stepp, modified by Jessica Miller.
bash scripting continued; remote X windows; unix tidbits
CSE 390a Lecture 3 bash shell continued: processes; multi-user systems; remote login; editors slides created by Marty Stepp, modified by Jessica Miller.
CSE 390a Lecture 3 bash shell continued: processes; multi-user systems; remote login; editors slides created by Marty Stepp, modified by Jessica Miller.
bash scripting continued; remote X windows; unix tidbits
bash scripting continued; remote X windows; unix tidbits
bash scripting continued; remote X windows; unix tidbits
bash scripting continued; remote X windows; unix tidbits
Introduction to Bash Programming, part 3
bash scripting continued; remote X windows; unix tidbits
Presentation transcript:

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

2 Lecture summary more shell scripting  if/else  while/until  select/case  advanced: arrays and functions Remote editing/GUI various new Unix/Linux commands  file archiving and compression  shell history  newlines in Unix vs Windows

3 if/else if [ test ]; then # basic if commands fi if [ test ]; then # if / else if / else commands1 elif [ test ]; then commands2 else commands3 fi The [ ] syntax is actually shorthand for a shell command called “ test ” (Try: “man test”) there MUST be spaces as shown: if space [ space test space ] include the semi-colon after ] (or put “ then” on the next line)

4 test operators if [ $USER = “husky12" ]; then echo ‘Woof! Go Huskies!’ fi LOGINS=`w -h | wc -l` if [ $LOGINS -gt 10 ]; then echo ‘attu is very busy right now!’ fi comparison operatordescription =, !=, compares two string variables -z, -n tests if a string is empty (zero-length) or not empty (nonzero-length) -lt, -le, -eq, -gt, -ge, -ne compares numbers; equivalent to Java's, >=, != -e, -f, -d tests whether a given file or directory exists -r, -w tests whether a file exists and is read/writable * Note: man test will show other operators.

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

6 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

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

8 Common errors [: -eq: unary operator expected  you used an undefined variable in an if test [: too many arguments  you tried to use a variable with a large, complex value (such as multi- line output from a program) as though it were a simple int or string let: syntax error: operand expected (error token is " ")  you used an undefined variable in a let mathematical expression

9 while and until loops while [ test ]; do # go while test is true commands done until [ test ]; do # go while test is false commands done while [ “$ACTION” = “open the pod bay doors” ]; do echo “I’m sorry Dave, I’m afraid I can’t do that.” read –p “What would you like me to do?” ACTION done

10 select and case Bash Select PS3=prompt # Special* variable for the select prompt select choice in choices; do commands # Break, otherwise endless loop break done Bash Case case EXPRESSION in CASE1) COMMAND-LIST;; CASE2) COMMAND-LIST;;... CASEN) COMMAND-LIST;; esac *see lecture 5

11 Exercise Have the user select their favorite kind of music, and output a message based on their choice

12 Exercise Solution PS3="What is your favorite kind of music? " select CHOICE in "rock" "pop" "dance" "reggae"; do case "$CHOICE" in "rock") echo "Rock on, dude.";; "pop") echo "Top 100 is called that for a reason.";; "dance") echo "Let's lay down the Persian!";; "reggae") echo "Takin' it easy...";; * ) echo "come on...yougotta like something!";; esac break done

13 Arrays name=(element1 element2... elementN) name[index]=value# set an element $name# get first element ${name[index]}# get an element ${name[*]}# elements sep.by spaces ${#name[*]}# array's length  arrays don't have a fixed length; they can grow as necessary  if you go out of bounds, shell will silently give you an empty string you don't need to use arrays in assignments in this course

14 Functions function name() {# declaration commands# ()’s are optional } name# call  functions are called simply by writing their name (no parens)  parameters can be passed and accessed as $1, $2, etc. (icky) you don't need to use functions in assignments in this course

15 Remote editing Gnome's file browser and gedit text editor are capable of opening files on a remote server and editing them from your computer  press Ctrl-L to type in a network location to open

16 Remote X display normally, you cannot run graphical programs on a remote server however, if you connect your SSH with the -X parameter, you can!  the X-Windows protocol is capable of displaying programs remotely ssh -X attu.cs.washington.edu Other options (-Y for “Trusted” mode, -C for compressed, see online)

17 Compressed files many Linux programs are distributed as.tar.gz archivesmany Linux programs  first, multiple files are grouped into a.tar file (not compressed)  next, the.tar is compressed via gzip into a.tar.gz or.tgz to decompress a.tar.gz archive: $ tar -xzf filename.tar.gz commanddescription zip, unzip create or extract.zip compressed archives tar create or extract.tar archives (combine multiple files) gzip, gunzip GNU free compression programs (single-file) bzip2, bunzip2 slower, optimized compression program (single-file)

18 tar examples $ tar -cvf filename.tar stuff_to_archive -c create an archive -v verbosely list the files processed -f read to/from a file (as opposed to a tape archive) stuff_to_archive - can be filenames or a directory $ tar -xzf filename.tar.gz -x extract from an archive -z filter the archive through gzip (compress/uncompress it) -f read to/from a file (as opposed to a tape archive)

19 Other useful tidbits Single quotes vs double quotes  Quotes tell the shell to treat the enclosed characters as a string  Variable names are not expanded in single quotes STAR=*  echo $STAR  echo “$STAR”  echo ‘$STAR’ Shell History  The shell remembers all the commands you’ve entered  Can access them with the history command  Can execute the most recent matching command with ! Ex: !less will search backwards until it finds a command that starts with less, and re-execute the entire command line

20 Newlines in Windows/Unix Early printers had two different command characters:  Carriage return (\r) – move the print head back to the left margin  Line feed (\n) – move the paper to the next line  Both occurred when you wanted a “newline” As time went on, both (\r\n) and just (\n) were used to signify a “newline” Windows typically uses the (\r\n) version, while Unix uses (\n)  Can cause problems when displaying text files created on one system on another system  Most modern text editors recognize both and do the right thing  Can convert if needed: dos2unix and unix2dos commands