Processes The Operating System, Shells, and Python Shell Commands a Command Line Interface accepts typed (textual) inputs and provides textual outputs. Synonyms: - Command prompt - Shell - CLI Shell commands are good for: - managing computer resources - automating tedious, repetitious, and long-running tasks 1
Some Available Shells bash - the default in most Linux installations sh - the original Unix shell; less capable than bash dash, csh, zsh, tsh, tcsh, other alternatives COMMAND.COM - the original DOS "command prompt" CMD.EXE - Command prompt for Windows NT installations Windows PowerShell - Fully programmable option for Windows XP and later Basic Computer Management Linux File and Directory Operations ls display listing of files in current directory pwd (print working directory) show the current directory's name cd, chdir (change directory) choose a new "current directory" md, mkdir make a new directory rd, rmdir remove a directory (if empty) ln -s make a symbolic link to a directory - similar to a DOS/Windows "shortcut" 2
Basic Computer Management File Operations rm remove a file entry from a directory - DOS: del or erase mv move a file entry to a different name or location - DOS: move, rename cp copy a file - DOS: copy, xcopy chmod change a file's usage permissions - DOS: attrib ln (link) create a new directory entry to an existing disk file Basic Computer Management Process-Control Operations Program execution Input / Output redirection - <, >, Process monitoring - ps, jobs - time Process prioritization - fg, bg, & - nice Process termination - kill 3
Program Execution A program is a set of computer instructions and commands that perform some actions - Programs are kept in disk files Running, or executing, a program places it into the computer's memory as a process - One program can lead to many processes - The collection of a running process or group of processes, and the files (and other resources) that are used, is often called a job Running a process requires finding the disk file in its directory - The Executable Path is the default set of directories Some Features of Shells Environment variables - set - declare - unset - echo Customizing the prompt Paths to executables aliases Command history Name completion 4
Input Redirection Processes get input from stdin - Defaults to the keyboard Redirect stdin to get input from a disk file - use < Example: - myprog < data.txt Output Redirection Processes send output to stdout and stderr - Both default to the screen ( console display ) Redirect stdout to send output to a disk file - use > Redirect stderr to send alternate output - use 2> - bash shell: &> also works Examples - myprog > results.txt - myprog 2> errorlog.txt 5
Appending Output The > operator creates or overwrites the destination file - Any previous contents are lost Existing output files can be added to: - myprog >> outputs.log - myprog 2>> errors.log This opens an existing file, or creates a new one, and adds the output to the end Redirecting Input and Output Redirecting can be combined: - myprog < in.txt > out.txt &> errors.txt stdout and stderr can be combined via >& : - myprog > all-output.txt 2>&1» First, redirect stdout to a file» Second, redirect stderr (2) to stdout (1)» Order of redirects is important - Also works with appending, via >>& : myprog >> history.log 2>>&1 6
Connecting Programs Standard output from one program can be sent directly to the standard input of another program - This is called piping output to the next program the pair (or more) of programs is called a pipeline Use the pipe operator Examples - prog1 prog2 - prog1 prog2 prog3 - prog1 < input.txt prog2 > output.txt a big example Try this: - time find /usr -regex '.*\(share\ lib\).*py' tee py-report.out wc -l & What this does: - time reports how long this pipeline takes to run - find looks for files that match a regular expression - tee copies its standard input both to standard output and to the provided file - wc -l counts the number of lines in its standard input - the & makes this pipeline run in the background 7
Linux Shell Scripts Shell scripts are collections of commands, saved in a text file Text file begins with "magic" comment: - #!/bin/sh or maybe #!/bin/bash (Comparable DOS batch files and command scripts are recognized by their name extensions, rather than by their contents.) Scripts include comments as lines starting with a # character - the first line is the "magic" comment shown above Simple Shell Script This is the "big example", scripted for easy reuse - The backslash "\" at the end of line 4 is a line continuation escape 8
Not-As-Simple Shell Script 9