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

Similar documents
Regular expressions: Text editing and Advanced manipulation. HORT Lecture 4 Instructor: Kranthi Varala

COMP 4/6262: Programming UNIX

Lab 5a Shell Script Lab 4 Using Arithmetic Operators in shell script

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

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Lecture 5. Essential skills for bioinformatics: Unix/Linux

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

Review of Fundamentals

9.2 Linux Essentials Exam Objectives

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

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

Using bash. Administrative Shell Scripting COMP2101 Fall 2017

Bash command shell language interpreter

Title:[ Variables Comparison Operators If Else Statements ]

More Scripting Techniques Scripting Process Example Script

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Shell Scripting. Winter 2019

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

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

Shells and Shell Programming

Introduction to Supercomputing

Shells and Shell Programming

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Essential Skills for Bioinformatics: Unix/Linux

5/20/2007. Touring Essential Programs

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

Bash scripting basics

Introduction: What is Unix?

Shells & Shell Programming (Part B)

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Useful Unix Commands Cheat Sheet

Implementation of a simple shell, xssh

Basic Linux (Bash) Commands

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

Shell Programming. Introduction to Linux. Peter Ruprecht Research CU Boulder

Working with Shell Scripting. Daniel Balagué

CSE 374: Programming Concepts and Tools. Eric Mullen Spring 2017 Lecture 4: More Shell Scripts

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

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

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?

EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018

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

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

Vi & Shell Scripting

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

CSC 2500: Unix Lab Fall 2016

Linux Command Line Interface. December 27, 2017

The UNIX Operating System. HORT Lecture 2 Instructor: Kranthi Varala

Practical 02. Bash & shell scripting

Introduction To. Barry Grant

COMP2100/2500 Lecture 16: Shell Programming I

UNIX System Programming Lecture 3: BASH Programming

Chapter 4. Unix Tutorial. Unix Shell

Introduction to UNIX Part II

Introduction to Shell Scripting

BASH Programming Introduction

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

Basic UNIX commands. HORT Lab 2 Instructor: Kranthi Varala

Bash scripting Tutorial. Hello World Bash Shell Script. Super User Programming & Scripting 22 March 2013

Processes and Shells

UNIX shell scripting

bash Execution Control COMP2101 Winter 2019

