UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts Scheduling Unix jobs Job Management.

Slides:



Advertisements
Similar presentations
CS 497C – Introduction to UNIX Lecture 26: - The Process Chin-Chih Chang
Advertisements

CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Introducing the Command Line CMSC 121 Introduction to UNIX Much of the material in these slides was taken from Dan Hood’s CMSC 121 Lecture Notes.
Linux+ Guide to Linux Certification, Second Edition
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
CS 497C – Introduction to UNIX Lecture 34: - Shell Programming Chin-Chih Chang
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.
T UTORIAL OF U NIX C OMMAND & SHELL SCRIPT S 5027 Professor: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2015.
UNIX. find command ● The find command is used to locate files on a Unix or Linux system. find will search any set of directories you specify for files.
Unix Filters Text processing utilities. Filters Filter commands – Unix commands that serve dual purposes: –standalone –used with other commands and pipes.
Using Linux Commands 2 Lab#5
Shell Script Examples.
7/17/2009 rwjBROOKDALE COMMUNITY COLLEGE1 Unix Comp-145 C HAPTER 2.
Linux Commands LINUX COMMANDS.
Using Linux Commands 2 Lab#5. Sort command Sort the lines of text files. $ sort fileName by default it will sort in normal order(alphabetical 0-9 A-Z.
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,
UNIX Processes. The UNIX Process A process is an instance of a program in execution. Created by another parent process as its child. One process can be.
UNIX command line. In this module you will learn: What is the computer shell What is the command line interface (or Terminal) What is the filesystem tree.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Introduction to Shell Script Programming
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 Nine Advanced Shell Scripting1 System Programming Advanced Shell Scripting.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Unix Basics Chapter 4.
Computer Programming for Biologists Oct 30 th – Dec 11 th, 2014 Karsten Hokamp  Fill out.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
UNIX and Shell Programming (06CS36) Unit 1 Continued… Shrinivas R. Mangalwede Department of Computer Science and Engineering K.L.S. Gogte Institute of.
Linux+ Guide to Linux Certification, Third Edition
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
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 Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Shell Scripting AFNOG IX Rabat, Morocco May 2008.
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
Chapter 2: Linux & POSIX “She sells bash shells by the C shore”
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.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 2a – A Unix Command Sampler (Courtesy of David Notkin, CSE 303)
Linux Commands C151 Multi-User Operating Systems.
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
Review Please turn in your homework and practicals Packages, installation, rpm command Apache – Quick and easy way to set up a web server to play around.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
Linux Tutorial Lesson Two *Getting Help in Linux *Data movement and manipulation *Relative and Absolute path *Processes Note: see chapter 1,2,3 from Linux.
Tutorial of Unix Command & shell scriptS 5027
Quality Thought Technologies
Prepared by: Eng. Maryam Adel Abdel-Hady
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
Linux 101 Training Module Linux Basics.
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
The UNIX Shell Learning Objectives:
C151 Multi-User Operating Systems
Shell Script Assignment 1.
CSE 374 Programming Concepts & Tools
INTRODUCTION TO UNIX: The Shell Command Interface
Tutorial of Unix Command & shell scriptS 5027
Basic UNIX OLC Training.
Introduction to UNIX.
Tutorial of Unix Command & shell scriptS 5027
Tutorial of Unix Command & shell scriptS 5027
Tutorial Unix Command & Makefile CIS 5027
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
Periodic Processes Chapter 9.
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Presentation transcript:

UNIX-21 WEEK 2 4/5/2005

UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts Scheduling Unix jobs Job Management

UNIX-23 Sample Files Download states.txt and statecap.txt from acelinux.tripod.com

UNIX-24 cut Used to extract fields from a line By defaults, fields are delimited by tab Syntax: cut –d -f e.g. cut –d ‘:’ –f1 /etc/passwd displays 1 st column of file /etc/passwd cut –d ‘:’ –f2 /etc/passwd displays 2 nd column of file /etc/passwd

UNIX-25 cut (contd.) more /etc/passwd cut –d’:’ -f1 /etc/passwd > usernames more usernames cut –d’:’ –f1-3 /etc/passwd cut –d’ ‘ –f2 statecap.txt cut –d’ ‘ –f2 statecap.txt > capcities

UNIX-26 Examples of cut

UNIX-27 Exercise Write a shell script that displays the current date and time on separate lines `

UNIX-28 dt=`date` day=`echo $dt | cut –d’ ‘ –f1` echo Day is $day mnth=`echo $dt | cut –d’ ‘ –f2` echo Month is $mnth dte=`echo $dt | cut –d’ ‘ –f3` echo date is $dte tme=`echo $dt | cut –d’ ‘ –f4` echo time is $tme yr=`echo $dt | cut –d’ ‘ –f6’ echo year is $yr

