Shell Start-up and Configuration Files

Similar documents
Control Structures. CIS 118 Intro to LINUX

Title:[ Variables Comparison Operators If Else Statements ]

COMP 4/6262: Programming UNIX

Essentials for Scientific Computing: Bash Shell Scripting Day 3

9.2 Linux Essentials Exam Objectives

example: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable.

Unix Shell scripting. Dr Alun Moon 7th October Introduction. Notation. Spaces

Examples: Directory pathname: File pathname: /home/username/ics124/assignments/ /home/username/ops224/assignments/assn1.txt

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Useful Unix Commands Cheat Sheet

Bash scripting basics

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

LING 408/508: Computational Techniques for Linguists. Lecture 5

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

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

Computer Systems and Architecture

Linux shell programming for Raspberry Pi Users - 2

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

A shell can be used in one of two ways:

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

Shell Script Example. Here is a hello world shell script: $ ls -l -rwxr-xr-x 1 horner 48 Feb 19 11:50 hello* $ cat hello #!/bin/sh

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

Shell programming. Introduction to Operating Systems

Conditional Control Structures. Dr.T.Logeswari

Review of Fundamentals

Scripting. More Shell Scripts. Adapted from Practical Unix and Programming Hunter College

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

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

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

Topic 2: More Shell Skills

Basic Linux (Bash) Commands

Last Time. on the website

Lecture 5. Essential skills for bioinformatics: Unix/Linux

Bourne Shell Reference

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

Unix Scripts and Job Scheduling. Overview. Running a Shell Script

Today. Review. Unix as an OS case study Intro to Shell Scripting. What is an Operating System? What are its goals? How do we evaluate it?

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

bash Execution Control COMP2101 Winter 2019

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

Essential Skills for Bioinformatics: Unix/Linux

Shell Programming (bash)

UNIX shell scripting

Bash shell programming Part II Control statements

CS Unix Tools & Scripting

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

Chapter 4. Unix Tutorial. Unix Shell

Shell script. Shell Scripts. A shell script contains a sequence of commands in a text file. Shell is an command language interpreter.

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

CSE 391 Lecture 5. Intro to shell scripting

5/20/2007. Touring Essential Programs

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Linux 系统介绍 (III) 袁华

My Favorite bash Tips and Tricks

Cisco IOS Shell. Finding Feature Information. Prerequisites for Cisco IOS.sh. Last Updated: December 14, 2012

Computer Systems and Architecture

CSE 390a Lecture 5. Intro to shell scripting

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

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

Bourne Shell (ch 8) Overview. Bourne Shell. Bourne Shell. Bourne Shell. Bourne Shell. Redirect standard error. Redirect standard error

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

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Introduction to the Shell

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

Linux Bash Shell Scripting

UNIX System Programming Lecture 3: BASH Programming

Topic 2: More Shell Skills

Shells and Shell Programming

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

More Scripting Techniques Scripting Process Example Script

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

Assignment clarifications

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Vi & Shell Scripting

Shells and Shell Programming

Exploring UNIX: Session 3

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

This is Lab Worksheet 3 - not an Assignment

Processes and Shells

Програмиранев UNIX среда

Basics. I think that the later is better.

UNIX and Linux Essentials Student Guide

Linux Operating System Environment Computadors Grau en Ciència i Enginyeria de Dades Q2

UNIX Shell Programming

CSE 390a Lecture 2. Exploring Shell Commands, Streams, and Redirection

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

Introduction to UNIX Part II

OPERATING SYSTEMS LAB LAB # 6. I/O Redirection and Shell Programming. Shell Programming( I/O Redirection and if-else Statement)

Overview of the UNIX File System

DAVE LIDDAMENT INTRODUCTION TO BASH

CS Unix Tools. Lecture 3 Making Bash Work For You Fall Hussam Abu-Libdeh based on slides by David Slater. September 13, 2010

Part 1: Basic Commands/U3li3es

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Chapter 9. Shell and Kernel

Introduction of Linux

CSC 2500: Unix Lab Fall 2016

INTRODUCTION TO SHELL SCRIPTING ITPART 2

Essential Linux Shell Commands

Bash command shell language interpreter

Basic Shell Scripting

Transcription:

ULI101 Week 10

Lesson Overview Shell Start-up and Configuration Files Shell History Alias Statement Shell Variables Introduction to Shell Scripting Positional Parameters echo and read Commands if and test statements for loop

Shell Start-up and Configuration Files Shell Start-up/Configuration files are settings that are applied every time a shell is created Start-up files are sequences of shell commands (scripts) They also apply when users log in, as it creates a shell There is a single system-wide configuration file that belongs to the root user - /etc/profile User-specific configuration files that belong to the user are hidden files found in the user s home directory.bash_profile.bashrc.bash_logout Executed when you log out

/etc/profile This file can only be modified by the root user Affects the environment of all users, regardless of their default shell Bash users can change their environment by modifying the.bash_profile or the.bashrc files Different shells have different configuration files Other configuration files such as.profile exist read comments in your.bash_rc file to find out more

