Sub-Topic 1: Quoting. Topic 2: More Shell Skills. Sub-Topic 2: Shell Variables. Referring to Shell Variables: More

Similar documents
Topic 2: More Shell Skills

Topic 2: More Shell Skills. Sub-Topic 1: Quoting. Sub-Topic 2: Shell Variables. Difference Between Single & Double Quotes

Topic 2: More Shell Skills

Lecture 4. Log into Linux Reminder: Homework 1 due today, 4:30pm Homework 2 out, due next Tuesday Project 1 out, due next Thursday Questions?

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Review of Fundamentals

bash, part 3 Chris GauthierDickey

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Log into Linux Reminders: Homework 1 due today; Homework 2 due on Thursday Questions?

Introduction: What is Unix?

Introduction to Bash Programming. Dr. Xiaolan Zhang Spring 2013 Dept. of Computer & Information Sciences Fordham University

Common File System Commands

Part 1: Basic Commands/U3li3es

CSE 303 Lecture 4. users/groups; permissions; intro to shell scripting. read Linux Pocket Guide pp , 25-27, 61-65, , 176

Shell Start-up and Configuration Files

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2017

Chapter 9. Shell and Kernel

Last Time. on the website

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

More Scripting Techniques Scripting Process Example Script

Unix Introduction to UNIX

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

Linux Training. for New Users of Cluster. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

The Unix Shell & Shell Scripts

5/20/2007. Touring Essential Programs

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

Linux shell programming for Raspberry Pi Users - 2

Introduction to UNIX Part II

Processes. Shell Commands. a Command Line Interface accepts typed (textual) inputs and provides textual outputs. Synonyms:

Introduction to UNIX Shell Exercises

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

Lab 3a Using the vi editor

CHE3935. Lecture 1. Introduction to Linux

A Brief Introduction to the Linux Shell for Data Science

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

CISC 220 fall 2011, set 1: Linux basics

Lab 2: Linux/Unix shell

EECS2301. Lab 1 Winter 2016

More Raspian. An editor Configuration files Shell scripts Shell variables System admin

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Introduction to Linux Basics Part II. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

UNIX COMMANDS AND SHELLS. UNIX Programming 2015 Fall by Euiseong Seo

Introduction to Linux Workshop 1

CST Algonquin College 2

PROGRAMMAZIONE I A.A. 2015/2016

Essential Linux Shell Commands

Linux & Shell Programming 2014

Understanding bash. Prof. Chris GauthierDickey COMP 2400, Fall 2008

1. What statistic did the wc -l command show? (do man wc to get the answer) A. The number of bytes B. The number of lines C. The number of words

Windshield. Language Reference Manual. Columbia University COMS W4115 Programming Languages and Translators Spring Prof. Stephen A.

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 2

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

UNIX Shell Programming

Introduction to Linux Part 2b: basic scripting. Brett Milash and Wim Cardoen CHPC User Services 18 January, 2018

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12)

The Command Line. Matthew Bender. Friday 18 th September, CMSC Command Line Workshop

Using UNIX. -rwxr--r-- 1 root sys Sep 5 14:15 good_program

Open up a terminal, make sure you are in your home directory, and run the command.

Practical 4. Linux Commands: Working with Directories

Perl and R Scripting for Biologists

Unix Tutorial Haverford Astronomy 2014/2015

Shell Scripting. With Applications to HPC. Edmund Sumbar Copyright 2007 University of Alberta. All rights reserved

Principles of Bioinformatics. BIO540/STA569/CSI660 Fall 2010

CSE 390a Lecture 4. Persistent shell settings; users/groups; permissions

A shell can be used in one of two ways:

Basic Unix Command. It is used to see the manual of the various command. It helps in selecting the correct options

5/8/2012. Exploring Utilities Chapter 5

Prof. Navrati Saxena TA: R. Sachan

Mills HPC Tutorial Series. Linux Basics II

Assignment clarifications

Unix tutorial. Thanks to Michael Wood-Vasey (UPitt) and Beth Willman (Haverford) for providing Unix tutorials on which this is based.

3/8/2017. Unix/Linux Introduction. In this part, we introduce. What does an OS do? Examples

Basic Linux Command Line Interface Guide

