Shell / command interpreter ● interactive user interface with an operating system ● command line interface (CLI) ● understands and executes the commands.

Slides:



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

Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
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
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
Scripting Languages and C-Shell. What is a scripting language ? Script is a sequence of commands written as plain text and run by an interpreter (shell).
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
1 UNIX essentials (hands-on) the directory tree running programs the shell (using the T-shell) → command line processing → special characters → command.
Introduction to Linux and Shell Scripting Jacob Chan.
Shell Programming. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Shell Script Examples.
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.
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.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
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.
Hp education services education.hp.com hp education services education.hp.com 1 HP World/Interex 2002 Linux BASH Shell Programming Chris Cooper (734)
Linux Shell Programming Tutorial 3 ENGR 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
CS 6560 Operating System Design Lecture 3:Tour of GNU/Linux.
1 UNIX essentials (hands-on) the directory tree running programs the shell → command line processing → special characters → command types → shell variables.
System Administration Introduction to Unix Session 2 – Fri 02 Nov 2007 Reference:  chapter 1, The Unix Programming Environment, Kernighan & Pike, ISBN.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
OPERATING SYSTEMS DESIGN UNIX BASICS & SHELL SCRIPTING.
Linux+ Guide to Linux Certification, Third Edition
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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 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.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
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.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Chapter Four I/O Redirection1 System Programming Shell Operators.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
CS252: Systems Programming Ninghui Li Slides by Prof. Gustavo Rodriguez-Rivera Topic 7: Unix Tools and Shell Scripts.
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
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.
UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Linux+ Guide to Linux Certification, Second Edition
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Linux Administration Working with the BASH Shell.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
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.
Some Linux Commands.
Lecture 9 Shell Programming – Command substitution
Shell Script Assignment 1.
CSE 303 Concepts and Tools for Software Development
Pepper (Help from Dr. Robert Siegfried)
LING 408/508: Computational Techniques for Linguists
Linux Shell Script Programming
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
LPI Linux Certification
Presentation transcript:

Shell / command interpreter ● interactive user interface with an operating system ● command line interface (CLI) ● understands and executes the commands a user enters ● outer layer of an operating system (in contrast to the kernel) ● invokes another program, shows the system settings, allows file system modification etc. ● many shells available on a typical Linux/Unix/MacOs systems: sh, bash, zsh, ksh

BASH ● Bourne Again SHell - enhanced version of the original Bourne shell program, sh (written by Steve Bourne) ● on local system: -started after a successful login (if specified in/etc/passwd) -terminal emulators (xterm, gnome-terminal, konsole,... - depends on window manager) ● on remote system : -ssh (Unix, Linux, MacOS,...) -ssh client program e.g. PuTTY

BASH ● interactive shell vanilla:etc root$ ls -al vanilla:etc root$ mkdir backup vanilla:etc root$ mv *.ini backup ● non-interactive shell vanilla:etc root$. script.sh script.s h if [ -d backup ]; then mv *.ini backup else if [ -e backup ]; then echo “Sorry, I can't create \ directory \"backup\" in directory `pwd`” else mkdir backup mv *.ini backup fi

Some linux commands ● grep - search the file or standard input for lines containing a match to the given pattern ● find – find files ● cat - read files sequentially, writing them to the standard output ● wc - print the number of newlines, words, and bytes in files ● ls - list directory contents ● mkdir - create the directories ● rm - remove files and directories (-r) ● cp - copy the contents of the source file to the target file ● mv - rename/move the file ● man - show command manual (man bash, man ls,...) ● whoami - display effective user id (also: id -un or echo "$USER" ) ● who - display a list of all users currently logged on

