Introduction to UNIX Shell Exercises

Similar documents
What is the Shell. Whenever you login to a Unix system you are placed in a program called the shell. All of your work is done within the shell.

Linux Command Line Interface. December 27, 2017

Introduction to Linux

5/20/2007. Touring Essential Programs

Lab 2: Linux/Unix shell

Introduction to Linux Organizing Files

CST Algonquin College 2

Unix Processes. What is a Process?

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

The UNIX Shells. Computer Center, CS, NCTU. How shell works. Unix shells. Fetch command Analyze Execute

bash, part 3 Chris GauthierDickey

Shells. A shell is a command line interpreter that is the interface between the user and the OS. The shell:

Exploring UNIX: Session 5 (optional)

5/8/2012. Specifying Instructions to the Shell Chapter 8

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

S E C T I O N O V E R V I E W

Standard. Shells. tcsh. A shell script is a file that contains shell commands that perform a useful function. It is also known as shell program.

Introduction to the Linux Command Line January Presentation Topics

CSE 390a Lecture 2. Exploring Shell Commands, Streams, Redirection, and Processes

Introduction: What is Unix?

Shells and Shell Programming

M2PGER FORTRAN programming. General introduction. Virginie DURAND and Jean VIRIEUX 10/13/2013 M2PGER - ALGORITHME SCIENTIFIQUE

Crash Course in Unix. For more info check out the Unix man pages -orhttp:// -or- Unix in a Nutshell (an O Reilly book).

Review of Fundamentals

UNIX COMMANDS AND SHELLS. UNIX Programming 2015 Fall by Euiseong Seo

CSE 390a Lecture 3. bash shell continued: processes; multi-user systems; remote login; editors

commands exercises Linux System Administration and IP Services AfNOG 2015 Linux Commands # Notes

Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12)

unix intro Documentation

UNIX Quick Reference

Command-line interpreters

The Unix Shell & Shell Scripts

Shells and Shell Programming

Sperimentazioni I LINUX commands tutorial - Part II

Mills HPC Tutorial Series. Linux Basics I

Assignment clarifications

Processes. Shell Commands. a Command Line Interface accepts typed (textual) inputs and provides textual outputs. Synonyms:

Lecture-4. Introduction to Unix: More Commands, Boot-up Actions and X Window

Linux Tutorial #6. -rw-r csce_user csce_user 20 Jan 4 09:15 list1.txt -rw-r csce_user csce_user 26 Jan 4 09:16 list2.

Chap2: Operating-System Structures

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

CSE 391 Lecture 3. bash shell continued: processes; multi-user systems; remote login; editors

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Unix tutorial. Thanks to Michael Wood-Vasey (UPitt) and Beth Willman (Haverford) for providing Unix tutorials on which this is based.

CSC UNIX System, Spring 2015

Useful Unix Commands Cheat Sheet

Self-test Linux/UNIX fundamentals

The Online Unix Manual

Introduction to Linux Workshop 1

3/8/2017. Unix/Linux Introduction. In this part, we introduce. What does an OS do? Examples

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Computer Systems and Architecture

ACS Unix (Winter Term, ) Page 21

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

CS370 Operating Systems

Perl and R Scripting for Biologists

Unix Tutorial Haverford Astronomy 2014/2015

Introduction to Linux

ACS Unix (Winter Term, ) Page 92

Appendix B WORKSHOP. SYS-ED/ Computer Education Techniques, Inc.

Introduction To. Barry Grant

System Programming. Unix Shells

I/O and Shell Scripting

27-Sep CSCI 2132 Software Development Lab 4: Exploring bash and C Compilation. Faculty of Computer Science, Dalhousie University

Introduction to Unix The Windows User perspective. Wes Frisby Kyle Horne Todd Johansen

Processes and Shells

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 2

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

S E C T I O N O V E R V I E W

System Administration

Introduction to UNIX. Introduction EECS l UNIX is an operating system (OS). l Our goals:

UNIX Tutorial Five

EECS2301. Lab 1 Winter 2016

Environment Variables

5/8/2012. Exploring Utilities Chapter 5

Chapter-3. Introduction to Unix: Fundamental Commands

Sub-Topic 1: Quoting. Topic 2: More Shell Skills. Sub-Topic 2: Shell Variables. Referring to Shell Variables: More

