ULI101 Week 10. Lesson Overview ● Shell Start-up and Configuration Files ● Shell History ● Alias Statement ● Shell Variables ● Introduction to Shell Scripting.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
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.
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.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Linux+ Guide to Linux Certification, Second Edition
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
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,
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
Introduction to Shell Script Programming
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
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.
Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
UNIX/LINUX Shells Shell is an UNIX/LINUX command interpreter. Shell command can be internal or external. The code to execute an internal command is part.
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
Basic Shell Scripting - Part 1 Objective - Learn to: Read Start-up Files Edit Start-up Files Modify Your User Environment Communicate with Users Write.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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.
CS465 - UNIX The Bourne Shell.
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
Workbook 6 – Part 2 The Bash Shell
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
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.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Chapter Six Introduction to Shell Script Programming.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Shell Control Statements and More
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.
Chapter 8: The Bourne Again Shell It’s a command interpreter, it’s a programming language, and it makes a mean martini.
Linux+ Guide to Linux Certification, Second Edition
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.
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
Lesson 8-Specifying Instructions to the Shell. Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing.
Linux Administration Working with the BASH Shell.
COMP075 OS2 Bash Scripting. Scripting? BASH provides access to OS functions, like any OS shell Also like any OS shell, BASH includes the ability to write.
Bash Shell Scripting 10 Second Guide.
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.
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Shell Environments.
Agenda Control Flow Statements Purpose test statement
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
CSC 352– Unix Programming, Fall, 2011
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
LPI Linux Certification
Presentation transcript:

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=“ ”

Read-Only Variables ● Including the keyword readonly before the command assignment prevents you from changing the variable afterwards For example: readonly phone=“ ” ● 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 substitutionExample:name=Bob echo $name

Introduction to Shell Scripting ● Shell programming – Scope ranges from simple day-to-day tasks to large database-driven 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 pressedExample: 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 The condition is tested in square brackets [ ]. If the condition is true, THEN command 1 thru to command N are executed. If the condition is false, then the commands are skipped. fi (opposite of the word “if”) is used to end the if statement. if [ condition ] then command(s) fi

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

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 ]

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: ● for item in list do commans(s) done Variable “item” will hold one item from the list every time the loop iterates List can be typed in explicitly or supplied by a command

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