UNIX Shell Scripting > Echo “A quick how-to”. What is shell scripting? Interpreted (non-compiled) language Slower compared to compiled languages Much.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Scalar Variables Start the file with: #! /usr/bin/perl –w No spaces or newlines before the the #! “#!” is sometimes called a “shebang”. It is a signal.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Lab 8 Shell Script Reference:
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Scripting CBIS BASH Scripting Step 1 – Create the bash file. Usually a good idea to end it in.sh file1.sh Step 2 – Using CHMOD make the bash file.
Introduction to Shell Script Programming
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Chapter 6: Shell Programming
GNU Compiler Collection (GCC) and GNU C compiler (gcc) tools used to compile programs in Linux.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Linux Operations and Administration
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 11: Shell.
Shell Scripting AFNOG IX Rabat, Morocco May 2008.
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
Writing Scripts Hadi Otrok COEN 346.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Flow Control. The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Shell Scripting September 27, 2004 Class Meeting 6, Part II * Notes adapted by Lenwood Heath from previous work by other members of the CS faculty at Virginia.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Linux Administration Working with the BASH Shell.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
ULI101 Week 10. Lesson Overview ● Shell Start-up and Configuration Files ● Shell History ● Alias Statement ● Shell Variables ● Introduction to Shell Scripting.
Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu.
COMP075 OS2 Bash Scripting. Scripting? BASH provides access to OS functions, like any OS shell Also like any OS shell, BASH includes the ability to write.
CIRC Winter Boot Camp 2017 Baowei Liu
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Chapter 9 Shell Programming
CSC 352– Unix Programming, Fall 2012
Shell Scripting March 1st, 2004 Class Meeting 7.
Variables, Expressions, and IO
What is Bash Shell Scripting?
Pepper (Help from Dr. Robert Siegfried)
An Introduction to Java – Part I, language basics
Presented by, Mr. Satish Pise
Chapter 5 The Bourne Shell
CSC 352– Unix Programming, Fall, 2011
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Basic shell scripting CS 2204 Class meeting 7
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

UNIX Shell Scripting > Echo “A quick how-to”

What is shell scripting? Interpreted (non-compiled) language Slower compared to compiled languages Much more portable than compiled language though This presentation uses the bash scripting language

Configuring for execution In order to execute a shell script in a UNIX environment, you have to configure the file’s permissions with the “chmod” command > Chmod 700 file1.csh The script must be executed from the folder which it resides in.

Shortcut alias You can set a script to be run from any folder by setting an alias for the file. This can be done by editing the user file which contains aliases, for example,.bashrc alias run=‘/home/user/file1.csh’

Hello World #!/bin/bash echo “Hello World” The #!/bin/bash line must be on the first line of all bash scripts - it indicates where the compiler for the script is located. echo is the shell command to print out a line > hello.csh > Hello World >

All for one, and quotes around them all In order to include whitespace in variables, and in several other cases, statements must be surrounded by quotation marks. There are three different types: “ ” (double quotes) - basic type, enclosed variables are substituted with their values ‘ ’ (single quotes) - same as doubles, except no variable substitution ` ` (back ticks) - used for evaluating enclosed commands

Variability Variables are defined as follows: variable_name=value There must be no whitespace between the operands and the operator (x=y) Unlike most compiled languages, bash scripts do not have different variable types - so no declaring variables of type int, char, etc. x=5

Echoing variables Except at execution, when referring to variables, they must be prefixed with a $ Depending on which type of quotation mark you use to enclose an echo command’s statement, enclosed variables may be substituted, as in the following example, where x = 12. INPUT echo “variable x = $x” echo ‘variable x = $x’ OUTPUT > variable x = 12 > variable x = $x

Control Structures if - if (statement) is true, then do one set of instructions, otherwise, do a different one while - while (statement) is true, do a set of instructions. Also, there are the “for”, “case”, and “until” structures, but we will not go over these two.

If (wishes = fishes) This structure basically goes: if (x)…then (y)…else (z)…fi There needn’t necessarily be an else, but if you use an “if” statement, you must conclude with a “fi” statement.

If…then…else…fi In the following code segment, $from_address is a variable which contains the name of the sender of an . … if [ “$from_address” = “Wendy” ] then echo “You have mail from Wendy” else echo “Wendy has not sent you any mail” fi …

While - the loopiest of the loops The while control structure basically goes: While (x)…then (y)…done “While” indicates the beginning of the loop and tests the condition. “Then” is executed as long as “While” is true. “Done” indicates the end of the loop segment.

While…do…done The following is a short while loop that counts to 3 #!/bin/bash number=1 echo -n "Testing..." while [ "$number" -lt 4 ]; do echo -n "$number..." number=`expr $number + 1` done echo "" > test.csh > Testing…1…2…3… >

Test conditions Below are several different tests that can be done on numbers or strings - the left box is what we’re used to, and the right box is the bash script code. x = y x != y x > y x < y x -eq y x -ne y x -gt y x -lt y Testing Numbers: x = y x != y -n x -z x x is the same as y x is not the same as y x is not null x is null Testing Strings:

Readin’, ‘Riting, and ‘Rithmatic There are two ways in a bash script to do simple arithmatic (add [+], subtract [-], multiply [*], divide [/], and modulus [%]) The first way is using the expr command: x=$(expr 3 + 4) -or- x=`expr 3 + 4` The second is using double parenthesis: x=$((3 + 4)) Note that both ways deal only with integers - neither handles deciman values.

Please enter your name: In some cases, you might need to prompt for user input - this is done with the “read” command. Format: “read variable_name” #!/bin/bash echo “What is your name?” read name echo “Hello $name.”

References _to_bash_Shell_Scripting.htmlhttp:// _to_bash_Shell_Scripting.html rial_id=468http:// rial_id=468