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.

Slides:



Advertisements
Similar presentations
CMPT 471 Networking II Linux Primer 1© Janice Regan, 2013.
Advertisements

Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Process The Process A process is.
Processes and Job Control. Foreground and Background (1)  Unix is a multi-tasking operating system –some of these tasks are being done by other users.
1 Introduction to UNIX Ke Liu
1 The Shell and some useful administrative Unix Commands How Unix works along with some additional, useful administrative Unix commands you might need.
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
Links Learning Objectives: 1. To understand the basics of links & its usage 2. To learn the construction / removal of different types of links 3. To distinguish.
CS 497C – Introduction to UNIX Lecture 26: - The Process Chin-Chih Chang
The UNIX Shell Software Tools. Slide 2 Basic Shell Syntax command [-[options]] [arg] [arg] … l The name of the command is first l Options are normally.
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
Linux+ Guide to Linux Certification, Second Edition
Links Learning Objectives: 1. To understand the basics of links & its usage 2. To learn the construction / removal of different types of links 3. To distinguish.
CS 497C – Introduction to UNIX Lecture 16: - File Attributes Chin-Chih Chang
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Links Software Tools. Lecture 4 / Slide 2 Links l A link is a pointer to a file. l In fact, in UNIX all filenames are just links to a file. Most files.
Introduction to Linux and Shell Scripting Jacob Chan.
Shell Script Examples.
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
1 The Shell and some useful administrative Unix Commands How Unix works along with some additional, useful administrative Unix commands you might need.
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.
1 THE UNIX FILE SYSTEM By Chokechai Chuensukanant ID COSC 513 Operating System.
The file structure and related utilities CS240 Computer Science II.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Writing Shell Scripts ─ part 1 CSE 2031 Fall September 2015.
Lesson 1. PC vs. Multi-user System  Personal Computer – each user gets his/her own processor (or multicore processor).  Multi-user system – The processor,
An Introduction to Unix Shell Scripting
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.
Linux+ Guide to Linux Certification, Second Edition
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
The Shell Chapter 7. Overview The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)
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.
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 Programming Any command or a sequence of UNIX commands stored in a text file is called a shell program. It is common to call this file a command.
CS465 - UNIX The Bourne Shell.
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.
Lesson 9-Setting and Using Permissions. Overview Describing file permissions. Using execute permissions with a file. Changing file permissions using mnemonics.
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.
Lesson 2-Touring Essential Programs. Overview Development of UNIX and Linux. Commands to execute utilities. Communicating instructions to the shell. Navigating.
Linux+ Guide to Linux Certification, Third Edition
Linux+ Guide to Linux Certification, Third Edition
Getting Started UNIX InKwan Yu Topics Unix Commands Unix System calls C function calls.
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.
Linux Commands C151 Multi-User Operating Systems.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Lecture 1: Introduction, Basic UNIX Advanced Programming Techniques.
Agenda Managing Processes (Jobs) Command Grouping Running jobs in background (bg) Bringing jobs to foreground (fg), Background job status (jobs) Suspending.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
Lesson 8-Specifying Instructions to the Shell. Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
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
Introduction to Shells
The UNIX Shell Learning Objectives:
System Programming and administration CS 308
Shell Script Assignment 1.
Basic UNIX OLC Training.
Shell Programming.
Linux Shell Script Programming
Chapter 3 The UNIX Shells
Presentation transcript:

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 Filename of the shell you are running displays. Example, if running the Bourne shell (sh): $ echo $SHELL /bin/sh $

Subshells Subshells are shells created by a shell –Environment variables are passed Times when subshells are used: –Explicit invocation –Shell script execution –Grouped command execution –Background processing

Changing Your Shell (temporarily) Commands for invoking a subshell: sh, ksh, bash, csh, tcsh Changes your shell and shows correct shell prompt. To leave the subshell and return to your default shell: exit Example, if your default is the Korn shell, change to the C shell and then back to the Korn shell: $ csh % exit $

Managing Processes Each command invokes program and runs it - while a program is running it is called a process. The ps command shows your running processes. $ ps PID TTY TIME CMD pts/0 00:00:00 ksh pts/0 00:00:00 ps $ Process information: PID - process ID TTY- associated terminal TIME - CPU time used by the process COMMAND - the name of the command

Managing Processes The –f option shows a full listing. $ ps –f UID PID PPID C STIME TTY TIME CMD smith :24:19 pts/2 0:00 –ksh jones :38:12 pts/0 0:00 –csh $ Additional process information shown: PPID – parent process ID C - % CPU time used in past minute STIME - time process was created

Managing Processes The –e option shows ALL processes running on the system (not just yours). $ ps –e PID TTY TIME CMD 0 ? 0:08 sched 1 ? 0:00 init 2 ? 0:00 pageout 3 ? 0:06 fsflush 484 ? 0:00 ttymon 58 ? 0:00 sysevent 246 ? 0:00 cron : 482 pts/1 0:00 ttymon 719 pts/2 0:00 ksh 747 pts/2 0:00 ps $

A process may be executed in the foreground or the background. Foreground: $ command –shell waits for the command completion –there is no prompt until the process is completed Background: $ command & [1] 276 $ –The command is started, the process id is returned, and the shell is immediately ready to take another command Managing Processes

If a background process will produce output, you must redirect the output to a file, or output will appear on your display. $ sort sorted.names &

Managing Processes By default, all processes are terminated when you log out. To allow a background process continue to run, place the nohup command in front of the background command. Example: $ nohup sort sortednames & $ exit

Managing Processes To suspend a foreground process Z To move a foreground command to the background: Z 1 + [stopped] $ bg To move a background process to the foreground: $ fg [process-id]

