Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Similar documents
Bourne Shell Reference

UNIX shell scripting

9.2 Linux Essentials Exam Objectives

Shell programming. Introduction to Operating Systems

A shell can be used in one of two ways:

Shell Programming (bash)

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

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

Computer Systems and Architecture

Computer Systems and Architecture

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

bash Execution Control COMP2101 Winter 2019

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

bash, part 3 Chris GauthierDickey

Assignment clarifications

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Useful Unix Commands Cheat Sheet

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Shells & Shell Programming (Part B)

Shells and Shell Programming

Bash scripting basics

Shells and Shell Programming

Vi & Shell Scripting

CSC 2500: Unix Lab Fall 2016

Week 6 Lesson 1: Control-Flow Statements (Continued)

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

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

UNIX System Programming Lecture 3: BASH Programming

Conditional Control Structures. Dr.T.Logeswari

While Statement Examples. While Statement (35.15) Until Statement (35.15) Until Statement Example

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

COMP 4/6262: Programming UNIX

Lecture 8. Introduction to Shell Programming. COP 3353 Introduction to UNIX

CS Unix Tools & Scripting

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

RHCE BOOT CAMP. System Administration

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

Shell script/program. Basic shell scripting. Script execution. Resources. Simple example script. Quoting

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

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

INd_rasN SOME SHELL SCRIPTING PROGRAMS. 1. Write a shell script to check whether the name passed as first argument is the name of a file or directory.

Bash shell programming Part II Control statements

Linux shell scripting Getting started *

Environment Variables

UNIX Shell Scripts. What Is a Shell? The Bourne Shell. Executable Files. Executable Files: Example. Executable Files (cont.) CSE 2031 Fall 2012

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

SANJAY GHODAWAT POLYTECHNIC

Shell Start-up and Configuration Files

Linux shell programming for Raspberry Pi Users - 2

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Last Time. on the website

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

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

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

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

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

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

Chapter 5 The Bourne Shell

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

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

There are some string operators that can be used in the test statement to perform string comparison.

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

Lecture 02 The Shell and Shell Scripting

Implementation of a simple shell, xssh

Practice 5 Batch & Shell Programming

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

Csci 132 Spring 13. Assignment 2 Due: Tuesday, March 5 (by 11:59PM)

EECS2301. Special variables. $* and 3/14/2017. Linux/Unix part 2

System Programming. Unix Shells

Linux Essentials. Smith, Roderick W. Table of Contents ISBN-13: Introduction xvii. Chapter 1 Selecting an Operating System 1

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

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash

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

Part 1: Basic Commands/U3li3es

Using bash. Administrative Shell Scripting COMP2101 Fall 2017

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

Bourne Shell Programming Topics Covered

Grep and Shell Programming

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

More Scripting Techniques Scripting Process Example Script

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

Essential Skills for Bioinformatics: Unix/Linux

Lab 4: Shell scripting

I/O and Shell Scripting

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

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

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

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

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

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

Shell Programming (ch 10)

Introduction: What is Unix?

Shell scripting Scripting and Computer Environment - Lecture 5

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

/smlcodes /smlcodes /smlcodes. Shell Scripting TUTORIAL. Small Codes. Programming Simplified. A SmlCodes.Com Small presentation

CSE 15L Winter Midterm :) Review

Prof. Navrati Saxena TA: R. Sachan Bharat Jyoti Ranjan

Basic UNIX Commands BASIC UNIX COMMANDS. 1. cat command. This command is used to create a file in unix. Syntax: $ cat filename

Transcription:

Advanced Unix Programming Module 03 Raju Alluri askraju @ spurthi.com

Advanced Unix Programming: Module 3 Shells & Shell Programming Environment Variables Writing Simple Shell Programs (shell scripts) Condition checks Iterative commands Writing Advanced shell scripts

Environment Variables Environment Variables are the variables used by shell to determine certain values How to see them env more echo $<variable> as in echo $DISPLAY env grep <variable> Commonly used environment variables DISPLAY, USER, PATH etc. It is a convention to use capitalized names

