What is Shell Script?

Similar documents
Systems Programming & Scripting

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

CS2720 Practical Software Development

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

Unix Scripts and Job Scheduling

CS Unix Tools & Scripting Lecture 9 Shell Scripting

Introduction to Shell Scripting

Advanced Bash Scripting. Joshua Malone

Lecture 4: Writing shell scripts

Bash shell programming Part II Control statements

Answers to Even-numbered Exercises

Setting Up the Site Licenses

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.

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

1001ICT Introduction To Programming Lecture Notes

A Crash Course on UNIX

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

AN INTRODUCTION TO UNIX

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Hands-On UNIX Exercise:

Introduction to Shell Programming

ESPResSo Summer School 2012

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

Lecture 2 Notes: Flow of Control

Introduction to Python

Command Line Interface Specification Linux Mac

Command Line - Part 1

PHP Tutorial From beginner to master

HP-UX Essentials and Shell Programming Course Summary

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

Informatica e Sistemi in Tempo Reale

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

Introduction to Java

Moving from CS 61A Scheme to CS 61B Java

Thirty Useful Unix Commands

Top 72 Perl Interview Questions and Answers

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Answers to Even- Numbered Exercises

Example of a Java program

Linux Shell Script To Monitor Ftp Server Connection

Computational Mathematics with Python

Ayear ago, I wrote an article entitled

CLC Server Command Line Tools USER MANUAL

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Using SVN to Manage Source RTL

Automated Offsite Backup with rdiff-backup

SWsoft Plesk 8.3 for Linux/Unix Backup and Restore Utilities

32-Bit Workload Automation 5 for Windows on 64-Bit Windows Systems

USEFUL UNIX COMMANDS

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

Computational Mathematics with Python

PHP Debugging. Draft: March 19, Christopher Vickery

Computational Mathematics with Python

Programming Languages CIS 443

SWsoft Plesk 8.2 for Linux/Unix Backup and Restore Utilities. Administrator's Guide

Chapter 2: Elements of Java

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Running your first Linux Program

Database migration using Wizard, Studio and Commander. Based on migration from Oracle to PostgreSQL (Greenplum)

Sources: On the Web: Slides will be available on:

TIP: To access the WinRunner help system at any time, press the F1 key.

All Tech Notes and KBCD documents and software are provided "as is" without warranty of any kind. See the Terms of Use for more information.

COSC282 BIG DATA ANALYTICS FALL 2015 LECTURE 2 - SEP 9

Lab 1 Beginning C Program

4/1/2017. PS. Sequences and Series FROM 9.2 AND 9.3 IN THE BOOK AS WELL AS FROM OTHER SOURCES. TODAY IS NATIONAL MANATEE APPRECIATION DAY

Plesk 8.0 for Linux/UNIX Backup and Restore Utilities

6. Control Structures

Bash Tutorial. Eigil Obrestad and Erik Hjelmås. August 18, 2015

Creating a Java application using Perfect Developer and the Java Develo...

Introduction to Grid Engine

Exercise 1: Python Language Basics

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

Introduction. Chapter 1

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

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

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

CAPIX Job Scheduler User Guide

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

Lecture 22 The Shell and Shell Scripting

Inteset Secure Lockdown ver. 2.0

JAVS Scheduled Publishing. Installation/Configuration... 4 Manual Operation... 6 Automating Scheduled Publishing... 7 Windows XP... 7 Windows 7...

Hadoop Hands-On Exercises

The Linux Operating System

SFTP SHELL SCRIPT USER GUIDE

Oracle SQL*PLUS (For , Fall 2006) Prepared by: Mr. Nkulu Nkasa

The Build Process. of (GNU Tools for ARM Embedded Processors)

Creating a Simple Macro

Tutorial Guide to the IS Unix Service

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

AUTOMATED BACKUPS. There are few things more satisfying than a good backup

Monitoring a Linux Mail Server

Introduction to Object-Oriented Programming

Lecture 18 Regular Expressions

Controlling LifeSize Video Systems from the CLI

MATLAB Programming. Problem 1: Sequential

How To Program In Scheme (Prolog)

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

PMOD Installation on Linux Systems

Transcription:

Linux Shell Scripts

What is Shell Script? We have seen some basic shell commands, it s time to move on to scripts. There are two ways of writing shell programs. You can type a sequence of commands and allow the shell to execute them interactively. You can store those commands in a file that you can then invoke as a program. This is known as Shell Script. We will use bash shell assuming that the shell has been installed as /bin/sh and that it is the default shell for your login.

Why Shell Script? Shell script can take input from user, file and output them on screen. Useful to create own commands. Save lots of time. To automate some task of day today life. System administration part can be also automated.

How to write and execute? Use any editor to write shell script. The extension is.sh. After writing shell script set execute permission for your script. chmod +x script_name Execute your script./script_name

Shell script format Every script starts with the line #!/bin/bash This indicates that the script should be run in the bash shell regardless of which interactive shell the user has chosen. This is very important, since the syntax of different shells can vary greatly. # is used as the comment character. A word beginning with # causes that word and all remaining characters on that line to be ignored.

