1 / 8

Tutorial2

Tutorial2. Discussing Assignment 1. What is "the shell"?.

korene
Download Presentation

Tutorial2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tutorial2 Discussing Assignment 1

  2. What is "the shell"? • The Linux/Unix shell refers to a special program that allows you to interact with it by entering certain commands from the keyboard; the shell will execute the commands and display its output on the monitor. The environment of interaction is text-based unlike the GUI • Retrieved from: http://linux.about.com/od/linux101/a/desktop11.htm

  3. Where can I learn about the shell? • Linux Man Pages • Official Ubuntu Documentation • Using The Terminal

  4. # The start of the shell while True: line = input('psh> ') print(line) python3 tut2_1.py

  5. # Non class version: Break the line into shell words. import shlex defword_list(line): lexer= shlex.shlex(line, posix=True) lexer.whitespace_split = False lexer.wordchars += '#$+-,./?@^=' args = list(lexer) return args while True: line = input('psh> ') print(word_list(line)) python3 tut2_2.py

  6. # Class version: Break the line into shell words. import shlex class PShell(): defread_loop(self): while True: line = input('psh> ') print(self.word_list(line)) defword_list(self, line): lexer= shlex.shlex(line, posix=True) lexer.whitespace_split = False lexer.wordchars += '#$+-,./?@^=' args = list(lexer) return args my_shell = PShell() my_shell.read_loop() python3 tut2_3.py

  7. # Executing only the first command on the line import shlex import os class PShell(): defread_loop(self): while True: line = input('psh> ') words = self.word_list(line) print(words) command = words[0] child = os.fork() if child == 0: # we are in the child os.execvp(command, words) else: os.waitpid(child, 0) defword_list(self, line): lexer = shlex.shlex(line, posix=True) lexer.whitespace_split = False lexer.wordchars += '#$+-,./?@^=' args = list(lexer) return args my_shell = PShell() my_shell.read_loop() python3 tut2_4.py

  8. Python exception handling # Class version # executing only the first command on the line import shlex import os class PShell(): defread_loop(self): while True: line = input('psh> ') words = self.word_list(line) print(words) command = words[0] try: child = os.fork() if child == 0: # we are in the child os.execvp(command, words) else: os.waitpid(child, 0) except OSError: print('Caught an OSError.') defword_list(self, line): """Break the line into shell words. """ lexer = shlex.shlex(line, posix=True) lexer.whitespace_split = False lexer.wordchars += '#$+-,./?@^=' args = list(lexer) return args my_shell = PShell() my_shell.read_loop() python3 tut2_5.py

More Related