Outline. Unix shells Bourne-again Shell (bash) Interacting with bash Basic scripting References



From this document you will learn the answers to the following questions:

What is the specific parameter to use in bash?

What is the default shell on modern Linux distributions?

What is bash used for?

Similar documents
Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

BASH Scripting. A bash script may consist of nothing but a series of command lines, e.g. The following helloworld.sh script simply does an echo.

CS Unix Tools & Scripting Lecture 9 Shell Scripting

Lecture 4: Writing shell scripts

Unix Scripts and Job Scheduling

This presentation explains how to monitor memory consumption of DataStage processes during run time.

Shell Scripts (1) For example: #!/bin/sh If they do not, the user's current shell will be used. Any Unix command can go in a shell script

Beginners Shell Scripting for Batch Jobs

Hands-On UNIX Exercise:

A Crash Course on UNIX

Automating admin tasks using shell scripts and cron Vijay Kumar Adhikari.

HP-UX Essentials and Shell Programming Course Summary

Command Line - Part 1

LSN 10 Linux Overview

Running your first Linux Program

CS 2112 Lab: Version Control

Learn Perl by Example - Perl Handbook for Beginners - Basics of Perl Scripting Language

BASH scripting. Sebastian von Alfthan Scripting techniques CSC Tieteen tietotekniikan keskus Oy CSC IT Center for Science Ltd.

Linux command line. An introduction to the Linux command line for genomics. Susan Fairley

UNIX / Linux commands Basic level. Magali COTTEVIEILLE - September 2009

AN INTRODUCTION TO UNIX

A Brief Introduction to the Use of Shell Variables

SSH Connections MACs the MAC XTerm application can be used to create an ssh connection, no utility is needed.

Introduction to Shell Scripting

grep, awk and sed three VERY useful command-line utilities Matt Probert, Uni of York grep = global regular expression print

UNIX, Shell Scripting and Perl Introduction

The Linux Operating System

CS2720 Practical Software Development

Unix the Bare Minimum

A fault tolerant, peer-to-peer replication network

Installing IBM Websphere Application Server 7 and 8 on OS4 Enterprise Linux

Introduction to Running Hadoop on the High Performance Clusters at the Center for Computational Research

PHP Debugging. Draft: March 19, Christopher Vickery

Using SVN to Manage Source RTL

A+ Guide to Managing and Maintaining Your PC, 7e. Chapter 2 Introducing Operating Systems

CLC Server Command Line Tools USER MANUAL

L01 Introduction to the Unix OS

OPERATING SYSTEM SERVICES

Steps for running C-program

MFCF Grad Session 2015

Advanced Bash Scripting. Joshua Malone

PrimeRail Installation Notes Version A June 9,

PKZIP 6.0 Command Line Getting Started Manual

Tour of the Terminal: Using Unix or Mac OS X Command-Line

Cisco Networking Academy Program Curriculum Scope & Sequence. Fundamentals of UNIX version 2.0 (July, 2002)

1001ICT Introduction To Programming Lecture Notes

CONNECTING TO DEPARTMENT OF COMPUTER SCIENCE SERVERS BOTH FROM ON AND OFF CAMPUS USING TUNNELING, PuTTY, AND VNC Client Utilities

Lab 1: Introduction to C, ASCII ART and the Linux Command Line Environment

New Lab Intro to KDE Terminal Konsole

Answers to Even-numbered Exercises

Stata R Release 13 Installation Guide

Unix Sampler. PEOPLE whoami id who

Adafruit's Raspberry Pi Lesson 6. Using SSH

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

Java Programming with SciTE on Linux and Windows

Bash Guide for Beginners

Grandstream Networks, Inc. XML Configuration File Generator User Guide

Basic C Shell. helpdesk@stat.rice.edu. 11th August 2003

Using a login script for deployment of Kaspersky Network Agent to Mac OS X clients

sftp - secure file transfer program - how to transfer files to and from nrs-labs

Universal Mobile Print Server On the Cheap, and Cloud-free. What You Will Need. Configuring your Pi as a Print Server

The Linux Boot Sequence

Introduction to Mac OS X

Install BA Server with Your Own BA Repository

Installing C++ compiler for CSc212 Data Structures

L04 C Shell Scripting - Part 2

Table of Contents. V. UPGRADING LSTC License Manager Software for Microsoft Windows A. Run the Installation Program B. Verify the Installation

Author A.Kishore/Sachin VNC Background

CPSC 226 Lab Nine Fall 2015

Windows PowerShell Essentials

Version Control Using Subversion. Version Control Using Subversion 1 / 27

DS-5 ARM. Using the Debugger. Version 5.7. Copyright 2010, 2011 ARM. All rights reserved. ARM DUI 0446G (ID092311)

Installing OptiRain Open on Macintosh OS X 10.6 (Snow Leopard)

There s a variety of software that can be used, but the approach described here uses freely available Cygwin software: (1) Cygwin/X (2) Cygwin/openssh

Application Notes for MUG Enterprise Call Guard with Avaya Proactive Contact with CTI Issue 1.0

Virtual Integrated Design Getting started with RS232 Hex Com Tool v6.0

VI(Visual) Editor Reference manual

1 Description of The Simpletron

System Administrator s Guide

Introduction to Linux and Cluster Basics for the CCR General Computing Cluster

Getting Started with Mplus Version 7.31 Demo for Mac OS X and Linux

Click Start > Control Panel > System icon to open System Properties dialog box. Click Advanced > Environment Variables.

CPSC2800: Linux Hands-on Lab #3 Explore Linux file system and file security. Project 3-1

File Sharing. Peter Lo. CP582 Peter Lo

