CSCI6303 – Principles of I.T. Fall 2012.  Student will become familiar with scripting in shell using Linux/Ubuntu  Student will write a script and execute.

Slides:



Advertisements
Similar presentations
Shell Script Assignment 1.
Advertisements

Lab6 – Debug Assembly Language Lab
1 The Shell and some useful administrative Unix Commands How Unix works along with some additional, useful administrative Unix commands you might need.
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
CS 497C – Introduction to UNIX Lecture 32: - Shell Programming Chin-Chih Chang
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Linux+ Guide to Linux Certification, Second Edition
Information Networking Security and Assurance Lab National Chung Cheng University 1 What Linux is? Free Unix Like Open Source Network operating system.
More Shell Basics CS465 - Unix. Unix shells User’s default shell - specified in /etc/passwd file To show which shell you are currently using: $ echo $SHELL.
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Introduction to Linux and Shell Scripting Jacob Chan.
Linux Shell. 2 Linux Command-Line Interface ■ Linux shells: A shell is a command interpreter that allows you to type commands from the keyboard to interact.
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.
TOPIC 5.0 LINUX SHELLS.
1 The Shell and some useful administrative Unix Commands How Unix works along with some additional, useful administrative Unix commands you might need.
Using the “CLI” Unix / Linux Preparation Course May 25 Djibouti.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Linux environment ● Graphical interface – X-window + window manager ● Text interface – terminal + shell.
Introduction to Shell Script Programming
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
An Introduction to UNIX System --- Cosc513 Presentation n Instructor: Morteza Anvari n Author: Yonghong Pan n ID#: n Date: Jan.29, 2001.
Chapter 6: Shell Programming
CENT 305 Information Systems Security Linux Introduction.
An Introduction to Unix Shell Scripting
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
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+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
Shell Scripting AFNOG IX Rabat, Morocco May 2008.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Agenda Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Review next lab assignments Break Out Problems.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Unix/Linux cs3353. The Shell The shell is a program that acts as the interface between the user and the kernel. –The shell is fully programmable and will.
Week 8: Linux / ubuntu Dr. I. H. Shah. / CSCS 301 Fall 2009.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Introduction to Programming Using C An Introduction to Operating Systems.
Chapter Six Introduction to Shell Script Programming.
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Unix Advanced Shells Chapter 10. Unix Shells u Command Line Interpreter –once logged in, login gives control to a shell –it prompts for input, then parses,
Batch Files Weaker form of UNIX shell scripts Copyright © by Curt Hill.
Linux+ Guide to Linux Certification, Second Edition
1 CS3695 – Network Vulnerability Assessment & Risk Mitigation – Introduction to Unix & Linux.
Lesson 8-Specifying Instructions to the Shell. Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Learning Unix/Linux Based on slides from: Eric Bishop.
Linux Administration Working with the BASH Shell.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Development Environment
System Programming and administration CS 308
CAP652 Lecture - Shell Programming
The Linux Operating System
Shell Script Assignment 1.
Tutorial of shell script
An Introduction to UNIX System --- Cosc513 Presentation
John Carelli, Instructor Kutztown University
Shell Programming.
Linux Shell Script Programming
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Basic shell scripting CS 2204 Class meeting 7
Presentation transcript:

CSCI6303 – Principles of I.T. Fall 2012

 Student will become familiar with scripting in shell using Linux/Ubuntu  Student will write a script and execute it.  Student must take screen shots and submit their code through blackboard

1. Write a shell script that says “Hello World”, using example 1. (remember to include quotations) 2. Modify the script in the last example in the tutorial which is the number guessing game, but instead have the user only guess from 1-25 instead of Make sure to take screens shots and include your code for submission through black board.

1. Start up the virtual machine that was previously installed 2. Start up Ubuntu/Linux. Use the password from lab 3-virtual machines 1. Password = utpa Open up terminal by hitting the Meta(Windows key) to bring up the Dash 4. Type terminal and you should see the icon. Double click it

 In the terminal window type  sudo apt-get install vim  You should be prompted to enter the password next  It should install packages and ask you to accept. Y/N…type Y and let it finish  Once installed you are ready to begin shell scripting.

 First we will create a new folder directory by typing the following  mkdir shell-scripts  (mkdir is short for make directory) shell- scripts will be the folder we create to put all of our scripts in.  Second we will need to change directory(cd) to our shell-script folder by typing.  cd shell-scripts

 Now you will be in the shell-scripts directory. It should be displayed like so  :~shell-scripts$  Now we will open the VIM editor and create a new script file by typing  vim test1.sh Test1.sh is our new file where the script is saved  You should now be in the vim editor. In order to write you must hit the INSERT key on the keyboard

 Now type in the following  #!/bin/sh (  always start scripts with this)  Hit enter after typing the above command  Now type  clear (then hit enter)  Next type  echo “Hello, $USER.” (all caps on user)  Now hit the Esc key once to exit INSERT mode in VIM  Now we need to write, save and then quit. We do this by typing the following  :wq

 In order to execute the script we need to give it executable permission by typing the following  chmod +x test1.sh  In order to run it, all we need to do is type the following ./test1.sh  If done correctly it should say Hello, and your name after it or in this case csci6303.

 Use the previous example (slide 7) to create a new script called guessgame.sh, you should already be in the shell-script directory.  Don’t forget to hit the INSERT key so that you can type in the VIM editor.  Don’t forget to also start the script with the most important piece of code. (slide 8)

 Type the following code and modify for question two. Don’t forget to take screens shots and also to include your modified code to turn in. (must type exactly and also must have proper spacing and indention or it will not run)

 Define the role and features of the Linux shell  Use the shell as a command interpreter  Create user-defined variables  Use shell environment variables  Create shell scripts in Linux

 A shell:  Is a utility program with the Linux system that serves as an interface between the user and the kernel  Plays an important role of command interpretation for the kernel

 Features of a Shell The shell has the following features: Interactive processing Background processing Input/output redirection Pipes Shell scripts Shell variables Programming language constructs Command history Job control File name completion Command completion

