Learn Unix from Scratch

https://www.eduonix.com/courses/system-programming/learn-linux-from-scratch

https://www.eduonix.com/blog/category/shell-scripting/page/2/

Advertisements

Python PhoneBook Example


import os
class Phonebook:
    def __init__(self):
        self.phonebook = {}
        self.phonebook_file = 'Phonebook.txt'
    def loadAll(self):
        #Clear the phonebook dictionary
        self.phonebook.clear()
        
        #Load all of the items from the text file into the dictionary
        file = open(self.phonebook_file, 'r')
        for line in file.readlines():
            name, number = line.strip().split()
            self.phonebook[name] = number
        file.close()
        
    def addEntry(self):
        self.loadAll()
        #Prompt the user for the details of the new entry
        name = raw_input("ENTER NAME: ")
        number = raw_input("ENTER NUMBER: ")
        #Create a string to be written to the file
        new_entry = name + '\t' + number + '\n'
        #Write the string to the file
        file = open(self.phonebook_file, 'a')
        file.write(new_entry)
        file.close()
        
    def readAll(self):
        self.loadAll()
        #Print out the entire phonebook dictionary
        for name, number in self.phonebook.items():
            print(name, " : ", number)
        if len(self.phonebook) == 0:
            print("PHONEBOOK IS EMPTY")
            
    def searchEntry(self):
        self.loadAll()
        #Prompt the user for the name to search for, and search the phonebook dictionary 
        search = raw_input("ENTER NAME TO SEARCH FOR: ")
        if search in self.phonebook.keys():
            print(search, " : ", self.phonebook[search])
        else:
            print("ENTRY NOT FOUND")
            
    def deleteEntry(self):
        self.loadAll()
        entry_to_delete = raw_input("ENTER NAME OF ENTRY TO DELETE: ")
        if entry_to_delete in self.phonebook.keys():
            del self.phonebook[entry_to_delete]
            file = open(self.phonebook_file, 'w')
            for name, number in self.phonebook.items():
                string = name + '\t' + number + '\n'
                file.write(string)
            file.close()
            print("ENTRY DELETED SUCCESSFULLY")
        else:
            print("ENTRY NOT FOUND")
    def exitProgram(self):
        os._exit
    def menu(self):
        print("""\
       -MENU-
1) READ ALL ENTRIES
2) ADD AN ENTRY
3) DELETE AN ENTRY
4) LOOK UP AN ENTRY
5) Exit\n""")
        choice = raw_input("ENTER CHOICE: ")
        choice_menu = {'1' : self.readAll,
                       '2' : self.addEntry,
                       '3' : self.deleteEntry,
                       '4' : self.searchEntry,
                       '5' : self.exitProgram}
        if choice not in choice_menu.keys():
            print("PLEASE ENTER A VALID CHOICE")
        else:
            choice_menu[choice]()
            
Book_1 = Phonebook()
Book_1.menu()

Ref : https://www.daniweb.com/programming/software-development/threads/361480/phonebook-program

Ref : http://mathamy.com/pages/phonebook.html

Unix Monitoring

General UNIX Monitoring Tools

The following general purpose UNIX monitoring tools can be used with Oracle Unified Directory.

Tool Description
iostat Provides information about disk I/O and CPU usage.
lsof Provides information about open file descriptors.
lslk Provides information about file system locks.
netstat Provides statistics about network functions.
nslookup Allows you to query DNS servers for information about hosts and domains.
ping Allows you to query the status of a remote host or network gateway.
sar UNIX System V performance monitoring tool.
tcpdump Allows you to debug and monitor network traffic.
top Provides quick, easy monitoring of processes and CPU activities.
trace Provides information about which system calls a process makes.
traceroute Provides the path a packet takes throughout the Internet to reach its final destination.
vmstat Provides statistics about process, virtual memory, disk, trap, and CPU activity.