A Gentler Introduction to Unix

A Gentler Introduction to Unix

A Gentler Introduction to Unix Zhizhang Shen ∗ Dept. of Computer Science and Technology Plymouth State University April 21, 2020 Abstract Since this course is to run C/C++ in a Unix like environment, we start with a brief introduction to Unix, based on[1], to learn some of the basic Unix commands, the directory structure of the Unix system, and how to compile and run a C program consisting of either one file, or multiple files. In the rest of this course, we will then proceed to learn now to write programs in C to solve various problems, and then go through the same process as mentioned above to compile, and run, such programs. Later on, we will also look at a variety of Unix system calls, and shell scripts consisting of such calls, and see a comprehensive example as how C is used to write programs that make use of system information as kept by Unix. Contents 1 An introduction 3 1.1 Howtologintothesystem? .. .. .. .. .. ... .. .. .. .. ... .. 3 1.2 Simple Unix commands.............................. 3 2 Unix file system 6 2.1 Whatisinaname?................................ 8 2.2 Workwithfiles .................................. 10 2.3 Workwithdirectories. .. ... .. .. .. .. ... .. .. .. .. ... 14 2.4 Backgroundprocessing . 17 2.5 Miscellaneouscommands . 18 ∗Address correspondence to Dr. Zhizhang Shen, Dept. of Computer Science and Technology, Plymouth State University, Plymouth, NH 03264, USA. Email address: [email protected]. 1 3 A Unix shell 19 3.1 How does a shell work?.............................. 19 3.2 Optionsandargumentsofcommands . 20 3.3 Redirection .................................... 20 3.4 Groupcommands................................. 21 3.5 Pipesandtees................................... 21 3.6 Quotingspecialcharacters . 22 4 The editors 23 5 C programming in Unix 23 5.1 Firstthingfirst .................................. 24 5.2 Areyouboredwithit? .............................. 24 5.3 Speakmathematically .. ... .. .. .. .. ... .. .. .. .. ... .. 25 5.4 Cutitintopieces ................................. 26 6 A debugger 28 6.1 Setbreakpoints.................................. 30 6.2 Howtoproceedfromabreakpoint?. 31 6.3 Checkoutthevalues ............................... 32 6.4 Afinalexample.................................. 33 7 shell scripts 36 7.1 Positionalvariables .. .. ... .. .. .. .. ... .. .. .. .. ... 38 7.2 User-definedvariables. 41 8 Unix system interface 42 8.1 Lookatonecallindetails ... .. .. .. .. ... .. .. .. .. ... 42 8.1.1 Anexample ................................ 42 8.1.2 Anotherexample ............................. 44 8.1.3 Scriptsagain?............................... 45 8.2 Anexample:Listingdirectories . ..... 47 8.2.1 Givenastandardinterface... 48 8.2.2 ... and animplementationofthe interface . ..... 51 8.2.3 Alittlesummary ............................. 53 2 1 An introduction Unix is a popular, and powerful operating system 1, which supports multitasking and time- sharing (Think about how Turing is operating, where multiple users can use it “at the same time”.). It consists of a kernel, a file system, a shell, and various utilities. As the core of the system, the kernel controls everything inside the computer, and manages all kinds of resources, in particular, it implements various queues to support the multitasking ability, which we will further explore in CS 4310 Operating Systems. With the support of the file system, Unix organizes all kinds of data into a bunch of files, categorized into directories (Referred to as folders in Windows platform). The Unix shell interprets user commands and passes their requests to the kernel so that they can be further carried out. A utility is just a useful piece of software that makes the system more user friendly. For example, we can print out a calendar, list all the files out in various forms, find a word in a file, etc.. There are various versions of Unix, such as System V Unix, BSD Unix, and Unix-like systems, such as Linux. Turing is a Linux based system. As mentioned earlier, even Apple iOS is built on top of Unix. Since Unix is a multiuser system, we need a user account/password to get into the system, which is just the usual myPlymouth account credential. 1.1 How to log into the system? We can get into Turing via puTTY, an SSH and telnet client, with the above credential. The puTTY program can be found in all the programming labs in Memorial, and can also be obtained via https://www.putty.org/. If you use a MAC, try https://www.ssh.com/ssh/putty/mac/. Once you start puTTY, you should enter turing.plymouth.edu into the “Host Name” box 2, then click “Open”, as shown in Figure 1. You will then be prompted for your credential. If everything works out, it should look like Figure 2. You are now ready to play with it. , 1.2 Simple Unix commands Once getting into the system, we can immediately try out a few simple things. • Change your password. % passwd Changing password for user zshen. Current passworE: 1234567 1Both Apple iOS and Adnroid are based on Unix. 2You can use turing later on. 3 Figure 1: What does PuTTy look like? Figure 2: How to log into turing? New passworE: 7654321 Re-enter passworE: 7654321 passwE: all authentication tokens updated successfully. % Here, by ‘%’, we mean the command line prompt, or simply prompt. It is often associated with a user path, which we will explain further later on. For example, in my case, the actual prompt looks like the following (Cf. Figure 2): /home/zshen > Question: What is the path in your case? • What is the date today? 4 % date Tue Jan 7 08:06:31 EST 2020 • Who just logged on? % who am i zshen pts/0 2020-01-07 08:06 (vpnuser-1-20.plymouth.edu) • What is the command that you want to use? Here “man” certainly refers to “manual”. % man date DATE(1) UserCommands DATE(1) NAME date - print or set the system date and time ... • I am done. % logout Labwork 1.2: Unix commands are case sensitive. Try out the following commands with turing to see what happens: WHO, CAL 2018, DATE, Who Am I, e.g., % WHO You might also want to use the manual command 3 man to check if they are validate Unix commands, e.g., % man WHO Collect what you have done with the above in a ‘.txt’ file, using, e.g., Notepad++, and send it in as part of your report for this lab 4. (Check out the course page for the whole assignment for Lab 1.) 3Notice that you can exit the manual mode by entering ‘q’, standing for “quit”?. 4An effective and efficient way to send in a lab report for this course is to copy the stuff that you have done in turing, and paste them into a .txt file. Make sure that you will clearly label your solutions to the problems. For example, collect all the stuff you just did for Labwork1.2 after a label “Labwork1.2”. 5 2 Unix file system There is nothing special in terms of the file system, compared with what we have been dealing with every day with the Windows system, except perhaps a folder in Windows is referred to as a directory in Unix. An ordinary file can be either a text file or a binary file. A text file is usually represented as ASCII codes; and a binary file, on the other hand, is composed as a sequence of binary bits, intended for a computer to use. A special file contains information necessary to work with a certain device. There are lots of files kept in a computer. To facilitate a mundane task of looking for a file, a bunch of related files are organized into a directory, and a bunch of related directories are organized into a parent directory, ..., until everything is collected under a root directory. From an operating system’s perspective, a directory is also regarded as a file. Thus, the whole thing is organized as a tree, which you should know very well after taking CS2381 Data Structure and Intermediate Programming. Below shows an example of such a file system, based on a tree. Figure 3: An example of a Unix tree To dig a bit deeply, the very reason to use a tree structure to organize all these directories and files is that, as you should have learned in MA2200 Finite Mathematics, MA2250 Mathematics for Computer Scientists, or MA3200 Discrete mathematics, that, in such a structure, the path between any two nodes, especially that between the root directory and any other directory and/or file, is unique. This simple fact allows us to use the same name to uniquely identify different entities in such a structure, which will be made clear later when we discuss the naming of such entities as directories and files. Another fact is that it is relatively fast for use to look for a specific file in such a tree structure, which will be made clear when you take CS3221 Algorithm Analysis. 6 More specifically, at the top level, a Unix file tree contains the following directories: bin, containing the software for the shell and most of other commands; dev, containing all the special files, especially drivers, associated with various devices; etc, containing various administrative files, such as the list of the authorized users; home, containing the home directories of various users; tmp, containing you-know-what; usr, containing something useful; and var, containing something that varies, e.g., users’ mailboxes. When just logging into the system, you are in your working directory. For example, when I log into the system, I am in my working directory /home/zshen, as shown in Figure 2. You can always find out your current working directory by using the “print working directory” command pwd. % pwd /home/zshen % If you want to have a look at the tree structure rooted at the current working directory, you can use the tree command. For example, a sub-tree rooted at the working directory of counting is shown in Figure 4, where ls lists all the stuff in a working directory. Figure 4: Usage of the tree command Once you know where you are, you can change to any directory you want to work with, by using the “directory change” command, cd.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    53 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us