EECS2301. Example. Testing 3/22/2017. Linux/Unix Part 3. for SCRIPT in /path/to/scripts/dir/* do if [ -f $SCRIPT -a -x $SCRIPT ] then $SCRIPT fi done

Introduction to Python: Data types. HORT Lecture 8 Instructor: Kranthi Varala

Exercise 1: Basic Tools

Linux shell scripting Getting started *

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

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

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Assignment clarifications

Process Management! Goals of this Lecture!

bash Scripting Introduction COMP2101 Winter 2019

COMS 6100 Class Notes 3

System Programming. Unix Shells

What is Bash Shell Scripting?

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

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 4 Shell Variables, More Shell Scripts (Thanks to Hal Perkins)

COMP 2718: Shell Scripts: Part 1. By: Dr. Andrew Vardy

Answers to AWK problems. Shell-Programming. Future: Using loops to automate tasks. Download and Install: Python (Windows only.) R

ITST Searching, Extracting & Archiving Data

5/8/2012. Specifying Instructions to the Shell Chapter 8

28-Nov CSCI 2132 Software Development Lecture 33: Shell Scripting. 26 Shell Scripting. Faculty of Computer Science, Dalhousie University

bash Data Administrative Shell Scripting COMP2101 Fall 2017

22-Sep CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control. Faculty of Computer Science, Dalhousie University

Chapter 9. Shell and Kernel

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

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

Environment Variables

Process Management 1

5/8/2012. Exploring Utilities Chapter 5

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

The Shell, System Calls, Processes, and Basic Inter-Process Communication

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

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

Shell Programming Overview

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

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

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

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

Linux shell programming for Raspberry Pi Users - 2

Transcription:

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

Text editors Programs built to assist creation and manipulation of text files, typically scripts. nano : easy-to-learn, supports syntax highlighting, lacks GUI. Emacs : provides basic editing functions but also extendible to add functionality. Supports GUI, extensions provide a wide range of functions. vi/vim : extensive editing functions and relatively limited extensibility, command and insert modes distinct, steep learning curve, but very rewarding experience.

Text manipulations Tabular data files can be manipulated at a columnlevel. 1. Cut: Divide file & extract columns. 2. Paste: Combine multiple columns into a single table/file. Sort: Sort lines in a file based on contents of one or more columns. Regular expressions : defining patterns in text. Special characters and quantifiers allow search and replacement of simple-to-complex matches. grep and awk use the power of regular expressions to make text processing very easy.

Command-line operations All commands so far are run one at a time. Redirection and pipes allow combining a few commands together into a single pipeline. Lacks logical complexity, such as ability to make decisions based on input / values in file. Certain repetitive tasks are tedious to user. All commands are being sent to and interpreted by the shell

Client/Server architecture User1 User2 User3 Server (UNIX/ Web/ Database etc..) User4

Terminology Terminal: Device or Program used to establish a connection to the UNIX server Shell: Program that runs on the server and interprets the commands from the terminal. Command line: The text-interface you use to interact with the shell.

Shells Shell itself is a program on the server and can be one of many varieties 1. bash : Most popular shell, default on most Linux systems. Installed on all Linux systems 2. zsh : A bash-like shell with some extra features. E.g., support for decimals, spelling correction etc. 3. tcsh : A C-like syntax for scripting, supports arguments for aliases etc. We will work with bash shell scripting since it is the most common and supported shell.

Environment variables A variable is a container that has a defined value. It s called a variable because the value contained inside it can change. Variables allow changing a part of the command that is to be executed. Every shell has a set of attached variables. See them by using the command env E.g., the variable SHELL contains the path to the current shell.

Working with environment variables Set the value of a variable as follows: FOO=BAR Retrieve the value of a variable as follows: echo $FOO

Example Environment variables On scholar: using the command env shows 99 environment variables: Examples: HOME=/home/kvarala SHELL=/bin/bash HOSTNAME=scholar-fe01.rcac.purdue.edu HISTSIZE=1000 RCAC_SCRATCH=/scratch/scholar/k/kvarala

Environment vs. Shell variables Environment variables are global i.e., shared by all shells started AFTER variable is defined. Shell variables are only present in the shell in which they were defined. Environment variables are inherited by child shells but shell variables are not. Shell variable can be made an environment variable by using export command. FOO=BAR export FOO

Environment vs. Shell variables $ export FOO=BAR (FOO defined in the environment) $ FOO2=BAR2 (FOO2 defined in shell) $ bash (Start new shell) $ echo $FOO BAR (echoes value of FOO) $ echo $FOO2 (empty)

Shell Scripting A script is simply a collection of commands that are intended to run as a group. Commands may or may not be dependent on each other. Variables, hence their values, can be transferred from one command to another. Supports complex choices and logic. A script is always executed in its own shell.

Example Shell Script First example script: Hello world! #!/bin/bash # This is our first shell script!! echo Hello World!

Variables in Shell Scripting Variables are containers that store a value. All variables created in a script are shell variables. A script can access the environment variables in addition to its own shell variables. Variable can store any kind of value ie., string or integer or floating point number etc.

Variables in Shell Scripting INT=1 FLOAT=1.5 STR=hello STR2= hello world RND=asdf2341.sfe echo $INT echo Value of FLOAT is $FLOAT echo $STR is a string echo $RND is non-sensical

Example Shell Script Second example script: lsscr.sh #!/bin/bash # List contents of scratch cd $RCAC_SCRATCH ls l Make script executable, place it in PATH.

Special shell variables Special Variables $# = No. of parameters given to script $@ = List of parameters given to script $0 = Name of current program (script) $1, $2.. = Parameter 1, 2 and so on.. $? = Exit value of last command run These variables are shell variables and only valid to the current shell.

Even more special characters * matches every character, just as in regular expressions. So, ls *txt in a script will list all files whose name ends in txt. \ is an escape character which tells the shell to not interpret the character after it. \ is commonly used to escape the special characters such as *, $ etc.

Example Shell Script Third example script: lsscr.2.sh #!/bin/bash # List contents of scratch echo Executing script : \ $0\ with $# parameters cd $RCAC_SCRATCH ls l Make script executable, place it in PATH.

Command Blocks Two fundamental blocks in scripting: Loops Repeat the commands in the block until the exit condition is met. Conditions Evaluate condition and if true execute commands in the block.

Loops Two kinds of loops supported in bash: for loop operates on a list and repeats commands in the block for each element on the list while loop repeats commands in the block until an exit condition is met.

for loops for loop operates on a list and repeats commands in the block for each element on the list for x in [ list ]; do done commands

for loops for loop operates on a list and repeats commands in the block for each element on the list for x in $( ls ); do done echo Found file $x

for loops for loop operates on a list and repeats commands in the block for each element on the list for x in 1 2 3 4 5 6 7 8 9 10; do done echo Value of x is : $x

while loops while loop repeats commands until exit condition is met while condition; do done echo Value of x is : $x

while loops while loop repeats commands until exit condition is met x=10 while [ $x gt 0 ]; do done echo Value of x is : $x x=x-1

Shell Scripting Condition blocks test for a condition and if TRUE execute one block and if FALSE execute another. if [ condition ] then else fi Block 1 Block 2

Shell Scripting Condition blocks test for a condition and if TRUE execute one block and if FALSE execute another. x = 5 if [ $x gt 0 ] then else fi echo $x is divisible echo 0 is not divisible

breaking loops Break command asks the shell to exit the loop x=10 while [ 1 ]; do echo Value of x is : $x x=x-1 if [ $x == 0 ] break done

Run external commands backticks are a way to send a command to the shell and capture the result. It s a special character : ` Eg., files = `ls *txt` echo $files

Functions in shell Scripting Functions separate logical blocks of code. Typically a function contains a piece of code that is used repeatedly in a script. Code in a function is only executed when a function is called. We will cover functions in tomorrows lab section.