Redirection ● there are always three default files open: stdin - file descriptor 0 (generally keyboard) stdout - file descriptor 1 (generally screen) stderr - file descriptor 2 (generally screen) ● redirect the standard output to a file ls -alR / > diretorytree.txt ( recursively list all directories in long format starting from /) ● redirect stderr to file cat * 2>errors.txt (writes all errors to the specified file) ● redirect and append stdout to file cat file2.txt>>all.txt ● redirect and append stderr to file cat privatestuff/* 2>>errors.txt

Redirection ● redirect both stdout and stderr to file cp -vrn privatestuff backup &>copy.txt ● redirects stderr to stdout cp -vrn privatestuff backup > copy.txt 2>&1 ● redirect stdin from file wc -l <file.txt writes number of lines in file.txt, the same as: $ wc -l twinkle, twinkle little star ● ^D 2 ● redirect stdin and stdout wc lines.txt

Pipeline ● a set of chained processes, so that the output of each process (stdout) feeds directly as input (stdin) to the next one command1 > file command2 file2 command1|command2|command3 command3 < file2 cat *.txt >file1.tmp sort file2.tmp cat *.txt|sort|uniq uniq <file2.tmp

Variables ● no data types ● can contain a number, a character, a string of characters ● no need to declare a variable, assigning a value to its reference will create it ● value of the variable is retrieved by putting the '$' before name ● dir=”/usr/local/bin” file=”php” echo running $dir/$file... $dir/$file ● capturing a commands output to a variable var=`command` or var=$(command) ● name=`whoami` ● dir=`pwd` ● echo $name is in $dir directory

Environment vs local variables ● global variables or environment variables are available in all shells – use command: $ env SHELL=/bin/bash USER=ania ORACLE_HOME=/Users/oracle/oracle/product/10.2.0/db_1 PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin: /Users/oracle/oracle/product/10.2.0/db_1/bin:/Users/oracle/ oracle/product/10.2.0/db_1/bin PWD=/Users/ania LANG=pl_PL.UTF-8 HOME=/Users/ania … ● local variables are only available in the current shell - use the set command to display a list of all variables (including environment ones)

Variables ● making variable a global one $ export VAR=”value” $ ORACLE_HOME=/home/oracle/db_10; export ORACLE_HOME ● removing variable $ unset VAR ● reading variable from stdin $ read VAR ● reserved BASH variables HOME - current user's home directory IFS - list of characters that separate fields PATH - colon-separated list of directories in which the shell looks for commands PS1the primary prompt string UID- numeric real user ID of the current user RANDOMgenerates random integer between 0 and PWDthe current working directory OSTYPE- string describing the operating system HOSTNAME- name of the current host BASH - full pathname used to execute the current instance of Bash and many more...

Pipe examples ● ls -al|grep ^-|tr -s " " " "|cut -f5,9 -d" "|sort -n (show only regular files in the current directory with their size, sorted by size) ● ls -alt|tr -s " " " "|cut -d" " -f9|head -n2|tail -n1 (write name of the last modified file ) ● echo $USER `finger 2>/dev/null|grep $USER|wc -l` (write number of user sessions) ● cat /etc/passwd|sort -t":" -k3 -n|cut -d":" -f1,3 (show users logins with their uid, sorted by uid) ● cat *.java|sort|uniq|wc -l (write number of different lines in all *.java files)

If … then... else... fi ● if condition; then ● command1 command2 … else command3 command4 … ● fi ● conditions test -f file.txt [ -f file.txt ] ((5==5&&6==6)) true ((5<9)) … … …

test ● man test ● [ -f file.txt ] t rue if file exists and is a regular file ● [ -d file.txt ] t rue if file exists and is a directory ● [ -e file.txt ] t rue if file exists (regardless of type) ● [ -z string ] t rue if the length of string is zero ● [ s1 = s2 ] true if the strings s1 and s2 are identical ● [ s1 != s2 ] true if the strings s1 and s2 are not identical. ● [ s1 < s2 ] true if string s1 comes before s2 based on the ASCII value of their characters ●

test ● [ n1 -eq n2 ] true if the integers n1 and n2 are algebraically equal ● [ n1 -ne n2 ] true if the integers n1 and n2 are not algebraically equal ● [ n1 -ge n2 ] true if the integer n1 is algebraically greater than or equal to the integer n2 ● [ n1 -lt n2 ] true if the integer n1 is algebraically less than the integer n2 ● [ ! expression ] true if expression is false ● [ expression1 -a expression2 ] true if both expression1 and expression2 are true. ● [ expression1 -o expression2 ] true if either expression1 or expression2 are true.

no_backup=0; if test -e backup then echo removing directory backup rm -r backup 2>/dev/null no_backup=$? fi if test $no_backup -eq 0 ; then echo creating directory backup mkdir backup echo copying files cp -v *.ini *.conf backup >summary.txt cat summary.txt echo Files copied: cat summary.txt| wc -l else echo Directory \"backup\" can\'t be removed fi

Special parameters $? expands to the exit status of the most recently executed foreground command expands to the positional parameters. when the expansion occurs within double quotes, each parameter expands to a separate word (similar to $*, but $* is no longer recommended) $#expands to the number of positional parameters in decimal. $$Expands to the process ID of the shell. $0Expands to the name of the shell or shell script. ● Parameters $1, $2, $3, …$# $script.sh Script name : script.sh ● command shift script.sh echo Script name: $0 if [ $# -eq 3 ]; then echo $1 echo $2 echo $3 else echo To few parameters fi

While loop ● while condition; do command1 command2 … done ● show script agruments while [ $# -gt 0 ]; do echo $1 shift done

Arithmetic evaluation ● expression evaluation ((expression)) ((i=i+3)) or i=$((i+3)) ● i=$[i + 3] ● i=`expr $i + 3` ● zm=0 while [ $1 ]; do zm=$[zm+$1] shift done echo Sum: $zm

sum=0 first=true while read line; do if ($first); then first=false max=$line min=$line fi if [ $line -gt $max ]; then max=$line fi if [ $line -lt $min ]; then min=$line fi ((sum=sum+line)) done echo Min: $min Max: $max Sum: $sum Script, which finds sum,minimum and maximum from the numbers on the standard input

For loop (1) ● for variable_name [in list] ; do command1 command2 … done ● for (( expr1 ; expr2 ; expr3 )) ; do command1 command2 … done i=1; for param ; do echo Parameter $i: $param i=$((i+1)) done for user in Mike Ann Max do logged=`who|grep $user` if [ "$logged" ];then echo $user logged in else echo $user is not logged in fi done

For loop (2) ● for filedir in * ; do if [ -d "$filedir" ]; then echo Directory: $filedir elif [ -f "$filedir" ];then echo File $filedir fi done ● for ((a=1;a<=$#;a++)); do echo $a : ${!a} done a: … $# ${!a}: $1 $2 $3 ….${$#}

For loop (3) ● for filedir in `ls` ; do if [ -d "$filedir" ]; then echo Directory: $filedir elif [ -f "$filedir" ];then echo File: $filedir else echo Problem: $filedir fi done ● IFS=$'\n' for filedir in `ls -1` ; do if [ -d "$filedir" ]; then echo Directory: $filedir elif [ -f "$filedir" ];then echo File: $filedir else echo Problem: $filedir fi done Wrong solution for a filenames with spaces: File file1.txt File file2.txt Problem: file Problem: with Problem: spaces.txt Good solution for a filenames with spaces: File file1.txt File file2.txt File: file with spaces.txt

Quotes, double quotes ● used where arguments contains spaces ● rm “big file” = rm 'big file' ● mkdir "max ' has ' a dog" creates max ' has ' a dog mkdir 'max “ has “ a dog' creates max “ has “ a dog ● var=1000 echo $var 1000 echo '$var' $var echo “$var” 1000 ● echo * file1.txt file2.txt file3.txt ….. echo '*' * echo “*” * ● escaping echo “\$var” $ var

Functions ● syntax: function_name () { command1 command2... } ● the exit status of a function is the exit status of the last command executed in the body ● executed in the context of the current shell - no new process is created to interpret them ● the arguments to the function become the positional parameters during its execution ● variables are shared between the function and its caller - variables local to the function may be declared with the local builtin command ● if the return command is executed in a function, the function completes and execution resumes with the next command after the function call function function_name { command1 command2... }

max_in () { ( max=0; maxline=""; while read line; do newmax=`echo $line|wc -m ` if [ $newmax -gt $max ] ; then max=$newmax maxline=$line fi done echo $max ":" $maxline )<$1 } for file do if [ -f $file ] then max_in $file fi done Functions Script finds the longest line in every file provided as a parameter

Factorial ● factorial() { if [ $1 -eq 0 ]; then echo 1 else #or: echo $[$1*$(factorial $[$1-1])] #echo $[$1*`factorial $[$1-1]`]; fi } ● factorial2(){ if [ $1 -eq 0 ]; then result=1 else factorial2 $[$1-1] result=$[result*$1] fi } factorial 6 factorial2 6 echo $result