Basic Shell Scripting - Part 1 Objective - Learn to: Read Start-up Files Edit Start-up Files Modify Your User Environment Communicate with Users Write.

Slides:



Advertisements
Similar presentations
A Guide to Unix Using Linux Fourth Edition
Advertisements

PacNOG 6: Nadi, Fiji Using Commands in Linux Hervey Allen Network Startup Resource Center.
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
Linux+ Guide to Linux Certification, Second Edition
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
UNIX chapter 04 UNIX Shells Mr. Mohammad Smirat. Introduction The shell is the software that listens to commands typed in at the terminal and translates.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
Linux+ Guide to Linux Certification, Second Edition
CS 497C – Introduction to UNIX Lecture 36: - Customizing the Environment Chin-Chih Chang
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Guide to Linux Installation and Administration, 2e1 Chapter 6 Using the Shell and Text Files.
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
Welcome to CSE  Name: Di Cao   Classroom: DL357  Class Time: T 8:30am - 9:18am  Office.
MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.
Chapter 9 Part II Linux Command Line Access to Linux Authenticated login using a Linux account is required to access a Linux system. The Linux prompt will.
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.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
An Introduction to UNIX System --- Cosc513 Presentation n Instructor: Morteza Anvari n Author: Yonghong Pan n ID#: n Date: Jan.29, 2001.
An Introduction to Unix Shell Scripting
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 8 Shell.
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.
Shell Features CSCI N321 – System and Network Administration Copyright © 2000, 2005 by Scott Orr and the Trustees of Indiana University.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
Software I: Utilities and Internals Lecture 2 – The vi Text Editor * Modified from Dr. Robert Siegfried original presentation.
Linux+ Guide to Linux Certification, Third Edition
Workbook 6 – Part 1 The Bash Shell
UNIX/LINUX Shells Shell is an UNIX/LINUX command interpreter. Shell command can be internal or external. The code to execute an internal command is part.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
Linux+ Guide to Linux Certification, Third Edition
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
Workbook 6 – Part 2 The Bash Shell
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
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.
UNIX shell environments CS 2204 Class meeting 6 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
Using Linux Commands Lab 4. Using the Shell in Linux Commands Syntax  Options: could be added to the commands to change their behavior (-a, -la, --help)
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
1 Day 18 Bash and the.files. 2 The.files ls shows you the files in your directory –Or at least most of them. –Some files are hidden. Try: ls –a –This.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 2a – A Unix Command Sampler (Courtesy of David Notkin, CSE 303)
Environment After log in into the system, a copy of the shell is given to the user Shell maintains an environment which is distinct from one user to another.
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1.
Module 1 - Introduction to Linux. Users must log-in Linux is case sensitive File and Directories naming conventions (No spaces!) Files and Directories.
Chapter 8: The Bourne Again Shell It’s a command interpreter, it’s a programming language, and it makes a mean martini.
Jozef Goetz, expanded by Jozef Goetz, 2006 Credits: Parts of the slides are based on slides created by textbook authors, Syed M. Sarwar, Robert.
Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems.
Lecture 02 File and File system. Topics Describe the layout of a Linux file system Display and set paths Describe the most important files, including.
Linux+ Guide to Linux Certification, Second Edition
Configuration your environment Many user-configurable Unix programs (such as your shell) read configuration files when they start up. These configuration.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
Linux Administration Working with the BASH Shell.
Agenda Customizing a Unix/Linux account Environment Introduction to Start-up Files (.bash_profile,.bashrc,.profile,.kshrc) Safe Methods for Changing Start-up.
ULI101 Week 10. Lesson Overview ● Shell Start-up and Configuration Files ● Shell History ● Alias Statement ● Shell Variables ● Introduction to Shell Scripting.
SUSE Linux Enterprise Desktop Administration
Introduction to Shells
Shell Features CSCI N321 – System and Network Administration
UNIX and Shell Programming (06CS36)
The Linux Command Line Chapter 11
Linux Shell Script Programming
Module 4 Command Line Skills
Introduction to Bash Programming, part 3
The Linux Command Line Chapter 11
Presentation transcript:

Basic Shell Scripting - Part 1 Objective - Learn to: Read Start-up Files Edit Start-up Files Modify Your User Environment Communicate with Users Write Basic Shell Scripts Create and Use Variables

