Introduction to Linux and Shell Scripting Jacob Chan.

Slides:



Advertisements
Similar presentations
Shell Script Assignment 1.
Advertisements

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
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
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
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.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
7/17/2009 rwjBROOKDALE COMMUNITY COLLEGE1 Unix Comp-145 C HAPTER 2.
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
Introduction to UNIX/Linux Exercises Dan Stanzione.
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.
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.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
PROGRAMMING PROJECT POLICIES AND UNIX INTRO Sal LaMarca CSCI 1302, Fall 2009.
System Administration Introduction to Unix Session 2 – Fri 02 Nov 2007 Reference:  chapter 1, The Unix Programming Environment, Kernighan & Pike, ISBN.
INTRODUCTION TO LINUX Jacob Chan. GNU/Linux Consists of Linux kernel, GNU utilities, and open source and commercial applications Works like Unix –Multi-user.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
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.
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.
Lesson 9-Setting and Using Permissions. Overview Describing file permissions. Using execute permissions with a file. Changing file permissions using mnemonics.
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.
INFO 320 Server Technology I Week 5 Shell environments and scripting 1INFO 320 week 5.
Lecture 24CS311 – Operating Systems 1 1 CS311 – Lecture 24 Outline Final Exam Study Guide Note: These lecture notes are not intended replace your notes.
UNIX An Introduction. Brief History UNIX UNIX Created at Bell Labs, 1969 Created at Bell Labs, 1969 BSD during mid 70s BSD during mid 70s AT&T began offering.
Linux+ Guide to Linux Certification, Third Edition
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Shell Programming.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
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.
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.
Linux+ Guide to Linux Certification, Second Edition
1 CS3695 – Network Vulnerability Assessment & Risk Mitigation – Introduction to Unix & Linux.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
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.
Learning Unix/Linux Based on slides from: Eric Bishop.
Linux Administration Working with the BASH Shell.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Tutorial of Unix Command & shell scriptS 5027
CS306 Lab Workout 2 Fall 2017.
Chapter 11 Command-Line Master Class
Prepared by: Eng. Maryam Adel Abdel-Hady
Linux Commands Help HANDS ON TRAINING Author: Muhammad Laique
The UNIX Shell Learning Objectives:
Part 1: Basic Commands/Utilities
C151 Multi-User Operating Systems
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
Tutorial of Unix Command & shell scriptS 5027
UNIX Reference Sheets CSE 2031 Fall 2010.
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Review.
Presentation transcript:

Introduction to Linux and Shell Scripting Jacob Chan

Review on Linux Commands Windows vs Linux –Stability Windows often crashes; Linux rarely does Bug potential –Cost –Learnability –Interface –Market prowess Windows for games, Linux for industry Windows’ customer service features –TIP: DON’T ALWAYS RELY ON THIS

