SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

Similar documents
SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

COMP 4/6262: Programming UNIX

CSC 2500: Unix Lab Fall 2016

Shell programming. Introduction to Operating Systems

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

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

Conditional Control Structures. Dr.T.Logeswari

Useful Unix Commands Cheat Sheet

9.2 Linux Essentials Exam Objectives

bash Execution Control COMP2101 Winter 2019

Bash scripting basics

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

Preview. Review. The test, or [, command. Conditions. The test, or [, command (String Comparison) The test, or [, command (String Comparison) 2/5/2019

Vi & Shell Scripting

Practice 5 Batch & Shell Programming

Title:[ Variables Comparison Operators If Else Statements ]

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

A shell can be used in one of two ways:

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 scripting Tutorial. Hello World Bash Shell Script. Super User Programming & Scripting 22 March 2013

Linux Shell Script. J. K. Mandal

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

Bourne Shell Reference

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

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

RHCE BOOT CAMP. System Administration

CS Unix Tools & Scripting

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

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

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

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

Grep and Shell Programming

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Lab 3a Using the vi editor

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

Control Structures. CIS 118 Intro to LINUX

1. Hello World Bash Shell Script. Last Updated on Wednesday, 13 April :03

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

Original Script. Display commands to manually creating an account. #!/bin/bash

Linux shell programming for Raspberry Pi Users - 2

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

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

Introduction to Linux (Part II) BUPT/QMUL 2018/03/21

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

Computer Systems and Architecture

Essential Skills for Bioinformatics: Unix/Linux

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

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

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

Bash shell programming Part II Control statements

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

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

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

Lecture 5. Essential skills for bioinformatics: Unix/Linux

CHE3935. Lecture 1. Introduction to Linux

Essential Unix (and Linux) for the Oracle DBA. Revision no.: PPT/2K403/02

The Unix Shell & Shell Scripts

UNIX shell scripting

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Introduction to Shell Scripting

Linux shell & shell scripting - II

CSE 390a Lecture 6. bash scrip'ng con'nued; remote X windows; unix 'dbits

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Basic File Attributes

Computer Systems and Architecture

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

Software I: Utilities and Internals. What is vi?

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

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

Shell Programming (bash)

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

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

DAVE LIDDAMENT INTRODUCTION TO BASH

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

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

Using bash. Administrative Shell Scripting COMP2101 Fall 2017

Shells & Shell Programming (Part B)

LOG ON TO LINUX AND LOG OFF

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

Editors in Unix come in two general flavours:

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

I/O and Shell Scripting

CSE 390a Lecture 6. bash scripting continued; remote X windows; unix tidbits

Lab 4: Shell scripting

Lecture 02 The Shell and Shell Scripting

Assignment clarifications

Shell Programming (Part 2)

Linux Systems Administration Getting Started with Linux

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

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

Bash Shell Programming Helps

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

INTRODUCTION TO SHELL SCRIPTING Dr. Jeffrey Frey University of Delaware, IT

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

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

30 Bash Script Examples

Introduction: What is Unix?

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

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

Transcription:

SHELL SCRIPT BASIC UNIX Programming 2014 Fall by Euiseong Seo

Shell Script Interactive shell sequentially executes a series of commands Some tasks are repetitive and automatable They are what programs are for Shell script is a set of shell commands and directives Similar to programs To be executed sequentially, and sometimes repeatedly

A Simple Shell Script #!/bin/bash echo Hello, World!

Another Example Logout when a specific file does not exist #!/bin/bash if test! -f $FILE then if test "$WARN" = "yes" then echo "$FILE does not exist" logout fi fi

Editors UNIX provides huge number of editor options cat ed Line editors are too primitive Screen (or visual) editors and GUI editors are being popularly used

VI Editor

What is VI Installed on most UNIX systems De-facto standard editor for CLI Also defined by POSIX Originally written by Bill Joy in 1976 VI derivatives Vim(proved) n Syntax highlighting, mouse support and many other new features Elvis nvi vile busybox n A set of standard Linux utilities in a single executable n Including a tiny VI clone n Being used for embedded systems

Modes of Operation Insertion mode What you input will be put into the current file Command mode You can input commands to manipulate the current file or move cursor Default mode Mode switch Insertion to command n Esc key Command to insertion n a, i and o

Cursor Move Basic move $ end of the current line 0 beginng of the current line :30 30 th line from the current line

Deletion x delete only the current character dd delete the current line D delete to the end of line d10 delete 10 lines from the current line p past the deleted lines after the current line

Copy and Paste Yanking Copying in VI yy yank a single line y10 yang the following 10 lines including the current line p past the yanked lines

Save and Quit :w save the current file :q quit vi :wq save and quit

Search and Replace /[pattern] search forward for the pattern?[pattern] search backward for the pattern n search for the next instance of a string :%s/foo/bar/g find each occurrence of foo in all lines, and replace them with bar :s/foo/bar/g find each occurrence of foo in the current line, and replace them with bar

Shell Script Syntax

Shebang Statement Every shell script begins with a shebang statement Declaration of the interpreter to interpret the script Format #!<interpreter path> #!/bin/bash #!/usr/bin/perl #!/usr/bin/python Script must be executable to be interpreted chmod a+x scriptname

Input and Output echo and printf echo is crude but easy If you want formatting, use printf

Input and Output read command to prompt for input #!/bin/bash echo -n "Enter your name: " read user_name if [ -n "$user_name" ]; then echo "Hello $user_name!" exit 0 else echo "You did not tell me your name!" exit 1 fi

Command Line Arguments $0 the name of the script $1 the first argument $2 the second argument $# - the number of arguments excluding $0 $* - all arguments excluding $0

Command Line Arguments #!/bin/bash function show_usage { echo "Usage: $0 source_dir dest_dir" exit 1 } # Main program starts here if [ $# -ne 2 ]; then show_usage else # There are two arguments if [ -d $1 ]; then source_dir=$1 else echo 'Invalid source directory' show_usage fi if [ -d $2 ]; then dest_dir=$2 else echo 'Invalid destination directory' show_usage fi fi printf "Source directory is ${source_dir}\n" printf "Destination directory is ${dest_dir}\n"

Functions function function_name { } Function arguments $0 the script name $1 the first function argument $# the number of function arguments Revised version function show_usage { echo "Usage: $0 source_dir dest_dir" if [ $# -eq 0 ]; then exit 99 # Exit with arbitrary nonzero return code else exit $1 fi } n this version of the routine, the argument is optional. Within

Variable and Scope Variables are global within a script Functions can create their own local variables With a local declaration #!/bin/bash function localizer { echo "==> In function localizer, a starts as '$a'" local a echo "==> After local declaration, a is '$a'" a="localizer version" echo "==> Leaving localizer, a is '$a'" } a="test" echo "Before calling localizer, a is '$a'" localizer echo "After calling localizer, a is '$a'"

If-Statement Syntax if [ condition ] ; then statement1 fi if [ condition ] ; then statement1 elif [ condition ] ; then statement2 else statement3 fi

If-Statement Nested if statement if [ condition ]; then if [ condition ]; then statement1 elif statement2 else statement3 fi else statement4 fi

If-Statement Comparison operators for condition x = y è x eq y x!= y è x ne y x < y è x lt y x > y è x gt y x <= y è x le y x >= y è x ge y x is not null è n x x is null è -z x

If-Statement File evaluation operators for condition file exists è -e file exists and a directory è -d file exists and a regular file è -f file exists and not empty è -s file has readable permission for you è -r file has writable permission for you è -w file1 is newer than file2 è file1 nt file2 file1 is older than file2 è file1 ot file2

If-statement Combining conditions And Or n [condition1] && [condition2] n [condition1] [condition2]

Case statement Syntax case expression in pattern1) statement ;& // Fall through pattern2) statement statement ;; *) statement ;; esac

Case statement Example case $# in 0) echo No arguments ;& 1) echo Insufficient arguments ;; *) do_operation ;; esac

Examples Write an log message printing function, logmsg This function gets two parameters, log level and log message There are five log levels depending on the importance n Level 0: Error n Level 1: Warning n Level 2: Info n Level 3: Debug n Level 4: Other The function prints log message in the following format Info: Configuration file not found There is a global variable PRINT_LEVEL that determines the highest level that will be printed