Shell as a Command Interpreter The shell: Reads the command Locates the file in the directories containing utilities Loads the utility into memory Executes the utility

 Shell as a Command Interpreter (Contd.)  The shell creates a child shell for the execution of a utility  The shell requests the kernel for any hardware interaction

 Unix Shells  Some of the popular Unix Shells are:  Bourne Shell: sh is the executable filename for this shell  C Shell: csh is the executable filename for this shell  Korn Shell: The executable filename is ksh  Restricted Shell: Is typically used for guest logins

Linux Shells Some of the popular shells available in Linux are: Bash: Is an acronym for ‘Bourne Again Shell’ and is the default shell for most Linux systems Uses the symbolic link sh Tcsh: Is an acronym for ‘Tom’s C shell’ also known as the TC shell It is an enhancement of the C shell Uses the symbolic link csh Can be executed by using either csh or tcsh at the shell prompt ASH: Is usually suitable on machines that have very limited memory Uses the symbolic link, bsh in Fedora Core 2

 Shell Scripts  Stores a sequence of frequently used Linux commands in a file  Enables the shell to read the file and execute the commands in it  Allows manipulation of variables, flow-of-control and iteration constructs that make programming possible

 The echo Command The echo Command Displays messages on the screen Displays the text, enclosed within double-quotes Puts a newline character at the end of the text by default $ echo "This is an example of the echo command"  This is an example of the echo command $ _

 Executing a Shell Script  A shell script can be executed:  In a new shell by  First granting the execute permission to the specified shell script  Then invoking its name at the $ prompt $ chmod +x filename.sh [Change File Access Permission] $./filename.sh [Execute the shell script]

Creating Variables Variables in shell scripts: are not declared as integers or characters are treated as character strings can be mathematically manipulated do not have to be explicitly declared can be created at any point of time by a simple assignment of value The syntax for creating a variable is: = Variables can be created: In shell scripts: A variable created within a shell script is lost when the script stops executing. At the shell prompt: A variable created at the prompt remains in existence until the shell is terminated.

Referencing Variables  The $ symbol is used to refer to the content of a variable variable1=${variable2}  The braces are essentially used to delimit the variable name.  The command to assign the value of today variable to x variable is: $ x=$today

Reading a Value into a Variable  The read command is used to enter a value from the keyboard into a variable during the execution of a shell script.  The syntax to use the read command is:  $ read  The read command, on execution, waits for the user to enter a value for the variable.  When the user presses key after entering the value, the remaining part of the shell script, if any, is executed.

The if Construct Linux provides the if Construct to perform decision making in shell scripts. The if construct is usually used in conjunction with the test command if then [else ] fi Linux also provides the if...elif construct, the syntax of which is as follows: if condition(s) then command(s) elif condition then command(s) else command(s) fi

The exit Command The exit command is used to stop execution of the shell script and return to the $ prompt based on the result of the test command. The following example of the exit command example, echo "Do you wish to quit?" read ans if [ $ans = "y" ] then exit fi The exit command can also be used in the then part of the if…else construct.

The case…esac Construct The case...esac construct in Linux: Is often used in place of the if construct if a variable is tested against multiple values Evaluates the value of the variable and compares it with each value specified The syntax to use the case... esac construct is: case $variable-name in value1) command. command;; value2) command. command;; *) command;; esac

The while Construct The while Construct in Linux supports iteration in shell scripts The while construct has the following syntax: while do done The while true command, creates an infinite loop. An example of the while construct is: reply=y while test "$reply" != "n" do echo –n "Enter file name?" read fname cat ${fname} echo –n "wish to see more files :" read reply done

The for Construct The for construct takes a list of values as input, and executes the loop for every value in the loop. The for construct has the following syntax: for variable_name in do … done The for construct supports wildcard characters in the list of values such as, *.c An example of the for construct is: for name in Ruby Samuel do echo "${name}" done

Debugging Shell Scripts Linux facilitates debugging of shell scripts by using the following two options: The –v option: Echoes the statements written in a shell script on the terminal, before actually executing them The –x option: Echoes the statements in the script preceded by a + symbol, if the statement has successfully been executed To debug the shell script, you can use the sh command with the –v and –x options at the shell prompt: $ sh –v or $ sh –x

 apt-get Search for and install software packages (Debian/Ubuntu)  bash GNU Bourne-Again SHell  chmod Change access permissions  clear Clear terminal screen  echo Display message on screen  mkdir Create new folder(s)  mount Mount a file system  su Substitute user identity  umount Unmount a device  "#!”, is the name of a program which should be used to interpret the contents of the file.  /bin/sh is to tell the contents that it will be a shell script

 Mike Gomez  Juan Prado  Sonny Kodali