17
Unix Primer

Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”

Embed Size (px)

Citation preview

Page 1: Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”

Unix Primer

Page 2: Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”
Page 3: Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”

Unix Shell

• The shell is a command programming language that provides an interface to the UNIX operating system.

• The shell is a “regular” program. It displays a prompt, waits for user input, and interprets the command line entered by the user.

• It goes back to the prompt again after interpreting a command line

• There are several different shells available, such as bsh, csh, tcsh, etc.

• We will be using the shell on UCFileSpace (Bourne-again shell or Bash)

Page 4: Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”

Accessing Unix shell in this course

• Go to https://ucfilespace.uc.edu/

• Search for Unix

• A description of accessing the Unix shell is given on Ucfilespace:– On Windows, PuTTY:

https://ucfilespace.uc.edu/wiki/search/109– On Mac, using ssh command:

https://ucfilespace.uc.edu/wiki/search/108

Page 5: Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”

Editors• Vim

For Vim tutorial type on command line

vimtutor

•Emacs For Emacs tutorial, while in Emacs type

<ctrl-h> t• for reference cards on Emacs and Vim

See Supporting Material on CascadeLMS

https://cascade.ceas.uc.edu/

Page 6: Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”

DirectoriesThe shell should start you in your home directory. This is your individual space on the UNIX

system for your files. You can find out the name of your current working directory by typing:

pwd

The system normally will display : /Users/username

Change to a sub-directory “sub” by typing: cd sub

No matter where in the directory structure you are, you can always get back to your home directory by typing:

cd(without specifying a directory name).

From your home directory, create a new subdirectory named "primer" for working through this tutorial:

mkdir primer

You can remove an empty subdirectory with the following command (but don't do this right now): rmdir primer

Now change to the "primer" subdirectory, making it your current working directory: cd primer

Page 7: Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”

Files (1) Since you just created the directory “primer”, nothing will be listed because the directory is empty.

Create your first file called first using the Emacs or Vim text editor to type in text:My name is Joe Doe.This is my first file.

Now when you list your files, you will see file "first" listed: ls

first

You can view a text file with the following command: cat first

My name is Joe Doe. This is my first file.

("cat" is short for concatenate - you can use this to display multiple files together on the screen.) If you have a file that is longer than your 24-line console window, use instead "more" to list one page at a time or "less" to scroll the file down and up with the arrow keys. Don't use these programs to try to display binary (non-text) files on your console - the attempt to print the non-printable control characters might alter your console settings and render the console unusable.

Page 8: Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”

Files (2) Copy file "first" using the following command: cp first 2nd

By doing this you have created a new file named "2nd" which is a duplicate of file "first". The file listing reveals:

ls2nd first

Now rename the file "2nd" to "second": mv 2nd second

Listing the files still shows two files because you haven't created a new file, just changed an existing file's name:

lsfirst second

If you "cat" the second file, you'll see the same sentence as in your first file: cat second

My first file.

"mv" will allow you to move files, not just rename them. Perform the following commands: mkdir submv second subls sub

secondls

first sub

Page 9: Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”

Files (3)You can list even more information about files by using the "-l" option with "ls": ls -l

-rw-r--r-- 1 username group 15 Jan 4 16:26 firstdrwxr-xr-x 2 username group 512 Jan 4 17:11 sub(where "username" will be your username and "group" will be your group name). Among other things, this lists the creation date and time, file access permissions, and file size in bytes. The letter 'd' (the first character on the line) indicates the directory names.

Next perform the following commands: cd subpwd

/Users/username/primer/subls -l

-rw-r--r-- 1 username group 4 Jan 4 16:55 secondcd ..pwd

/Users/username/primer

Finally, clean up the duplicate files by removing the "second" file and the "sub" subdirectory: rm sub/secondrmdir subls -l

-rw-r--r-- 1 username group 15 Jan 4 16:26 first

Page 10: Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”

Online manual

man - command for online manual

Page 11: Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”

Path Namesdir name + file name

– Absolute pathname: starting from root

– E.g.

/user/doej/bin/myProg.cpp

– Relative pathname

myProg.cpp (a file under current dir)

bin/myProg.cpp (a file under subdir “bin” of current dir

– The system maintains a “current working directory”, and will prefix it to a relative pathname

Page 12: Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”

Special Directories

• The first two members of any subdirectories are two special directories “.” and “..”

• “.” : self– Will show how to use it

• “..”: parent dirE.g.: cd ..– Go to the parent directorypwd sub1cd ../sub2

Page 13: Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”

Source Code Naming Convention

.cpp : C++ source code file

Page 14: Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”

How to use GNU C++ compiler/linker g++

g++ myProg.cpp Compiles myProg.cpp, generates an object file,

link the object file (with libraries), and generates an executable file called “a.out”.

g++ myProg.cpp –o myProgSame as above, however the executable has a

name “myProg”

Page 15: Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”

How to execute your program

• Find the executable from the current directory, and run it

• Unix can take any file extension as executable– Does not have to use a “.exe” extension name– As long as the file is marked as executable

For example, using default name a.out for executable, enter on command line:

uc ./a.out

DON’T forget ./ to indicate current directory

Page 16: Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”

Redirection of Input and Output

• < filename take standard input from that filename

• > filename send standard output to filename

• Examples:

ls > myfiles

more < myfiles

cat < myfiles > myfilesoutput

Page 17: Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”

The UNIX pipe

• ls | more

• Same as

ls > temp

more < temp

rm temp