UNIX-29 day=`date | cut –d’ ‘ –f1` echo Day is $day mnth=`date | cut –d’ ‘ –f2` echo Month is $mnth dte=`date | cut –d’ ‘ –f3` echo date is $dte tme=`date | cut –d’ ‘ –f4` echo time is $tme yr=`date | cut –d’ ‘ –f6’ echo year is $yr

UNIX-210 paste Command to combine files. By default, adds a space between fields e.g. paste /etc/passwd /etc/passwd paste –d “X” /etc/passwd /etc/passwd

UNIX-211 paste - examples more states.txt paste states.txt states.txt > 2states.txt more 2states.txt paste states.txt /etc/passwd > junk more junk

UNIX-212 wc wc – word count Displays the number of lines, words and characters in the file(s) Syntax: wc filename e.g wc states.txt

UNIX-213 wc Options wc –l states.txt displays # of lines wc -m /etc/passwd displays # of characters wc –w states.txt displays # of words

UNIX-214 grep bash /etc/passwd | wc –l # displays # of users with bash as default shell grep csh /etc/passwd | wc –l # displays # of users with csh as default shell

UNIX-215 export Used to make variables available to all child shell processes By default, shell variables are available only the shell that they are defined in To make shell variables available in child shell processes, they have to be “exported”

UNIX-216 mainscript A in childscript is echo A in childscript is $A A=apple # define a variable A childscript # call another script. This script # executes in a child shell process mainscript childscript

UNIX-217 mainscript A in childscript is apple echo A in childscript is $A export A=apple # define a variable A childscript # call another script. This script # executes in a child shell process mainscript childscript

UNIX-218 source,. (dot command) source,. are used to execute scripts in the same shell. (dot) is more portable than source i.e. available on almost all versions of all shells Can be used from command line or from inside a script

UNIX-219. (cmd-line) When invoked on command line, executes the script. myscript Executes myscript in the current shell

UNIX-220 county=fairfax myscript. myscript or source myscript echo $county fairfax echo $county

UNIX-221 export city=centreville myscript2./myscript2 echo $city

UNIX-222 echo Setting county variable county=fairfax myscript. myscript or source myscript echo $county fairfax echo $county Setting county variable

UNIX-223. (within script) When used within a script,. executes the called script in the same shell as the main script Same effect as if “sourced” script was imported into the main script

UNIX-224 echo Setting county variable county=montgomery myscript echo executing myscript myscript echo Value of county is $county mymainscript executing myscript Setting county variable Value of county is

UNIX-225 echo Setting county variable county=montgomery myscript echo executing myscript. myscript echo Value of county is $county mymainscript executing myscript Setting county variable Value of county is montgomery

UNIX-226 Importing functions Functions can be imported into a script using the. command Once imported, functions can be used in script as if they were defined in the script. Need to import the complete function file Functions are NOT executed when imported – only when “called”

UNIX-227 sum_fn() { let result=$1+$2 return $result } diff_fn() { let result=$1-$2 return $result } funfile

UNIX-228. funfile sum_fn $1 $2 echo sum of $1 and $2 is $? diff_fn $1 $2 echo difference of $1 and $2 is $? mathscript mathscript sum of 10 and 30 is 40 difference of 10 and 30 is -20

UNIX-229 Exercise Rewrite the calculator script by writing all the functions in a single file and “importing” that file into the main script

UNIX-230 pushd, popd, dirs Set of commands to “bookmark” working directories Traverse directories in order First In Last Out pushd dir-name pushes dir-name to directory stack and changes current directory to dir-name popdremoves top directory in stack and changes current dir to the dir popped from stack

UNIX-231 pushd, popd, dirs dirs lists contents of directory stack pushd, popd commands also list contents of directory stack Used in scripts that require changes to current-working directory without hard- coding directory name

UNIX-232 pushd - examples pushd /tmp pwd pushd /usr pwd dirs pushd /bin pwd dirs

UNIX-233 popd - examples pwd popd dirs pwd popd pwd dirs popd pwd

UNIX-234 pushd, popd in scripts # script pushd somedirectory # we are now in somedirectory … popd # back in original directory …

UNIX-235 Shell Debugging No debugger nor any debugging commands/constructs Cryptic error messages Use bash options for primitive debugging

UNIX-236 Bash debugging options -v echoes each command before executing it (before evaluating) -n checks for syntax errors without actually executing the script (weak check) -x print each command before executing (after evaluating)

UNIX-237 Debugging method-1 Add option to bash definition in 1 st line of script #!/bin/bash –v …

UNIX-238 #!/bin/bash –x echo Enter password read pass if [ “$pass” != “magic” ] then echo Wrong password else echo Correct password fi

UNIX-239 #!/bin/bash –v echo Enter password read pass if [ “$pass” != “magic” ] then echo Wrong password else echo Correct password fi

UNIX-240 #!/bin/bash –n echo Enter password read pass if[ “$pass” != “magic” ] then echo Wrong password else echo Correct password fi

UNIX-241 Debugging Method - 2 Execute script in cmd-line bash with required option Don’t need to hard-code option inside script $ bash –v myscript

UNIX-242 #!/bin/bash echo Enter password read pass if [ “$pass” != “magic” ] then echo Wrong password else echo Correct password fi $ bash –v myscript myscript

UNIX-243 #!/bin/bash echo Enter password read pass if [ “$pass” != “magic” ] then echo Wrong password else echo Correct password fi $ bash –x myscript myscript

UNIX-244 #!/bin/bash echo Enter password read pass if[ “$pass” != “magic” ] then echo Wrong password else echo Correct password fi $ bash –n myscript myscript

UNIX-245 Downloading bash 3.0 Create a temporary directory /tmp/bash mkdir /tmp/bash Go to: File: bash-3.0-tar.gz Download file to /tmp/bash

UNIX-246 Downloading bash-3.0 (contd.) cd /tmp/bash cd to directory gunzip bash-3.0.tar.gz Unzip file tar –xvf bash-3.0.tar Un-tar the file cd bash-3.0./configure Run configure script make Run make utility to Build bash binary ls –l bash Verify bash was built successfully cp bash ~ Copy bash binary to home dir

UNIX-247 read (re-visited v3.0+) read command can be used to read multiple variables -s do not echo input -n N accept only N chars of input -p string echo string as prompt before reading input

UNIX-248 read –s –n 6 –p “Enter password” pass accepts 6 char input, does not echo keystrokes and prompt is “Enter password” read –p “Enter 2 numbers” x y accepts 2 values into x and y. If less values are passed, remaining variables are not set

UNIX-249 #!/home/acestudent7/bash read –s –n 6 –p “Enter password” pass

UNIX-250 Exercise Write a script that prompts the user to create a new 6 character password. The user must be prompted to reconfirm the new password. A success message must be displayed if the user re-enters the password correctly else a failure message must be displayed. The password must not be echoed on the screen.

UNIX-251 Enter 6 char password Re-enter password Password entered successfully Enter 6 char password Re-enter password Passwords do not match Sample execution of script

UNIX-252 dt=`date` day=`echo $dt | cut –d’ ‘ –f1` mnth=`echo $dt | cut –d’ ‘ –f2` dte=`echo $dt | cut –d’ ‘ –f3` tme=`echo $dt | cut –d’ ‘ –f4` yr=`echo $dt | cut –d’ ‘ –f6’ bkupdir=/tmp/backup_$day_$month_$tme_yr mkdir $bkupdir cp –R ~ $bkupdir