Setting Environment Variables A Variable can be set by a very simple syntax VAR=value Once you set, you can export the variable using the syntax export VAR Then only it becomes an Environment Variable Environment variables are inherited by all the processes spawned by the current process

The PATH Environment Variable The PATH variable indicates the list of directories from which executable files are to be fetched. e.g. Most of the executables are taken for /usr/bin and /usr/sbin The sequence of directories in PATH controls the sequence of search

Writing Simple Shell Scripts Create a file with.sh extension $ touch myscript.sh Give it execute permissions $ chmod +x myscript.sh Edit the file to write the code (see next slide for example code) $ vi myscript.sh After saving the file, execute it $./myscript.sh

Syntax of a shell script Always starts with the directive #!/bin/sh to indicate that this is a shell script #!/bin/sh Comment lines start with # character # This is a comment line. Any command will look like as if you executed it in the command shell echo Hello World! It is a good idea to end the program with an exit value (0 for success, non zero for error value) exit 0

Example Shell Script Example Shell Script #!/bin/sh # This is a comment line. echo Hello World! exit 0

Exit Values Significance of exit values Traditionally, exit value 0 indicates successful execution. Non zero values indicates some error condition How to see the exit value of a script $./myscript.sh $ echo $? 0 $

Using Input Arguments The $ character is used to access input arguments to the script $0 denotes name of the executing script $n denotes nth argument $* denotes all input arguments $n denotes number of input arguments $$ denotes PID of the executing script #!/bin/sh echo Hello World from $0 exit 0

Traversing Input Arguments $1 points to first argument and $# gives the count of arguments After you use shift, $1 points to the next argument and $# will be decremented by 1, until it reaches 0

Condition Checks: if The if construct checks if a particular check is successful or not Example: Check if the input arguments are given or not if [ $# gt 0 ]; then echo Input arguments are given else echo No input arguments fi The elif keyword is used for a sequence of condition checks.

Iteration: while The while construct checks if a particular condition is valid or not, and executes the commands repeatedly as long as the condition is valid Example: Print the date 5 times MYVAR=0; while [ $MYVAR lt 5 ]; do done /usr/bin/date MYVAR=`expr $MYVAR + 1`

Iteration: for The for loop executes for each value of the string that exists in a variable. Example: for val in $LIST do echo $val is the value done

Multiple Conditions: case The case statement begins with case and ends with esac. Each condition match is evaluated one after another. * matches with any value. case $1 in h ) echo printing help... break;;... *) echo default esac

Executing other programs You can execute other programs/scripts by Using Absolute path Using relative path from current directory Depending on PATH environment variable Prefered in some special cases, but not recommended all the time Taking the output from a command Keep it in an environment variable DATE=`/usr/bin/date` HOWMANY=`/usr/bin/who /usr/bin/wc l ` Redirect the output to a file and process the file later

Using /dev/null Sometimes, you need to execute a program, and need only the exit value, but not the output In such case, redirect the output to a special file /dev/null For example, all the errors (standard error file descriptor) can be redirected to /dev/null, if the errors are not important

Modularity: Subroutines At the beginning of the script, write all the subroutines mysubroutine() { } At any point later in the script, call the subroutine, with arguments mysubroutine hello world Subroutines extract the arguments just like the regular scripts would do ($1 etc.) Sobroutines can call other subroutines, provided they are already defined earlier in the script

Including code from other scripts If you need some code to be used by several scripts, you can place it in a common file and include it in all the scripts. mycommoncode.sh

Checking multiple conditions Logical operations can be done on multiple conditions to derive complex condition checks.!, &&, Note: && and evaluate the second argument only when needed. if [ $HOLIDAY && $GOOD_MOVIE ]; then

File checks Several checks can be done on files/directories to test the existence, permissions etc. f checks if the file exists or not and if it is a regular file d checks if the file exists and is a directory r checks if the file exists and is readable w checks if the file exists and is writable x checks if the file exists and is executable if [ $DATA && f $OUTFILE ]; then For a complete list of possible checks, see man sh

Shell Programming Summary You can use sh vx <script_name> to run shell scripts in verbose mode Shell Programming is very simple and powerful feature Shell scripts can be run on any Unix system Shell scripts are interpreted by the shell