Part 1: Basic Commands/Utilities

Slides:



Advertisements
Similar presentations
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.
Advertisements

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).
More Shell Programming Learning Objectives: 1. To learn the usage of environment (shell) variables in shell programming 2. To understand the handling of.
Introduction to Linux and Shell Scripting Jacob Chan.
Shell Script Examples.
7/17/2009 rwjBROOKDALE COMMUNITY COLLEGE1 Unix Comp-145 C HAPTER 2.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
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.
CS 6560 Operating System Design Lecture 3:Tour of GNU/Linux.
The Shell Chapter 7. Overview The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
OPERATING SYSTEMS DESIGN UNIX BASICS & SHELL SCRIPTING.
Keyword Shell Variables The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where.
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)
Linux File System and The Shell Yonglei Tao. Linux File System  Consists of one or more self-contained file management units  each is known as a filesystem.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Lesson 2 1.Commands 2.Filename Substitution 3.I/O Redirection 4.Command Grouping 5.Shell Responisibilites Review of the Basics.
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 Unix Seminar #1 T.J. Borrelli Lecturer for CS and NSSA February 6th, 2009.
Lecture 24CS311 – Operating Systems 1 1 CS311 – Lecture 24 Outline Final Exam Study Guide Note: These lecture notes are not intended replace your notes.
40 Years and Still Rocking the Terminal!
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
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.
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.
Linux Commands C151 Multi-User Operating Systems.
Week Five Agenda Link of the week Review week four lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
Lecture 1: Introduction, Basic UNIX Advanced Programming Techniques.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
Various 2. readonly readonly x=4 x=44 #this will give an error (like what in java?)
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
File Management commands cat Cat command cat cal.txt cat command displays the contents of a file here cal.txt on screen (or standard out).
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.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
IT244 - Introduction to Linux / Unix Instructor: Bo Sheng
Tutorial of Unix Command & shell scriptS 5027
Shell Control Structures
CS306 Lab Workout 2 Fall 2017.
Linux Commands Help HANDS ON TRAINING Author: Muhammad Laique
The UNIX Shell Learning Objectives:
CSC 352– Unix Programming, Fall 2012
CSE 374 Programming Concepts & Tools
Some Linux Commands.
C151 Multi-User Operating Systems
Shell Programming (ch 10)
Shell Script Assignment 1.
CSE 303 Concepts and Tools for Software Development
Tutorial of Unix Command & shell scriptS 5027
Basic UNIX OLC Training.
Introduction to UNIX.
Tutorial of Unix Command & shell scriptS 5027
Pepper (Help from Dr. Robert Siegfried)
CS 60 Discussion Review.
Tutorial of Unix Command & shell scriptS 5027
UNIX Reference Sheets CSE 2031 Fall 2010.
Tutorial Unix Command & Makefile CIS 5027
Linux Shell Script Programming
Shell Control Structures
Module 6 Working with Files and Directories
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Review.
LPI Linux Certification
Presentation transcript:

Part 1: Basic Commands/Utilities

Final Exam May 17th 3:00~4:00pm S-3-143 Same types of questions as in mid-term

Basic Utilities ls, cat, echo ls -l ls -a cat file1 file2 file3 … e.g., regular file or directory, permissions, file size ls -a cat file1 file2 file3 … echo “hello world” > file echo $VAR, echo hello* echo –n “hello world”

Basic Utilities more, less, head, tail pwd, cd head -5 file tail -f file pwd, cd cp, mv, rm, mkdir, rmdir ‘-i’ option ‘-r’ option: rm –r dir1, cp –r dir1 dir2 rmdir an empty directory

Basic Utilities sort, wc, grep, |(pipe) wc -l file, wc –c file grep “keyword” file grep -v sort linux | head -5 command1 | command2 command1 > temp command2 < temp rm temp

Part 2: File Systems

Filename and Path Hierarchical tree structure Absolute path name ls /home/it244/it244 Relative path name cd /home/it244 ls it244 ‘.’ ‘..’ ‘~’ ‘*’ ‘?’ echo hello*, echo ./.?a*

Access Permissions r, w, x (chmod) Execute permission of a directory chmod a+rw file chmod a=rx file chmod 644 file Execute permission of a directory A directory is a table of files (filename:meta) Executable means searchable