Start-up Files /etc/profile –belongs to root.bash_profile –belongs to user.bashrc –belongs to user.bash_logout (executed when you logout)

/etc/profile This file can only be modified by “root” Affects the environment of all Users Determines the History Size of all Users Users can change their environment by modifying the.bash_profile or the.bashrc files

.bashrc File.bash_profile File They are located in the user’s home directory These files are executed every time a user logs in. By modifying either one of these files, each user can change his individual working environment Can change your : prompt, screen display, create local variables, create new linux commands (aliases), map new keys on the key board, etc

The Shell Interpreter The Shell interprets every command line If it encounters any meta-characters such as *, ?, $ (there are many more), it will interpret the meta-character and in its place substitute whatever the meaning of that character is.

The Shell Interpreter - Continued e.g. Files in your current directory are: file1 file2 myfile yourfile You enter on the command line: ls file? The Shell interprets the command and substitutes each argument that contains the meta-character: ls file1 file2 This is the actual command that will be executed.

Shell Variables Primarily used for data storage Are always located in memory (fast access) 2 types of variables: –global/environment variables (permanent) –local/user defined variables (temporary) To view the contents of all variables, execute the command: set|more

Global Variables Global variables are assigned to each user as they login. Some Global Variables: HOME=user’s home directory HOSTNAME=matrix PS1=Prompt USER=user_nameHISTSIZE=1000 PATH=/dirX:/dirY:. TERM=vt220 SHELL=/bin/bashPWD=current_dir

Local Variables Created by the user, they vanish upon user logout. Created by a script, they vanish when the script ends. They can be used anywhere on the command line

Assigning Values to Variables Creating/Assigning a value to a variable: variable=value e.g. var=“Seneca College” Displaying the contents of a variable: echo $var The shell recognizes the $ as the variable name meta character, it will interpret the command and substitute the variable name with its contents, thus the interpreted command that will be executed is: echo Seneca College

Examples uname=jsmith file=datafile num=10 echo $uname $num $file grep “$uname” /etc/passwd grep “$uname” $file sort -k3 $file sort -k$num $file Write down the interpreted version of each one of these commands.

Exporting Variables uname=jsmith file=datafile num=10 The above created variables are only available to the current running shell. To make them available to any sub-shells spawned by the current running shell, they must be exported: export uname file num

Examples From the command line, execute the following commands in sequence: one=blue two=red echo “$two roses for a $one lady.” sh echo “$two roses for a $one lady.” exit export one two sh echo “$two roses for a $one lady.”

Input Data into Shell Scripts For the purpose of this course, you will only learn one way to input - from the terminal using the read command. The purpose of the read command is to pause the execution of the script until a user enters data from the terminal and presses the key.

Input Data into Shell Scripts  Examples of the read command:  read something  echo $something  read var1 var2  echo $var1  echo $var2 Shell script pauses until user types text (like hello there) and presses key. Text hello there is stored as the variable called something to be used in script Similar to above example, but multiple variables store text separately delimited (separated by) space(s) or tabs. For example, if user types hello there in this example var1 would store hello, var2 would store there

Input Data into Shell Scripts  WARNING: –It is highly recommended to avoid using variable names that are already reserved for other Unix/Linux commands or utilities. –Local variables (user-defined) names can be made up of any letter, the _, or any number, but must never start with a number! –A good suggestion is using the man or which utilities to determine if that name already exists!

alias - Creating New Commands As a user, you can create your own unique commands: alias la=“ls -a” alias dir=ls alias swho=“who |sort” Refrain from using existing command names as aliases, you will subvert the original command.

Exercises Make a backup copy of your.bash_profile file Using vi, open the file.bash_profile At the bottom, insert the following lines: PS1=‘My New Prompt$ ‘ LS_OPTIONS='-N --color=tty -T 0‘ alias la=‘ls -a’ echo -n “The number of users logged on is: “ who|wc -l log off and log back on again Notice any difference?

Exercises - Continued How many users are logged in the same cluster as you? Has your prompt changed? Execute the new command la Execute the command ls -is the output different now? Delete the PS1=‘My New Prompt$ ‘ line from the.bash_profile file. log off and log back on again, notice the old prompt is back If you wish, restore your original.bash_profile file by copying it from the backup you created.