80 likes | 198 Views
Tutorial2. Discussing Assignment 1. What is "the shell"?.
E N D
Tutorial2 Discussing Assignment 1
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
Where can I learn about the shell? • Linux Man Pages • Official Ubuntu Documentation • Using The Terminal
# The start of the shell while True: line = input('psh> ') print(line) python3 tut2_1.py
# 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
# 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
# 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
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