DEVICE DRIVERS AND TERRUPTS SERVICE MECHANISM Lesson-14: Device types, Physical and Virtual device functions

Command Line Interface User Guide for Intel Server Management Software

Ayear ago, I wrote an article entitled

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

AES Crypt User Guide

Bash shell programming Part II Control statements

LAE Enterprise Server Installation Guide

Editing Files on Remote File Systems

CNC Transfer. Operating Manual

Using the Radmind Command Line Tools to. Maintain Multiple Mac OS X Machines

IBM Tivoli Monitoring for Databases: Microsoft SQL Server. Reference Guide. Version SC

Eventia Log Parsing Editor 1.0 Administration Guide

Operating System Structure

2015 Exelis Visual Information Solutions, Inc., a subsidiary of Harris Corporation

LECTURE-7. Introduction to DOS. Introduction to UNIX/LINUX OS. Introduction to Windows. Topics:

Transcription:

Ryan Hulguin

Outline Unix shells Bourne-again Shell (bash) Interacting with bash Basic scripting References

Unix shells This lets users issue commands to the Unix operating system Users can interact with various parts of the Unix operating system such as the input/output system the scheduler memory management The shell is simply a program whose job is to execute other programs Different shells exist such as the C shell (csh), the Korn shell (ksh), the Bourne shell (sh) and the Bourne-again shell (bash)

Bourne-again shell (bash) bash is the default shell on modern Linux distributions as well as Mac OS X bash is both a command interpreter and a highlevel programming language When used as a programming language, bash processes commands stored in files called shell scripts Most system shell scripts are written for bash Like other programming languages, bash has variables and control flow commands (i.e. for loops and if statements)

Active participation It is highly encouraged to try all the examples as we go along You may use your own shell from a Linux/Mac terminal You may also use the free linux shell provided by http://simpleshell.com

Shell Variables Variables in a bash script are typically written with all-uppercase letters This is not a strict requirement Assignment is done using the equals sign without spaces VAR1= test string Referencing the value of a variable is done using the dollar sign echo My assigned variable contains $VAR1 An exception to this rule is evaluating arithmetic expressions, covered later

Documenting bash scripts with comments It is always a good idea to document bash scripts with comments Any text immediately following the # character will be considered a comment and ignored by the shell # This is a comment The exception to this the shebang #! found at the top of most scripts The shebang specifies which shell to use to execute the script Example: #!/bin/bash

Command substitution To assign the value of another bash command/ script to a variable, command substitution is used TODAYS_DATE=$(date +%m/%d/%y ) TODAYS_DATE=`date +%m/%d/%y ` echo $TODAYS_DATE echo $(TODAYS_DATE) Newer scripts will use $(), while older scripts will use backticks ` ` The $() convention makes it easier to read and nest commands within commands

Our first bash script Visit http://www.simpleshell.com Click Start my session nano hello_world.sh Ctrl-X Y to save modified buffer Hit return/ enter to accept filename

Running our first bash script We can run our bash script several ways We can pass the script to the shell of our choice /bin/bash./hello_world.sh /bin/sh./hello_world.sh We can specify which shell to use with the shebang, and then make the script executable nano hello_world.sh Insert #!/bin/bash at the top Ctrl x, y, enter to save chmod u+x./hello_world.sh./hello_world

Embedding variable values in text Suppose you have the following variable defined PREFIX=sun If you wanted to echo the following words using this prefix: sunflower, sunshine, sunset you would use echo ${PREFIX}flower echo ${PREFIX}shine echo ${PREFIX}set Note the use of the braces Without the braces, echo $PREFIXflower would return nothing since the variable PREFIXflower is undefined

Using parameters in a bash script scripts are often called using input parameters To reference these parameters use ${position #} where position # is the specific parameter to use that starts at 1 Example: echo The first input parameter is ${1} echo The second input parameter is ${2} You can also get the number of input parameters passed using ${#} echo There were ${#} input parameters passed

Setting default values for parameters Many times you would like parameter set to a default value if none is given Suppose you have a script that is expecting a directory as an input parameter, but needs to default to the $HOME directory if none is specified default_param.sh: #!/bin/bash # List the contents of the 1 st argument # Use the $HOME directory if none is specified TARGET=${1:-$HOME} ls $TARGET

Setting default values for variables Just like parameters, variables can also have default values assigned to them default_variable.sh: #!/bin/bash # Show the first 5 lines of $TARGET file TARGET=/proc/cpuinfo head -5 ${TARGET:=./default_variable.sh} echo TARGET file is $TARGET unset TARGET echo ---------- head -5 ${TARGET:=./default_variable.sh} echo TARGET file is $TARGET

The for control structure for loop-index do commands done The loop-index takes on the values of each of the command line arguments Example: # Display all of the command line arguments for INPUT_ARG do echo $INPUT_ARG done

The for in control structure for loop-index in argument-list do commands done The loop-index takes on the values of each argument in the specified argument-list Example: # Display all of the animals in a given list for ANIMAL in lions tigers bears do echo $ANIMAL done

The for control structure with C like syntax bash also allows a syntax for for loops much like the C programming language # count from 1 to 10 on a single line for (( count=1; count<10; count+=1 )) do echo n $count done echo

References A Practical Guide to Linux Commands, Editors, and Shell Programming by Mark G. Sobell Bash Cookbook: Solutions and Examples for Bash Users by Carl Albing, JP Vossen, and Cameron Newham http://en.wikibooks.org/wiki/bash_shell_scripting These slides will be posted on http://www.nics.tennessee.edu/hpc-seminar-series

Questions? / Contact Ryan Hulguin ryan-hulguin@tennessee.edu