A sample shell script #!/bin/bash echo "Hello User" echo "See the files in current directory" ls

Variables In Linux (Shell), there are two types of variable: System variables created and maintained by Linux itself. echo $USER echo $PATH User defined variables created and maintained by user. All variables are considered and stored as strings, even when they are assigned numeric values. Variables are case sensitive.

Variables When assigning a value to a variable, just use the name. No spaces on either side of the equals sign. var_name=value Within the shell we can access the contents of a variable by preceding its name with a $. myname=a [ use quotes if the value contains spaces ] myos=linux text = 1+2 echo Your name:$myname [ A ] echo Your os:$myos [ Linux ] echo $text [ 1+2 ]

Variables If you enclose a $variable expression in double quotes, it s replaced with its value when the line is executed. If you enclose it in single quotes, no substitution takes place. You can also remove the special meaning of the $ symbol by prefacing it with a \. myvar= Hello echo $myvar [ Hello ] echo $myvar [ Hello ] echo $myvar [ $myvar ] echo \$myvar [ $myvar ]

Read To read user input from keyboard and store it into a variable use read var1,var2,...varn #!/bin/bash echo n "Enter your name: read name echo n "Enter your student no: read stdno echo "Your Name:$name echo "Your Age:$stdno

Shell Arithmetic The expr command evaluates its arguments as an expression. It is commonly used for simple arithmetic operations. #!/bin/bash expr 1 + 1 expr 1 1 expr 1 \* 1 expr 1 / 1 va r=`expr 1 + 1` x=1 x=`expr $x + 1`

Shell Arithmetic

If Else if [ conditiong1 ]; then statement1 elif [ condition2 ]; then statement2 else statement3 fi It is must to put spaces between the [ braces and the condition being checked. If you prefer putting then on the same line as if, you must add a semicolon to separate the test from the then.

If Else

If Else

If Else #!/bin/bash echo "Enter first number " read num1 echo "Enter second number" read num2 if [ $num1 gt $num2 ] ; then echo "$num1 is greater than $num2" elif [ $num1 lt $num2 ] ; then echo "$num1 is less than $num2" else echo "$num1 and $num2 are equal" fi

Case case $var in condition1) statement ;; condition2) statement ;; *) statement3 esac Notice that each pattern line is terminated with double semicolons ;;. You can put multiple statements between each pattern and the next, so a double semicolon is needed to mark where one statement ends and the next pattern begins.

Case #!/bin/sh echo Is it morning? Please answer yes or no read timeofday case $timeofday in yes) echo Good Morning ;; no ) echo Good Afternoon ;; y ) echo Good Morning ;; n ) echo Good Afternoon ;; * ) echo Sorry, answer not recognized ;; esac

Case #!/bin/sh echo Is it morning? Please answer yes or no read timeofday case $timeofday in yes y Yes YES ) echo Good Morning ;; n* N* ) echo Good Afternoon ;; * ) echo Sorry, answer not recognized ;; esac

Command Line arguments Command line arguments can be passed to the shell scripts. There exists a number of built in variables $* command line arguments $# number of arguments $n nth argument in $*./script_name arg1 arg2... argn

For for variable in list do statement done for (( expr1; expr2; expr3 )) do statement done

For [1] #!/bin/bash echo "the number of args is $#" a=1 for iin $* do echo "The $a No arg is $i" a=`expr $a + 1` done [2] #!/bin/bash for iin `ls` do echo $i done [3] for(( i=0;i<=50;i++)) do echo $i done

While while condition do statements done #!/bin/bash password="abc" echo "Enter password" read pass while [ $pass!= $password ] do echo "Wrong Password,Try again" read pass done echo "Write Password"

Until until condition do statements done #!/bin/bash password="abc" echo "Enter password" read pass until [ $pass = $password ] do echo "Wrong Password,Try again" read pass done echo "Write Password"

Functions Functions can be defined in the shell and it is very useful to structure the code. To define a shell function simply write its name followed by empty parentheses and enclose the statements in braces. function_name () { statements } Function must be defined before one can invoke it.

Functions #!/bin/sh foo() { echo Function foo is executing } echo script starting foo echo script ending output script starting Function foo is executing script ending

Functions When a function is invoked, the parameters to the script [$*, $#, $1, $2] and so on are replaced by the parameters to the function. When the function finishes, they are restored to their previous values. #!/bin/bash showarg() { a=1 for i in $* do echo "The $a No arg is $i" a=`expr $a + 1` done } echo "Listing start" showarg $* echo "Total:$#" echo "Listing End"

Functions Functions can return numeric values using the return command. Functions can also return strings by the following ways. [1] f() { var="123 ; } f echo $var [2] f() { echo "123"; } result="$(f)"

Functions #!/bin/sh yes_or_no() { echo "Is your name $*?" echo "Enter yes or no:" read x case $x in y yes ) return 0;; n no ) return 1;; esac } if yes_or_no $1 then echo Hi $1, nice name else echo Never mind fi

Functions Be careful : Function calling can be recursive. f() { statements f } f The parameter must be passed every time a function is invoked either from main or from any other functions.

Thanks