UNIX-253 Exercise Write a shell script that does the following: Creates a directory called backup_ in the /tmp/directory Copies all files and directories in your home directory to the new directory

UNIX-254 Scheduling Jobs - cron cron – utility used to execute commands at specified times/dates Output/error of commands is ed to user Commands are executed with privileges of user who setup the cron-job

UNIX-255 crontab Command to schedule jobs Each input line corresponds to a job Each input line must contain 6 space separated fields 1-5 date/time 6 – command to be executed

UNIX-256 crontab options -l list user’s crontab tasks -r remove all of user’s crontab tasks -e edit user’s crontab file

UNIX-257 crontab time/date fields 1.Minute (0 – 59) 2.Hour (0 – 23) 3.Day-of-month (1 – 31) 4.Month-of-year (1 – 12) 5.Day-of-week (0 – 6) (0=Sunday) *  all valid values Comma-separated values Specify range using hyphen 6. Command to be executed

UNIX-258 crontab - examples * * 1,2,3,4,5 script1 arg1 5am M-F * * 5 somescript 6:45am every Fri * * myscript 1am 1 st of every month

UNIX-259 crontab – setting up jobs crontab * * * * * /home/acestudent7/mybackupscript Ctrl-d

UNIX-260 crontab – setting up jobs (contd.) crontab – eallows to setup jobs in a editor EDITOR variable must be set to a valid editor export EDITOR=gedit

UNIX-261 crontab – viewing jobs crontab –l displays current job list

UNIX-262 jobs List jobs running in the background sleep 1000 & jobs kill %1 stops the first job kill 1932stops job with id 1932

UNIX-263 wait Stops script execution until all jobs running in background have terminated Or until job with number specified terminates … wait 200 # wait till job with process id 200 terminates …

UNIX-264 $! Process id of last back-ground process find / -name core –print 2> /tmp/err > out & pid=$! … wait $pid mail user < out

UNIX-265 Next Week Application Development Procedures Compilers and GNU development tools Version control demo