B a s h s c r i p t i n g

Shell scripting and system variables. HORT Lecture 5 Instructor: Kranthi Varala

Basic Linux Command Line Interface Guide

CSE 390a Lecture 3. bash shell continued: processes; multi-user systems; remote login; editors

Introduction to Unix The Windows User perspective. Wes Frisby Kyle Horne Todd Johansen

bash startup files Linux/Unix files stty Todd Kelley CST8207 Todd Kelley 1

Introduction to Linux Spring 2014, Section 02, Lecture 3 Jason Tang

Introduction to the Shell

27-Sep CSCI 2132 Software Development Lab 4: Exploring bash and C Compilation. Faculty of Computer Science, Dalhousie University

Exploring UNIX: Session 3

Bash Check If Command Line Parameter Exists

Introduction to the Linux Command Line January Presentation Topics

Shells. A shell is a command line interpreter that is the interface between the user and the OS. The shell:

Basics. I think that the later is better.

Linux Systems Administration Shell Scripting Basics. Mike Jager Network Startup Resource Center

System Programming. Unix Shells

GNU/Linux 101. Casey McLaughlin. Research Computing Center Spring Workshop Series 2018

I/O and Shell Scripting

Linux shell scripting Getting started *

Introduction to UNIX. Introduction. Processes. ps command. The File System. Directory Structure. UNIX is an operating system (OS).

Introduction to UNIX. CSE 2031 Fall November 5, 2012

UNIX and Linux Essentials Student Guide

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

Transcription:

Topic 2: More Shell Skills Plan: about 3 lectures on this topic Sub-topics: 1 quoting 2 shell variables 3 sub-shells 4 simple shell scripts (no ifs or loops yet) 5 bash initialization files 6 I/O redirection & pipes 7 aliases Sub-Topic 1: Quoting Useful command: echo Prints all its arguments to the screen Normal rules: spaces separate words in commands some characters have special meanings to bash Sometimes useful to quote strings or characters. Three quoting mechanisms: 'string': every character in the string taken literally "string": most characters taken literally exceptions later \c: character taken literally 1 2 Sub-Topic 2: Shell Variables Referring to Shell Variables: More shell variables: a way to store information in bash To give a value to a variable: today=tuesday If no "today" variables exists, creates it. Sets value to the string "Tuesday" To refer to value of variable: use $. Example: echo $today Awkward situation: malamb@linux5:~$ object=computer malamb@linux5:~$ echo "I have two $objects" I have two malamb@linux5:~$ Why? Fix: malamb@linux5:~$ echo "I have two ${object}s" I have two computers 3 4

Shell Variables and Quotes The $ character retains its special meaning inside double quotes. echo "Today is $today" Not inside single quotes. Uses of Shell Variables store information for use by programs, bash also very useful in shell scripts (soon!) To see all variables with values: set 5 6 Shell Variables Are Temporary Changes & new variables only last until you log out. To make a permanent change: use an initialization file (soon!) PATH Contains list of directories separated by ":"s. When you type ls Bash looks for a command named "ls". Searching order: 1. Is it a built-in shell command? 2. In each directory in $PATH: is there a program called "ls"? 7 8

PS1 Useful Variables Set By bash $PS1 is your bash prompt. Can contain literal characters plus some special ones: \d: the current date \h: the name of the host machine \j: number of jobs you have running \s: the name of the shell \u: name of user \w: current working directory \!: history number of this command $HOME: your home directory $PWD: current directory $SHELL: path name of current shell $USER: your user name 9 10 Sub-Topic 3: Sub-Shells Sub-Topic 4: Simple Shell Scripts A shell is a program. Linux automatically runs the bash program when you log on. You can run it too. One reason: experiment with settings. Command to start a sub-shell: bash Exit back to login shell: exit Use Shell variable $SHLVL to see if you're in a sub-shell A sub-shell "inherits" many of your settings, including current folder, umask Does not automatically inherit variables or aliases export command: says variable should be inherited by sub-shells export myvar shell script = file containing bash commands Comments in scripts: # means rest of line is a comment Special variable names: $0: name of command $#: number of arguments $1,$2,...: the individual arguments $*: all the arguments in one string, separated by spaces 11 12

