UNIX/Linux PDF
Document Details
Uploaded by ReputableBluebell8765
Tags
Summary
These notes provide an overview of UNIX/Linux, including its operating system, file systems, commands, and history. The document also covers the history of computers and concepts like recursion.
Full Transcript
UNIX/Linux What is Linux? - Operating system on a computer controlling the hardware, manages all other software running, and manages user interactions (Other systems include Mac OS, Windows, Android, IOS, etc...) - The original user interface was text based (like cmd on wind...
UNIX/Linux What is Linux? - Operating system on a computer controlling the hardware, manages all other software running, and manages user interactions (Other systems include Mac OS, Windows, Android, IOS, etc...) - The original user interface was text based (like cmd on window or terminal on Mac OS) - Linux is a free and open-source software started by Linus Torvalds in 1991 (Since it is open source, there are many different variations) - Linus only built the Linux kernel (core) and others - Linux is a family of Unix like OS with the Linux core File Systems - Structure to organize or store information - A directory is like a cabinet, storing files that can be sorted - E.g. (Files on your computer or google drive) Linux file system - / Is the root (top) directory (cabinet) to store information (biggest cabinet that holds everything in a computer) - Linux root “This PC” (Windows) - (e.g., /home, /bin, etc...) - /bin is for the location of binaries or executable programs Basic Operating System Commands Text based: Linux - pwd print onscreen (path to current working directory) - ls lists the current content of the directory - ls –l lists the current contents of the directory in long format (detailed) - clear: wipes the screen - cd: change directory (equivalent to clicking on a folder or typing the address in the navigation bar) - Cd/homework (brings you to homework directory) - Cd.. (brings you up one level such as going from homework to home) - Cd (brings you back to home directory) - mkdir (creates a new directory with specified name listed after command) - mkdir / fakehomework (creates a new folder called fakehomework) - rmdir (deletes an existing directory with specified name (must be empty)) - rmdir / fakehomework (removes a new folder called fakehomework) - rm –r (REMOVES A DIRECTORY) - Touch: makes a file - Gedit: Open fie for editing using gedit text editor - Nano: Open file for editing using nano text editor - rm – removes a specified file - mv – moves a file or directory from one location or name to another - cp – making a copy of a file Linux/Unix Permissions: D: denotes that it is a directory R: read access (same as view perms on google docs) W: write access (same as edit perms on google docs) X: execute access (can run the contents of a file needed for programs) -(dash): indicates that the attribute not applicable (specify a standard input or a standard output for a command) If directory, it has a “D” instead of a “–” for files Rwx – owner access perms, can do anything (read, write, execute) Rw- can read, write for group projects r—for outside people can only view [u/g/o] refers to user / group (all CPSC undergraduate students) / other (everyone in the world) [r/w/x] refers to read a file / write to a file / execute (a file) or go into (a directory) chmod [u/g/o] [+/-] [r/w/x] [file_name] change permission on file Setting Permission Levels - chmod (command can set permission levels) - e.g., chmod History of Computers Abacus (300-400BC) - Hand-operated calculation tool used in Middle East, Europe, China, and Russia Difference Engine (Charles Babbage) - Automatic mechanical calculator used to calculate polynomial functions (Invented in 1822) - Began the concept of a digital programmable computer - Charles Babbage was a philosopher, mathematician, inventor, and mechanical engineer, termed as the “father of the computer” - 1991, a functional difference engine was constructed Ada Lovelace (1815-1852) - Mathematician and writer, the daughter of lord Byron - Worked alongside Charles Babbage, termed as “the first programmer” - Developed the early philosophy of computer science Alan Turing (1912 - 1954) - Called “the father of computer science and artificial intelligence” - He worked at Bletchley Park to break the Nazi encryption code during the 2 nd world war - He was a mathematician, logician, computer scientist, philosopher, and cryptanalyst - Created: 1. Turing machine 2. Turing test ENIAC (1945) - Stands for Electronic Numerical integrator and Computer - Is the first programmable, electronic digital computer, computed manually by humans - Was 167 square meters (about half the area of a tennis court) - Solved many numerical problems Modern Computers - Integrated Circuits (1959) patented by Robert Noyce. Made of silicon - William Shockley (manage of research group at bell labs), John Bardeen (researcher), Walter Brattain (researcher) were awarded the Nobel prize in physics in 1956 for their research on semi-conductors and discovery of the transistor effect (controlling the flow of current in semi-conductors) - Were considered the founders of silicone valley, inspiring intel, AMD, etc... John Von Neumann - Mathematician and physicist - Modern CPUs are based off his research (Princeton) (1945 Von Neumann Architectures) - Program: Instruction for data, able to process data and execute on a set of instructions - Control units: Track current step of program in memory - Stored-program architecture (Allows the automatic transfer of data between sources) James Gosling – Founder of java programming language - Bachelor of computer science in University of Calgary Computer Science and Problem Solving Bloom’s Taxonomy - Framework for categorizing educational goals Problem solving method (Top-down design to problem solving) 1. Dividing the problem into smaller specific parts 2. Find easily solvable parts and begin breaking the problem down into smaller parts if not understood 3. Solve smaller problems Python - Algorithm: Finite sequence of specific steps to solve a problem - An application of the top-down design to problem solving - Can be expressed in English and translated to any programming language - Programming: Process of creating software with the goal of translating algorithms into computer language (binary, java, python, c++, etc...) Python Interpreter - A python interpreter reads the code you write and runs your code line by line, following your algorithm - You must follow a set of rules varying with each language, called the syntax or it will not run - Every colon, parentheses, semi colon, indentation must be correct What is syntax? - Indentation: python uses 4 spaces to define a block - Data types: variables must be declared with a data type such as int, string, bool, float, etc... - Comments: use “#” to begin comment which will not be run by the python interpreter - Conditional statements: If, Elif, else to set conditions for the block of code to run - Loops: for and while loops to repeat algorithm process until the problem is solved or for a set amount of times - Functions: defined using “def” keyword which are algorithms that can be repeatedly used in different scenarios Comments: - Words used to describe steps in an algorithm for other coders to read - It is not for the computer to execute upon - Begins with “#” in python Python Statements - Instruction that the interpreter can execute - The instruction must be specific and follow syntax Input and output statements - Input(message) - Receives user interaction - Output(message) - Outputs information Variables - Variable is an entity that remembers a value assigned to it - The names of variables are case sensitive - Use meaningful names for variables that you declare so yourself or others can read it - Converting variables to types: - Int () - String () - Float () - Import math to use math library (pi) - Math. Pi Reserved keywords - These keywords cannot be used as a variable name Python Data Types Input - Built in function to receive input from user - name = input (“Please enter your name:”) - The received user input is stored in the variable “name” as a string - Can convert to different data types if necessary - e.g., age = int(age) - Can be done in one line: - age = int (input (“Please enter your age”)) Output - Make sure the output is meaningful, give specific context to user - Print () Type addition - Cannot concatenate distinct types (integer + String) Formatting - Formatting methods: - A string “pi is %.2f” - Format float to 2 decimals: %.2f - Format symbol for string: % - Value to format: Math. Pi - Print(f”String”) - F string allows you to insert a variable inside the string by using {variable} inside the parenthesis. - Types: - F – float - G – Scientific notation - S – String - D – Integer - [width]. [precision][type] Types of Errors 1. Syntax - Issue with structure of program/programming rules - Program will not run at all - E.g.: - print (“abc 2. Logic - Program runs successfully but causes results that were not as intended - E.g.: - while True - (Condition turns out to be always true) - Or - X+Y / 2 instead of (X+Y)/2 3. Runtime - Crashes in the middle of running - E.g.: - numbers = 123 - print (nomber) Python Conditionals - Conditionals are yes/no statements (does the condition meet this requirement...) Operators - == (does it equal?) - < (is it less than?) - (is it greater than?) - >= (is it greater than or equal to?) - ! = (is it does not equal to?) If statement - Conditional statement is evaluated - If true execute block... - If false do not execute block... - Indented block only runs if the if statement is true - Number of indented spaces must be consisted in a block Else statement - If conditional if or Elif statement is false (else) execute this block - Runs after the if and Elif statements are checked and is stated to be false Elif statement - Checked after the if statement if the if statement condition is false - Makes code more efficient and sequential by not making the executor check all if statements if one before is found to fit condition Nesting - Control structure inside another control structure - e.g., if statement inside an if statement. (In this case the inner if statement only runs if the first if statement runs) Boolean Logic - Conditions are questions that are either true or false, no ambiguity - A variable that’s value can only be true or false is a Boolean type - Program can branch into other control structures depending on answer - The branch condition is called a Boolean expression Relation operators - Relation operators: < > == >= 10111111 Binary to Decimal - 1111 base 2 to base 10 - 1+2+4+8=15 Integer Data - 8 bits is referred to as a byte (2^8 -1) - 16 bits is referred to as a half word/short (2^16 -1) - 32 bits is referred to as a word (2^32 -1) - 64 bits is referred to as double word/long (2^64 -1) Negative numbers - A simple idea is called “signed” - Signed Byte - Rightmost digits refer to the magnitude, the 8 th bit refers to the sign - Floating point numbers - Representing real numbers - Standard Representation: IEEE 754 Floating Point - Express the number in scientific notation - 0.0002589 becomes -2.589 * 10 -4 - Need to store sign, exponent, and mantissa (the fraction) - 32-bit floating point representation: - sign (1 bit), exponent (8 bits), mantissa (23 bits) - 64-bits: - sign (1 bit), exponent (11 bits), mantissa (52 bits) How many values can be represented by 32 or 64 bits? 2^32 = 4.2 billion, 2^64 = 1.8 x 1019 Largest values: 2^32 – 1 and 2^64 – 1 What is the problem? Some numbers cannot be represented exactly in a float point such as 2/3 (irrational numbers) Floating points can only approximate these numbers DATA REPRESENTATION - - Download speed is shown as mb/s (megabytes per second) - - Strings Character Data - Standard encoding scheme is called ASCII - American Standard Code for Information Exchange - 7 bits/character 2^7 = 128 characters - Ascii includes - Printable characters - Control characters (tab, newline, data transmission) - Limits of ASCII - Does not support Latin characters - Does not support accents, and additional characters in other languages (Mandarin, Spanish, Danish etc...) UTF – 8 (Uniform Transformation Format) - Variable length: 1,2,3, or 4 bytes per character - Compatible with ASCII, it is another encoding scheme for characters - Loops - Repeatedly performing tasks While Loop - Initialize loop control - Check condition - Keep rerunning while condition is not fulfilled - Divided into two parts 1. Body of the loop (simple or compound statement to be repeated) 2. Condition (Boolean expression, checking if the loop should keep rerunning) - Loop control: - Variable used in the loop conditional statement - Loop control statement is a condition for loop execution - Stops loop when condition is no longer true - while (condition): (body of code) - While loop is used when we do not know how many times, we want to repeat a task. - Sentinel value: value inside the loop that terminates the process (e.g., enter 0 to exit) Palindrome check Word = input (“Input a word, and Ill check if it is a palindrome”) Length = len(Word) Int counter = 0; While counter 1 - print (x ) -> 2 - a,b = x - print (a) -> 1 - print (b) -> 2 Sets - Collection of unordered values - It will only contain unique values - It can be changed - set_test = set () - Only way to define an empty set - Things you cannot do: - Concatenate - Repetition - Why do we use sets? - Since it can only store unique items, its faster - Common in fields of study - Examples are Venn diagrams - Types of notations in Venn Diagrams - A&B, A and B - A | B, A or B - A^B - B–A - Difference between each: Files I/O - Files are a way to store data in a generic format 1. Files are saved on a permanent storage device (DVD, Hard drive, USB drive) - Do not get lost if unplugged 2. Memory is a temporary storage device (RAM) - Are lost if unplugged - Files are stored in folders/directories - e.g., Users\Adam\Downloads\Cs\Assignments - Windows uses backslash for folders - Mac and Linux use forward slash for folders Inputs - Info can come from many ways 1. Coded by programmer 2. Keyboard input 3. File info - Input could be a path to a file or directory where file is located so it can be processed 1. Opening file 2. Reading the file 3. Overwriting or appending the file 4. Closing the file - Two file types: 1. Text - Bits encoded with ASCII or Unicode - Can be viewed with editors such as Vim (on Linux), TextEdit (on Mac) Notepad (on Windows), or PyCharm (any system) - Examples: Python source files, web pages 2. Binary - Contain arbitrary sequences of bits which do not conform to ASCII or Unicode characters - Examples: Most images, word processor files,... Paths - Telling the computer exactly where to find the file - ABSOLUTE PATHS: start from the drive or root - Examples: - Windows: C:\users\Richard\Desktop\inputFile.csv - Mac: /users/Richard/Desktop/inputFile.csv - Linux: /home/grads/Richard/inputFile.csv - Check if the file exists with path.exist() function in python - If input file is in the same level/location as the executed module, you can omit the path and just provide the file name - File names - Can be almost anything (Invalid characters, or reserved keywords cannot be used) - Opening a file - Open() - fileObject = open (filePath, mode) - mode can be: - ‘r’ for reading, - ‘w’ for writing (truncates the file first), - ‘a’ for appending (write to the end of the file), - The default is ‘r’ - filename = "data" - fileObject = open (filename, "r") - Reading a file - File I/O - File Pointer - When open () is called, a file handler with a pointer is returned to the caller - It points at the start of the file -.tell() gives the position of the file - Reading files from a line - seek (offset, whence) to reposition the pointer - Offset = position to move to - Whence = option parameter - 0: offset is an absolute position starting from the beginning of the file - 1: offset is a relative position to the current pointer position - 2: offset is from the end of the file - Writing to a file - File must be opened for “w” or “a” (write or append) - file handler = open (“cs.txt”, “w”) - Ways to write: 1. outputFile.write() 2. outputFile.writelines() - Closing a file -.close() - When writing to a file, it does not happen instantly, it has a buffer - Python and OS calculate the best time to flush the data onto the actual file area in memory - If your program crashes or terminates without closing, it could remove the flush process - Closing files frees up memory - Exceptions - Types of errors: 1. Syntax 2. Logic/Semantic 3. Runtime - Exceptions are runtime errors, events that disrupt the execution - They can come up due to many errors such as: - Improper use of functions or operators - User input - Logic Errors - Hardware and OS limitations - Exceptions can be handled - Using conditionals - Using try/catch blocks - Try/except - try: #segment that could cause an error except: #action to take if error occurs - Should be trying to catch errors in conditionals before using try/except blocks - When to use if statements vs try/catch? - Use if statements to cover as many errors as you can - If it cannot be done. use try/except - Naming exceptions - We must give the type of error names - 10/0 is a ZeroDivisionError - int(“Hello”) is a ValueError Try/Except/Except - Have multiple excepts for different type of errors - try: #blah blah except (Error1, Error2): #Do something except (Error3, Error4): #Do something else - Better coding convention to have a separate except for each type of error that could occur Try/Except/Else - Else is an additional optional clause that is checked after the exceptions - Code within the else clause is executed only if the code under the try clause did not raise an exception. - It is useful in situations where you do not want some code to be protected by the try clause and you want the code to execute only when the code protected by try did not raise an exception. Try/Except/Finally - finally is also an optional clause that may appear after all the except clauses and the else clause (if it was present). - Code within the finally clause always executes even if an exception is raised. - Code within the try clause stops executing at the line where an exception arises. So, it is important to place any code that must execute in finally Linux Command Line - cat (filename) shows contents of the file without opening it - python (filename) executes the python code in the file - You can pass 0,1 or more command line arguments as a form of input - python.py arg1 arg2 - Arguments must be separated by space - If an argument contains a space, you must surround it with double quotes - Python accesses these arguments through sys.argv - argv (argument values) is a list of arguments - - Be careful not to use an index out of the range. To ensure this, use len() - Good program design: 1. Check if argument count is correct - If not end with error message 2. Check if argument data is correct - If not end with error message 3. Check if files can be opened - Use exception handling Command Line Arguments - Reading arguments from the command line - cat (file.py) lists the contents of the file without opening - python (file.py) runs the python code in the file - Argv lists the list of argument Objects and Classes - OOP is a way of programming that splits code into classes - Classes are like the blueprint, and objects are like instances of the blueprint - Creating objects: (object name) = (class name) (arg1, arg2, etc...) - Classes - self refers to the object created from the class - Variables defined outside methods are class variables, where it is shared among all objects in the class - Creating an object from another class - (variable) = (class name) (arg1, arg2, arg3,...) - You can then use the methods from the class - variable.method(arg1, arg2,...) - OOP principles: 1. Abstraction (Hiding implementations, only showing designed functionalities to users) 2. Encapsulation (protecting data) 3. Inheritance (Inheriting methods, variables, traits) 4. Polymorphism (Having the same method name behave differently based on implementation) - Python is less strict with encapsulation - Inheritance - class subclass name (parent class name): - def __init__ (self, arg1, arg2, arg3,...): super(). __init__ (arg1, arg2,...) Recursion - A function that calls itself is called a recursive function - Mutual recursion is when two functions call each other recursively - Problem with mutual recursion: - Could lead to infinite loops - Therefore, every call to the function should make the problem smaller (will solve at one point) - Well defined recursive function has two cases: - Recursive case: Function calls itself - Generally, the call makes the problem smaller - Base case: Does not make a recursive call - Permits function to return and end recursion - Without a well formed base, we get infinite recursions - Recursion use cases: - Drawing Fractals - Search through maze - Merge sort, quick sort, binary sort - Finding total size of all files in dictionary - Factorial - Def factorial (n) if n == 0: return 1 return n*(n-1)! - Binary Tree - defined structure that is recursive - Left.value < self.value < right.value