Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

Slides:



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

NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
Chapter Seven Unix Shell Environments1 System Programming UNIX Shell Environments.
CS 497C – Introduction to UNIX Lecture 26: - The Process 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.
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
CS 497C – Introduction to UNIX Lecture 36: - Customizing the Environment Chin-Chih Chang
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
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. 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.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Welcome to CSE  Name: Di Cao   Classroom: DL357  Class Time: T 8:30am - 9:18am  Office.
Introduction to Shell Script Programming
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.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
Chapter 6: Shell Programming
An Introduction to Unix Shell Scripting
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.
Shell Features CSCI N321 – System and Network Administration Copyright © 2000, 2005 by Scott Orr and the Trustees of Indiana University.
CS 2061 Shells Also known as: Unix Command Interpreter.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
CS 6560 Operating System Design Lecture 3:Tour of GNU/Linux.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Shell Scripting AFNOG IX Rabat, Morocco May 2008.
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.
UNIX/LINUX SHELLS.  “A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Unix Shell Environments February 23rd, 2004 Class Meeting 6.
UNIX shell environments CS 2204 Class meeting 6 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
Customizing the Shell Environment. UNIX Shells Two characteristics of shells –Interactive: prompts ($) and waits for your response/requests –Noninteractive:
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
1 Day 18 Bash and the.files. 2 The.files ls shows you the files in your directory –Or at least most of them. –Some files are hidden. Try: ls –a –This.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1.
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
Configuration your environment Many user-configurable Unix programs (such as your shell) read configuration files when they start up. These configuration.
Various 2. readonly readonly x=4 x=44 #this will give an error (like what in java?)
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
1 CS101 Fall 2001 Lecture 1 In order to write a program, you must first telnet to your pegasus account and login either from a Rutgers computer in a lab,
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Introduction to Scripting Workshop February 23, 2016.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu.
CIRC Winter Boot Camp 2017 Baowei Liu
Department of Computer Engineering
SUSE Linux Enterprise Desktop Administration
Introduction to Shells
Shell Features CSCI N321 – System and Network Administration
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
Shell Scripting March 1st, 2004 Class Meeting 7.
Unix Scripting Session 4 March 27, 2008.
Shell Environments.
Basic UNIX OLC Training.
John Carelli, Instructor Kutztown University
Shell Programming.
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
Lab 5: Complex Inputs.
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Presentation transcript:

Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

First tale Print the types of all the files in the current directory. file * Check it. Correct?

First tale Close, but it misses out on files beginning with a ".". ls is probably a better choice than the wildcard character "*". And how?

First tale Like this? for f in `ls -A` do file $f done Unfortunately, not all ls's have the -A option.

First tale No big deal, since they all have the -a option. How do you know? Well, the earliest version has it; so must all the subsequent versions. So we have the following script for f in `ls -a` do if [ $f !=. -a $f !=.. ] ; then file $f fi done

First tale Seems all right, until some weirdo comes by and protests, "my filenames have spaces!". Fortunately, sh is all too potent for trivia like this. Weapon : IFS –IFS is the "inter-field separator". We set it to the newline character instead of the default space character to make sure that each line (possibly containing spaces) of `ls -a` is considered a single filename. We need to add some double quotes too.

So will the following script work IFS=' '# This is a new line quoted by two ' for f in `ls -a` do if [ "$f" !=. -a "$f" !=.. ] ; then file "$f" fi done First tale

Boom - it still cracks (breaking up filenames with spaces into pieces). After some head-scratching, we got the culprit nailed: the file command, a stiff-necked oddball out there, refuses to give spaces their due respect. Try anything you can (file "a file"... file "'a file'"... file a\ file...), it just wouldn't do it.

First tale Interestingly, if you put those problematic filenames in a file, file happily goes along, as follows. $ echo "a file" >ff $ file -f ff a file: ascii text What can you say? Looks like the script is not as simple as we first thought it is.

Beyond sh S.R. Bourne is probably still up and running, but the shell that bears his name, the Bourne shell (sh), has really gone pass its prime of life. Many had made their offer as a replacement, but only a few made it to the stage: csh tcsh ksh bash zsh Please refer to the article UNIX Shell Differences( ml )for a comparison, and advice on which one you should trust and use.

Bash-- A Better Shell Command-line editing –Default is emacs mode. If you want vi mode, you type –$ set -o vi –Then you can edit your command line just like a line in your vi session. Previous commands are like previous lines, and so you can use vi commands such as j, k to go back and forth.

Bash-- A Better Shell The fc command –fc -l lists the most recent commands in the history list. –fc -s repeats the last command. –fc -s 27 repeats command #27. fc alone lets you edit the last command. And so on.

Bash-- A Better Shell History expansion –You can use the up and down cursor commands of vi or emacs to retrieve, edit, and execute previous commands, or the fc command, or the "!" notation. –For example, "!!" repeats the last command.

Bash-- A Better Shell Aliases –One of the most common aliases: –$ alias ls="ls -l" –If you append a space, then you can do the following. –$ alias ls="ls -l " –$ alias home="/usr/staff/flau" –$ ls home –...

Bash-- A Better Shell Prompt –Setting –PS12="\t | \w -> " –gives you a prompt containing the current time and working directory.

Bash-- A Better Shell Functions –Later versions of sh support functions. –Bash goes one step further: functions can have local variables apart from $1, $2,...

Bash-- A Better Shell Command substitution –The back quotes are ugly looking and clumsy to use when you have to nest them. Bash uses $( ) instead of ` `. Which one in the following d'you prefer? for s in $(cd $d; file $(ls -A) 2>/dev/null | fgrep $i directory | cut -d: -f1) –or for s in `cd $d; file \`ls -A\` 2>/dev/null | fgrep $i directory | cut -d: -f1`

Bash-- A Better Shell Select –In addition to for, while, case, if, bash gives you the handy select construct. Here is an example. Try it and find out why select is so neat. #!/usr/local/bin/bash PS3='Please type a number: ' select x in yes no quit; do case $x in yes) echo YES ; break ;; no) echo NO ; break ;; quit) echo BYE ; break ;; "") echo ERROR - please try again ;; esac done

Bash-- A Better Shell Typed variables The following is self-explanatory. $ x=2 y=5 $ z=x*y $ echo $z x*y $ declare -i z $ z=x*y $ echo $z 10

Bash-- A Better Shell Typed variables –You can also do the following. $ echo $(( * 15 )) 47 $ let z='2 * 5'

Bash-- A Better Shell Job control –You can suspend a job, run a job in the background, and exercise certain control over running or suspended background jobs.

Bash-- A Better Shell Finally, something about the builtin function eval. –Simply put, eval evaluates (executes) an expression that is supplied as a parameter. For example, the eval line in the following actually evaluates the expression "$2". $ set hello world $ eval echo '$'$# world

The End Thank you for your patience.