Executing a Shell Script Putting "." in Your Path First step: must be executable (chmod) Type command name alone: bash looks in your PATH directories Type command name with directory: no lookup needed Advantage: You can run scripts & programs in your current directory without specifying the directory Disadvantages: 1. Security: Have you downloaded a malicious program called "ls"? 2. Possibility of confusion: Have you created a program with the same name as another program earlier on your path? 13 14 Complication: scripts & sub-shells Special Initial Comment When you type the name of a script as a command: executed in a sub-shell. Consequence: can't use to create aliases or set variables. If you want script to affect current shell: source myscript Executes commands from myscript in the current shell. #!/bin/bash At beginning of script, specifies this is a script to be run with bash. Why bother? Syntax of some commands different for every shell. Now your script will work for people using other shells. Not required for 220, but generally a good idea. 15 16

Sub-Topic 5: Bash Initialization Files In your home directory:.bash_profile: executed when you log in (with source).bashrc: executed when you start up a sub-shell Use these files for your own settings (variables, etc.) Sub-Topic 6: I/O Redirection & Pipes Three standard files associated with any command or program: file 0: standard input: default is keyboard file 1: standard output: for regular output, default is screen file 2: standard error: for error messages, default is screen I/O Redirection can change the defaults for these three files. Advice: before you change, make a copy! don't remove calls to standard system files Sample initialization files in /cas/course/cisc220 17 18 Command For Examples Redirecting Standard Input Command to use as example: sort. Reads standard input and sorts to standard output. demo... (use ^d to end input) command 0< file Reads input from file, instead of keyboard. Shorter form: command < file example: sort < myfile 19 20

Redirecting Standard Output command 1> file Sends standard output of command to file, instead of screen. Shorter form: command > file Output Redirection: Existing Files If file already exists, bash will refuse command > file: if file already exists, overwrite command >> file: if file already exists, append Example: sort > file Example of redirecting standard input and output: sort < input > output 21 22 Redirecting Standard Error Getting Rid of Output You Don't Want command 2>filename example: ls file_thats_not_here 2>errs send to /dev/null Example: cmd produces error messages you want to throw away: cmd 2>/dev/null 23 24

Redirecting Both Output Files Pipes To 2 separate files: cmd 1>goodOutput 2>errOutput or: cmd >goodoutput 2>errOutput To the same file: cmd 1>outFile 2>&1 or: cmd 2>outFile 1>&2 means file 2 is sent to same place as file 1 cmd1 cmd2 Sends standard output of cmd1 to standard input of cmd2 Examples: command less command sort command sort > output command 2>&1 command2 cmd1 cmd2 cmd3 25 26 Sub-Topic 7: Aliases Interesting Aliases Set For You Alias = easy way to create synonym for a command provides shorthand for something you do often alias long="ls l" rm="rm i" cp="cp i" mv="mv i" interactive mode asks before overwriting file asks before overwriting file Also a way to turn on certain options always. Example from standard.bashrc file: alias rm="rm -i" ls="ls F" adds characters at end of file names To execute original program (without alias): quote the name "rm" file1 file2 To cancel an alias: unalias rm To see all your aliases: alias (no arguments) 27 28

Extra Topic: Text File Formats Basic text file format: ASCII or Unicode characters Problem: Different OSs, different character(s) for end of line! Unix/Linux: '\n' (newline) Windows/Dos: '\r' + '\n' (carriage return + newline) Mac: sometimes '\r', sometimes '\n'! Some programs can deal with any of these formats. Others are confused by some formats. Windows: try Notepad++ and Notepad Linux: try cat and emacs Two Useful Tools wc ("word count"): number of lines, words & characters in a file od ("octal dump"): listing of bytes in a file default or -o: each pair of bytes as an octal number -c: each byte as a character -x: each pair of bytes as a hexadecimal number -d: each pair of bytes as a decimal number 29 30 Fixing With FTP Conversion Tools (in Linux) Proper settings in FTP program can avoid problems will translate text files into appropriate format while transferring unix2dos myfile: if myfile is in Unix/Linux text format, converts to Dos/Windows otherwise, does nothing dos2linux myfile: if myfile is in Unix/Linux text format, converts to Dos/Windows otherwise, does nothing Notice: overwrites original! 31 32