Linux Command Line Primer. By: Scott Marshall

Part 1: Basic Commands/U3li3es

Using UNIX. -rwxr--r-- 1 root sys Sep 5 14:15 good_program

CS 307: UNIX PROGRAMMING ENVIRONMENT REVIEW FOR EXAM 2

UNIX Quick Reference

Getting started with Hugs on Linux

Getting started with Hugs on Linux

File system Security (Access Rights)

First of all, these notes will cover only a small subset of the available commands and utilities, and will cover most of those in a shallow fashion.

Linux shell scripting Getting started *

Command Line Interface The basics

CSCI 2132 Software Development. Lecture 3: Unix Shells and Other Basic Concepts

Processes. System tasks Campus-Booster ID : **XXXXX. Copyright SUPINFO. All rights reserved

Chapter 9. Shell and Kernel

Unix Shells and Other Basic Concepts

UNIX. The Very 10 Short Howto for beginners. Soon-Hyung Yook. March 27, Soon-Hyung Yook UNIX March 27, / 29

Mills HPC Tutorial Series. Linux Basics II

Lab Week02 - Part I. Shell Commands. Professional Training Academy Linux Series

UNIX Shell Programming

Unix Basics. Systems Programming Concepts

UNIX shell scripting

Transcription:

Introduction to UNIX Shell Exercises Determining Your Shell Open a new window or use an existing window for this exercise. Observe your shell prompt - is it a $ or %? What does this tell you? Find out which shell you are using with the command: echo $SHELL Find out which shells are available on your system: which csh which tcsh which ksh which bh which bash Try invoking other shells and notice how the prompt changes. Then list your processes to see them sh csh ksh bash ps Return to your original shell by ing consecutively from each of the shells you started above. List your processes to verify that you have only your original (tcsh) shell running: ps 7. Change to csh for the rest of this exercise tcsh 8. Set up a history for later on set history = 150 1

Processes Review the man page for the ps command man ps Try using the ps command to list your processes. Then try to list other processes on the system. For example: ps ps uc ps ug ps aux Review the man page for the kill command man kill Start a process which does nothing except sleep for 5000 seconds sleep 5000 Now try issuing some other commands, for example ls. What happens? Why won't the shell accept any commands? To kill a process which is running in the foreground, CTRL-C can usually be used. Kill the sleep 5000 process you just started in this manner. What happens after you kill the process? Try another UNIX command, such as ls. Does it work now? Start another sleep 5000 process, except this time, start it in the background: sleep 5000 & Now try issuing some other commands, for example ls. What happens? 7. Use the jobs command to find out about jobs you have running in the background. What does the output of the jobs command tell you? Start a couple more sleep 5000 processes in the background and then use the jobs command again. Do they all appear? 8. 9. 10. Bring the third sleep process into the foreground with the command fg %3 What happens? Can you run anything else now? Why? Suspend the foreground sleep process with the CTRL-Z command. What happens? Try the jobs command now. What does it show? A suspended process can be put in either the background or foreground. Use the bg %3 2