.bashrc and.bash_profile Located in the user s home directory These files are executed every time a user logs in or creates a new shell Things vary depending whether the shell is interactive or not By modifying either one of these files, each user can change his individual working environment They can be used for the following: Setting the prompt and screen display Creating local variables Creating temporary Linux commands (aliases) Mapping new keys on the keyboard

Shell History Many shells keep a history of recently executed command lines in a file This history is used by users to save time, when executing same or similar commands over and over Bash uses the up/down arrow keys Use the Ctrl+r to search by keyword Bash stores it s history in the.bash_history file

Alias A way to create shortcuts or temporary commands in UNIX Stored in memory, while the user is logged in Usually found in the.bash_profile Syntax: alias name=value For example: alias dir=ls Even complex command lines can have an alias enclose the command within double quotes For example: alias clearfile= cat /dev/null >

Shell Variables Shell variables a classified in 2 groups System (shell) variables, describing the working environment User-created variables, associated with scripts Variables can be read/write or read-only Name of a variable can be any sequence of letters and numbers, but it must not start with a number

Common Shell Variables Shell environment variables shape the working environment whenever you are logged in Common shell variables include: PS1 primary prompt PWD present working directory HOME absolute path to user s home PATH list of directories where executables are HOST name of the host USER name of the user logged in SHELL current shell The set command will display all available variables

The PATH variable PATH is an environment variable present in Unix/Linux operating systems, listing directories where executable programs are located Multiple entries are separated by a colon (:) Each user can customize a default system-wide PATH The shell searches these directories whenever a command is invoked in sequence listed for a match In case of multiple matches use the which utility to determine which match has a precedence On some systems the present working directory may not be included in the PATH by default Use./ prefix or modify the PATH as needed

Assigning a Value Syntax: name=value For example: course=uli101 If variable values are to contain spaces or table they should be surrounded by (double) quotes For example: phone= 1 800 123-4567

Read-Only Variables Including the keyword readonly before the command assignment prevents you from changing the variable afterwards For example: readonly phone= 123-4567 After a variable is set, it can be protected from changing by using the readonly command Syntax: readonly variable For example: readonly phone If no variable name is supplied a list of defined read only variables will be displayed

Removing Variables variable= For example: course= OR unset variable For example: unset phone Read-only variables cannot be removed you must log out for them to be cleared

Variable Substitution Whenever you wish to read a variable (its contents), use the variable name preceded by a dollar sign ($) This is commonly called variable substitution Example: name=bob echo $name

Introduction to Shell Scripting Shell programming Scope ranges from simple day-to-day tasks to large databasedriven CGI applications Shell-dependent each shell script is written for a specific shell, such as bash First line of each script usually specifies the path to the program which executes the script - #! statement, for example: #/bin/bash Use the which utility to find out what path to use there This must be the first line and nothing can precede it, not even a single space This line is not necessary if the script will be executed in the default shell of the user Any line other than first one starting with a # is treated as a comment

Positional Parameters Every script can have parameters supplied Traditionally command line parameters are referred to as $0 $9 Parameters > $9 can be accessed by using the shift command shift will literally shift parameters to the left by one or more positions Some shells can use the ${ } form This enables direct access to parameters >$9 For example: ${10}

Positional parameters $* and $@ represent all command line arguments $# represents the number of parameters (not including the script name)

echo command Displays messages to the terminal followed by a newline Use the n option to suppress the default newline Output can be redirected or piped Arguments are usually double quoted

read command The read command allows obtaining user input and storing it in a variable Everything is captured until the Enter key is pressed Example: echo n What is your name? read name echo Hello $name

Using Logic The purpose of the if statement is execute a command or commands based on a condition The condition is evaluated by a test command, represented below by a pair of square brackets if [ condition ] then command(s) fi

if Statement Example read password Test with a condition Notice the spaces after [ and before ] if [ $password = P@ssw0rd! ] then echo BAD PASSWORD! fi

The test Command The test command can be used in two ways: As a pair of square brackets: [ condition ] The test keyword: test condition The condition test can result in true (0) or false (1), unless the negation is not (!), is used The test can compare numbers, strings and evaluate various file attributes Use = and!= to compare strings, for example: [ $name = Bob ] Use -z and -n to check string length, for example: [! -z $name ] Use -gt, -lt, -eq, -ne, -le, -ge for number, for example: [ $salary -gt 100000 ]

The Test Command Common file test operations include: -e (file exists) -d (file exists and is a directory) -s (file exists and has a size greater than zero) -w (file exists and write permission is granted) Check man test for more details

Using Loops A for loop is a very effective way to repeat the same command(s) for several arguments such as file names Syntax: Variable item will hold one item from the list every time the loop iterates for item in list do commans(s) done List can be typed in explicitly or supplied by a command

Loop Examples for for addr addr in in $(cat $(cat ~/addresses) do do mail mail -s -s Newsletter $addr $addr < ~/spam/newsletter.txt done done for for count count in in 33 22 11 'BLAST 'BLAST OFF!!!' OFF!!!' do do sleep sleep 11 for for id id in in $(seq $(seq 11 1000) echo 1000) echo $count $count do done do done mkdir mkdir student_$id student_$id done done