Jobs The jobs command tells you what jobs are running: $ cat names | sort > snames Z Suspended $ jobs [1] +Suspended cat names | sort > snames $

Stopping Background Processes kill %[job#] Example: $ kill %1 “%1” means “job #1”. You can also use the PID. kill [process-id] –but the process can refuse to be killed… kill -9 [process-id] –the system kills the process without a question! Example: $ kill –9 1278

Links Revisited ln creates a new link, not a new file. The new link and the original filename are equivalent pointers to the file. The last argument is the link destination: $ ln names lnames jones lnamesnamesletter3 007 Golden Eye Tomorrow Never Dies File Contents

Links A link contains two pieces of information –A name –An inode number An inode number is an index into a system table that has all the information about the file. You can use ls -i to see if two links point to the same inode: $ ls -li total rw-r--r-- 3 jones cs 64 Feb 6 18:36 lnames rw-r--r-- 1 jones cs 34 Feb 4 15:00 letter rw-r--r-- 3 jones cs 64 Feb 4 15:00 names drwxr-xr-x 2 jones cs 512 Feb 4 17:10 secret/

Links When you use rm –only removes a link to the file –when the last link to a file is removed, the file contents are then deleted

Symbolic Links A symbolic link is a pointer to a pathname, not a pointer to the file itself. $ ln -s original target (creates symbolic link) A symbolic link has a different inode than the original. $ ln -s names snames $ ls -li total rw-r--r-- 3 jones cs 64 Feb 6 18:36 lnames rw-r--r-- 1 jones cs 34 Feb 4 15:00 letter rw-r--r-- 3 jones cs 64 Feb 4 15:00 names drwxr-xr-x 2 jones cs 512 Feb 4 17:10 secret/ lrwxrwxrwx 1 jones cs 5 Feb 8 17:09 snames -> names Symbolic links are sometimes called soft links, and “regular” links are sometimes called hard links.

Differences Between Hard and Soft Links (1) You can’t make a hard link to a directory, but you can make a symbolic link to a directory. $ ln secret seclink ln: secret is a directory $ ln -s secret seclink $ ls -li total rw-r--r-- 3 jones cs 64 Feb 6 18:36 lnames rw-r--r-- 1 jones cs 34 Feb 4 15:00 letter rw-r--r-- 3 jones cs 64 Feb 4 15:00 names drwxr-xr-x 2 jones cs 512 Feb 4 17:10 secret/ lrwxrwxrwx 1 jones cs 6 Feb 8 17:21 seclink -> secret/ lrwxrwxrwx 1 jones cs 5 Feb 8 17:09 snames -> names $ cd seclink $ pwd /homes/jbond/secret

Differences Between Hard and Soft Links (2) You can also make symbolic links across file systems (on different drives) $ ln /tmp/ps_data ps_data ln: ps_data is on a different file system $ ln -s /tmp/ps_data ps_data $ ls –li total rw-r--r-- 1 jones cs 154 Feb 4 16:38 letter lrwxrwxrwx 1 jones cs 12 Feb 8 17:39 ps_data->/tmp/ps_data There is no way to tell how many symbolic links there are to a file.

Biggest Difference Between Hard and Soft Links The most important difference between hard and symbolic links occurs when a link is removed. - For a hard link: $ echo 123 > first $ ln first second $ rm first $ cat second 123 $ echo 456 > first $ cat first 456 $ cat second For a symbolic link: $ echo 123 > first $ ln -s first second $ rm first $ cat second cat: cannot open second $ echo 456 > first $ cat first 456 $ cat second 456

Hard Link Summary A hard link is a directory entry which points to the disk space of another file –It is like another name for a file There is only a single copy of the file on disk –A file may have numerous links A link may only be to a file on the same file system Changing either name has no side effects

Symbolic Link Summary A symbolic link is a directory entry which contains the ASCII text string of the pathname of another file or directory –It is like a pointer to the other file Changing the name of the pointed-at file will “break” the link

Shell Scripts A sequence of operations can be scripted (i.e. make to be automatic) by placing the operations in a script file. Shell scripts provide a full featured programming language, with variables, conditional statements, and the ability to execute other programs.

Shell Script Example $ cat myscript # show date date # show who is logged on echo “Currently logged on:” who # display all files in home directory ls ~ $

Running a Shell Script Give the file execute permission: $ ls -l myscript -rw-r--r-- 1 smith345 users 114 May 12 11:28 myscript $ chmod 744 myscript $ ls -l myscript -rwxr--r-- 1 smith345 users 114 May 12 11:28 myscript Execute the script: $ myscript Mon May 12 12:26:44 MDT 2003 Currently logged on: small000 pts/2 May 12 12:24 (pcisys.net) file1 file2 myscript

Running a Shell Script Possible Error Message: If you get a “file not found” error, then your path may not be set up to recognize your current working directory. Modify by: $ PATH=$PATH:.

Here documents Use the “<<” character with the cat command to create “here” documents. Format: cat << [keyword] Mainly used inside scripts to display multiple lines without echo on each.

Here document Example $ cat script2 cat << stop I can’t stop now... Long live Unix! stop date $ script2 I can’t stop now... Long live Unix! Mon May 12 12:26:44 MDT 2003 $

Exit Codes Every Unix process sends an exit code back to the shell when it terminates –An exit code of 0 indicates success. –Variable $? holds the exit code of the last command. Example: $ ls f* file1 file2 $ echo $? 0 $ ls x* ls: x*: No such file or directory $ echo $? 1 $

Exit Codes Your scripts should return exit codes. Format: exit [NUMBER] For a successful exit: exit 0