Access Permissions Operations on book/book1 ls book/book1 book ( x ) cat book/book1 book1 ( r ), book ( x ) rm book/book1 book ( xw ), book1 ( w )

Links Hard link and soft link Hard link cannot refer to a directory Soft link can point to a nonexistent file $ ln -s hello hello.sl $ rm hello $ echo “new” > hello $ cat hello.sl new

Part 3: Bash/Shell Scripting

Execute Commands Foreground and background jobs Group commands command &, CTRL+Z difference between a suspended job and a background job, PID jobs, ps, kill, bg, fg Group commands (cmd1; cmd2; cmd3)

Redirection Standard input, output, error output cat file1 file2 > out 2>err cat file1 file2 &> out cat file1 file2 > out 2>&1 Redirected output will not be displayed on the screen.

Variables System variables User defined variables $PATH, $PWD, $HOME VAR=hello export VAR=hello declare; declare -x

Variables New shell for executing a script $cat script1 echo $VAR VAR=new $VAR=old;./script1;echo $VAR $VAR=new ./script1;echo $VAR $export VAR=old;./script1;echo $VAR

Expansion/ Quote Types Single quote suspends all expansions echo hello*; echo ~ echo $VAR echo $(pwd) echo $((1+1)) VAR=hello* Single quote suspends all expansions

Control Flow if..then..fi, if..then..else..fi, while test-command do commands done for loop-index in argument-list

Control Flow Conditions test $# -eq 0 [ -f “$1” ] [ condition1 –a condition2 ], -o [[ && ]], (( || )) Exit status $? if [ read <file $line ] …

Expressions Arithmetic Logic a=5;b=3;echo $((a%b)) echo $((a+++3)) cat abc && ls echo $?

Control Flow Boolean operators cond1 cond2 cond1 && cond2 True False false

Control Flow Boolean operators Short circuiting $((a=2, b=2)) $echo $a $b $((--a>2 || b++))

Control Flow Boolean operators Short circuiting (($#==2)) || echo "Usage:require two arguments” mkdir $1 && cp file1 $1

Variables Arguments $$, $? $1-$n, $0, $#, $@ set shift set a 123 hello echo $1 $2 $3 set $(date) shift echo $@ $$, $?

File I/O exec read exec 3 < infile Write to a file exec 4 > outfile read read -p “Enter sth: ” input1 input2 read <&3 line, read –u3 line Determine the end of a file $line has no value ‘read’ returns false Write to a file echo “abc” >&4

Expressions Arithmetic Logic a=5;b=3;echo $((a%b)) echo $((a+++3)) cat abc && ls echo $?

Part 4: Perl/AWK/SED

Run a Program Perl AWK SED perl simple.pl perl –e ‘print “hi\n”’ gawk -f simple file gawk ‘/chevy/ {print}’ file SED sed -f simple file sed ‘/line/ p’ file Stand-alone scripts #!/usr/bin/perl #!/usr/bin/gawk -f #!/bin/sed -f

Patterns AWK SED awk ‘/chevy/ {print}’ cars gawk ‘$1 ~ /h/’ cars gawk ‘$5 > 2000 && $5<6000’ cars SED sed -n ‘3,6 p’ lines sed –n ‘s/line/sentence/w temp’ lines

File I/O in perl Read Write open ($infile, '<', “filename”); while ($line=<$infile>){ print $line; } $line=<> Write $filename="./log"; open($outfile,'>',$filename); print $outfile "Hello world!\n";

Questions If we execute the following sequence of commands in order, what are the values of variables a, b and the output AFTER executing each line? $((a=1, b=2)) $echo $((a-- + b++)) a=_0_, b=_3_, output=_3_ $echo $((a++ + ++b)) a=_1_, b=_4_, output=_4_

Questions Read the following shell script and explain its function. $ cat script1 #!/bin/bash count=0 for i in * then ((count++)) fi echo $count

Questions Read the following shell script and answer the questions $cat script1 #!/bin/bash exec 3<$1 exec 4<$2 exec 5>$3 while read line <&3 do echo “$line” >&5 done while read line <&4 Q1. The script requires 3 filenames as arguments, e.g., ./script1 file1 file2 file3 Which files are input files and which are output files? Q2. Inside the while-loop, why variable “$line” has to be enclosed by double quotes?