Review on Linux Commands Some important commands in Linux –File commands file (print file type) stat (print more detailed file information) cat (view file contents) touch (create new blank file) gedit (create new blank file, but it is not yet saved –Directory commands mkdir cd ls (argument can be –l, -I, -a)

Review on Linux Commands Some important commands in Linux –System commands exit pwd (print working directory) who (displays currently logged in users) whoami (current user displayed) –Manual pages man (opens manual page of command) info (works like man but has link to other pages)

Review on Linux Commands Some important commands in Linux –File data manipulation wc grep sort head cut –File access chmod umask ln –s

Review on Linux Commands Other commands –Regular Expressions (*, [0-9], ?) –I/O Redirection >

Review on Linux Commands Other commands –Pipes | are used for combining different commands at once –Example: Create a file containing the top 10 foods you like –Then, type this command: cat food.txt | grep “b” | sort What happens?

Review on Linux Commands Running processes in the background –Sometimes, when we run gedit, we can’t type on the command line –Solution: put & –This way, you can work on your text file AND type at the command line at the same time

Review on Linux Commands Processes –There is an ID for every process running –Viewing PID’s ps -ax -Killing processes (terminates the process no matter what) kill -9

Now What? Typing all these commands is a hassle Piping is a solution, but you don’t want to pipe a lot of commands together! This is especially true in handling multiple files at the same time –Database management –Running executables –Backups

A more efficient way: SHELL /bin/bash or /bin/sh (Bourne again shell) Automates processes that are typed over and over Works like.bat files (for Windows users) –But Windows has a tendency to make “weird” changes from those.bat files –In Linux/Unix, you won’t get that Shell files are special text files –Usually ends in.sh –First line: #!/bin/bash Helps in identifying that the file is shell script (comment)

Shell Script Typical content of shell –Text –Write first shell script #!/bin/bash pwd ls –l echo “hello world” touch pogi.txt

Shell Script Running shell –Try typing the shell file first. –Then try typing./before writing the shell file –Reason why latter worked: system issues The shell script is not included in the path variable, so it won’t work by default Same goes with executables Good job on your first shell script!

Shell Programming Variables –Some variables are pre-defined by the system (RESERVED) PATH (list of directories searched when system looks for command) TERM (name of current terminal) HOME (user’s home directory) LOGNAME (user’s login name) SHELL (name of shell program) –How to use variables a =

Shell Programming Variables –Access variable value Use $ or ${} –Access program output Use $(programName) or “ ‘programName’ “ –Displaying value of variables: echo $PATH echo ${PATH} –Variables can either be used as command name or argument cp $file ${file}02 $CC myprog.c -o myprog

Shell Programming Accessing program output example login=$(cat /etc/passwd | grep user| cut -d: -f1) echo ${login} today=`date | cut -d\ -f1-2` echo $today echo ${today} > /tmp/today.txt

Shell Programming Quoting –These characters have special meaning for shell (space, &, $ * “ ‘) –How to go around them: \ cat Your\ Mom Mkdir food\&bar –Single quotes suppresses special meanings Example: help=‘Donuts $35.00 & Brownies $20.00’ –BUT double quotes won’t suppress meaning of $ Example: PATH = “$Path:/home”

Shell Programming Quoting Test –Given that foo = “bar”, what is the result of the following: echo “$foo” echo \$foo echo $’foo’ echo $foo

Shell Programming Conditionals –If Statements if command1 then command2 fi if command1; then command2; fi if command1; then command2 else command3 fi

Shell Programming Conditionals –example of if statement read ans if [ “$ans” = “y” ]; then echo “Your answer is yes” else echo “Your answer is not yes” fi The [] command is like the test command (like a shortcut for checking booleans)

Shell Programming Conditionals –The “normal” if (the one we usually know) if command1; then command2 elif command3; then command4 else command5 fi

Shell Programming Conditionals –The case statement (no switch required) case WORD in (PATTERN) commands ; ; esac

Shell Programming Loops –For loops for in ; do commands done #will check for each item in list #useful for processing each file in directory #list is either a command in Linux, a list (or sequence) of numbers or a plain list #curly braces are optional for list (and are used for numbers only)

Shell Programming Loops –Example of for loop for file in /etc/* do echo “${file}” done

Shell Programming Loops –While loop while [ condition ] do command1 command2 command3 done

Shell Programming Loops –Until loop (works like while except that statement will be executed until condition is TRUE (therefore, it will execute when FALSE) until [ condition ] do command1 command2 command3 done

Shell Programming Reading input –Use either the read command –Example #!/bin/bash echo “what is your name?” read name echo “hello $name”

Shell Programming Scripts are executable files. So to run them, make the file executable. How? chmod +x

PROGRAMMING EXERCISE Try creating a program that displays the date today, and checks if the time now is in the morning, afternoon, or evening. (6am to 12pm is morning, 12pm to 6pm is afternoon, 6pm to 6am is night). Print the appropriate greeting (Good morning, good afternoon, good evening) Clue: find a way on how to print out the date (it was already discussed a while ago)

LAB 1: Printing File Details Create a lab filedetails.sh that describes a file as detailed as possible. The command will be run as./filedetails.sh NAME where NAME is the name of the file we want details of. Wildcards will also be considered, meaning I can type./filedetails.sh * (which prints out details of every file Note: you also need to know how to get file types (if it is a device file, regular file, directory, or a symbolic link

LAB 1: Printing File Details Regular Files (and other file types) File Name: File Type: Owner: Permissions for Owner: Group: Permissions for Group: Permissions for Others: Actual size: Size on Disk: Inode No.: N of Links: Access Date/Time: Modify Date/Time: Change Date/Time:

LAB 1: Printing File Details Device Files File Name: File Type: Owner: Permissions for Owner: Group: Permissions for Group: Permissions for Others: Block or Char? Device Major: Device Minor: Inode No.: N of Links: Access Date/Time: Modify Date/Time: Change Date/Time:

LAB 1: Printing File Details Symbolic Link File Name: File Type: Link to Pathname?

LAB 1: Printing File Details Directories File Name: File Type: Owner: Permissions for Owner: Group: Permissions for Group: Permissions for Others: Inode No.: N of Links: Access Date/Time: Modify Date/Time: Change Date/Time:

LAB 1: Printing File Details Deadline of submission: NEXT WEEK –Links are provided for Sections A and B. Put your filedetails.sh in a folder, along with a CERTIFICATE OF AUTHORSHIP (DO NOT FORGET THIS) Filename: CS162B_ _Lab1_ _.tar Submit on the link provided for each class. Each link I will post a password

NEXT MEETING C Basics More programming and LAB

THE END No dream is too big and no dreamer is too small