command to resume running the third sleep process in the background. 1 1 The kill command can be used in various ways. Kill the third sleep process with the command kill %3 and then use the jobs command to check on it. What do you see? Now use the ps command to view your processes. Do you see the remaining two sleep processes? Notice their PIDs and then use the kill command to kill both of them. For example: % ps PID TTY TIME CMD 15493 pts/0 0:00 sleep 1000 18054 pts/0 0:00 sleep 1000 18309 pts/0 0:00 -tcsh 27017 pts/0 0:00 ps % kill 15493 18054 % ps PID TTY TIME CMD 15498 pts/0 0:00 ps 18309 pts/0 0:00 -tcsh [3] + Terminated sleep 1000 [2] + Terminated sleep 1000 Redirection Redirect the output of the man command into a file instead of to your screen with the command: man cp > cp.manpage List your directory to confirm that the command worked. View the file with the more cp.manpage command also. Redirect the output of the ls -l command to a file and then view the file after the command completes: ls -l ~/* > homedir.files Redirect both the stdout and stderr from a command to a file. For example, type the command ls * nosuchfile and notice that both the command output and an error message are displayed on your screen. Then, type the command below to redirect both to a file. View the file after the command completes. ls * nosuchfile >& ls.out-err Redirection will overwrite existing files. To prove it, try the following sequence of commands: echo 'one' > file1 echo 'two' > file1 3

You can append redirected output to an existing file if you use >> instead of >. Try to commands below to prove it: echo 'one' > file1 echo 'two' >> file1 Pipes and Filters Obtain a sorted list of users on the system by piping the output of the who command to the input of the sort command: who sort View all system processes one screen at a time by piping the output of the ps aux command to the input of the more command: ps aux more View all $USER processes one screen at a time by piping the output of the ps aux command to the input of the grep command: ps aux grep $USER View all root processes one screen at a time by piping the output of three different commands together. Note that in this example, the grep command is acting as a filter: ps aux grep root more Features (csh) Command History: At the start you invoked the TC Shell's command history feature. To view your command history simply type the command history. What do you see? You may wish to view your command history in reverse chronological order (most recent commands at beginning of list), one screen at a time: history -r more Tcsh allows you to "cycle through" your command history by using the up arrow key. Try pressing your up arrow key at the Shell prompt. Try it several times. Your previous commands should appear, allowing you to edit them and/or press return to reexecute them. Event Reexecution: Try reexecuting several events from your history list. An example is 4

shown below - note that your actual command history will differ: % history -r more 36 12:34 cat sorted.list 35 12:34 sort < unsorted > sorted.list 34 12:34 ls -l > unsorted 33 12:32 sort < ls * > sorted.list 32 12:23 ps ug grep studnt 31 12:22 ps ug grep root more 30 12:20 ps ug more 29 12:18 ps aux 28 12:16 ls %!36 Now try reexecuting an event by using the!string method. For example, the command!his will reexecute the last command you issued which began with the string "his" (such as a history command). Finally, try reexcuting a previous command by typing!! Modifying Previous Events: Type history -r > unsorted. Then, type a command with an obvious typo: sort unsrted grep Systems After the shell tells you it can't open the file "unsrted", modify the command as shown below. The command should now work. ^srt^sort Using the Tcsh, you could also accomplish this by recalling the command with the up arrow key and then editing the command line. Using Aliases: Create a few aliases and then use them: alias h "history -r more" h alias ll "ls -l" ll alias rm "rm -i" rm unsorted Issue the alias command without any arguments. It should show you all of your defined aliases. 7. Filename Generation: You have been using this feature all along. For example: ls /Applications/* ls /Applications/i* ls /Applications/[NS]* You can turn off filename generation with the command set noglob. Do this and then try the same commands above. When you're convinced that it is turned off, turn it back on again with the command unset noglob. 5

8. Filename Completion: Turn on this feature with the command set filec. Then, create a file with a long name: touch Introduction.UNIX.Filesystems Now, type the following and notice what happens. Note that you should use the actual Tab key instead of the literal words "Tab key". Also note that some systems use the Esc key instead of the Tab key. ls IntTab key Variables Display your shell's variables with the set command. Review the list. Then, use the echo command to display a few of them individually. For example: echo $user echo $home echo $term The PATH(path) variable is a very important variable. It determines the directory paths which the shell will search when looking for an executable. Find out what your path is: echo $path echo $path fold - use this if it "runs off" the right hand side of the screen Display your shell's environment variables with the setenv command. Review the list. Then, use the echo command to display a few of them individually. For example: echo $HOST echo $MANPATH echo $HOME Try using variables with UNIX commands. For example: ls -l $HOME echo My userid is $user and my machine is $HOST Set a few variables of your own creation and then display/use them. For example: set myvar1 = "this is a string variable" echo $myvar1 setenv dir1 /usr/local/bin ls $dir1 set colors=(red green blue) 6

echo $colors[1] echo $colors[2] echo $colors[3] echo $colors @ num1 = 256 @ num2 = 512 @ num3 = ($num1 + $num2) echo $num3 Unset any of the variables you previously set and then try to display them. What happens? For example: unset colors echo $colors unsetenv dir1 echo $dir1 Initialization Files Change to your home directory and list all of your files including your hidden files. Which initialization files mentioned in the tutorial do you see? cd ls -a Use either vi or pico to edit your.cshrc file. Notice the variables and aliases which have already been set for you. Customize the file, perhaps creating some variables or aliases of your own. You may get an idea or two from the example.cshrc file in the tutorial. Use the command source.cshrc to effect the changes you made. Try them out...do they work? 7