# Make script executable (for unix systems) $ chmod "a+x" cli.py # Run script (the unix way) $ ./cli.py Hello, world # Run script (the Windows way) $ python cli.py Hello, world # Get help on the chmod program $ man chmod # Running the version of cli.py that lists the contents of sys.argv $ ./cli.py 0 ./cli.py $ ./cli.py this is "some data" you know 0 ./cli.py 1 this 2 is 3 some data 4 you 5 know # Running the version of cli.py that reads stdin as well (I flushed stdin with ctrl-d and killed the script with ctrl-c) $ ./cli.py this is "some data" you know 0 ./cli.py 1 this 2 is 3 some data 4 you 5 know first line second line third line Read first line Read second line Read third line ^CTraceback (most recent call last): File "./cli.py", line 9, in for i in sys.stdin: KeyboardInterrupt # Same version of cli.py, this time piping to stdin from echo (a simple program that prints the given text to stdout) $ echo "hi there" | ./cli.py this is "some data" you know 0 ./cli.py 1 this 2 is 3 some data 4 you 5 know Read hi there # New version of cli.py -- does't print contents of sys.argv, does read from either a given filename or stdin (for the special case of filename == "-") $ echo "hi there" | ./cli.py - Read hi there # Same version, printing itself $ echo "hi there" | ./cli.py cli.py Read #!/usr/bin/env python Read Read import sys Read Read if(__name__ == "__main__"): Read if(len(sys.argv) < 2): Read sys.stderr.write("Usage: "+sys.argv[0]+" -|input_filename\n") Read if(sys.argv[1] == "-"): Read myfile = sys.stdin Read else: Read myfile = open(sys.argv[1]) Read Read for i in myfile: Read print "Read",i Read Read # Same thing, trimming the extra newlines that print introduces $ echo "hi there" | ./cli.py cli.py Read #!/usr/bin/env python Read Read import sys Read Read if(__name__ == "__main__"): Read if(len(sys.argv) < 2): Read sys.stderr.write("Usage: "+sys.argv[0]+" -|input_filename\n") Read if(sys.argv[1] == "-"): Read myfile = sys.stdin Read else: Read myfile = open(sys.argv[1]) Read Read for i in myfile: Read print "Read",i, Read Read # Same thing, this time triggering the error message for no input file given (this is more useful than defaulting to stdin) $ echo "hi there" | ./cli.py Usage: ./cli.py -|input_filename # Same thing, the Windows way $ python cli.py cli.py Read #!/usr/bin/env python Read Read import sys Read Read if(__name__ == "__main__"): Read if(len(sys.argv) < 2): Read sys.stderr.write("Usage: "+sys.argv[0]+" -|input_filename\n") Read sys.exit(1) Read if(sys.argv[1] == "-"): Read myfile = sys.stdin Read else: Read myfile = open(sys.argv[1]) Read Read for i in myfile: Read print "Read",i, Read Read # Using "<" to redirect a file to stdin $ ./cli.py - < cli.py Read #!/usr/bin/env python Read Read import sys Read Read if(__name__ == "__main__"): Read if(len(sys.argv) < 2): Read sys.stderr.write("Usage: "+sys.argv[0]+" -|input_filename\n") Read sys.exit(1) Read if(sys.argv[1] == "-"): Read myfile = sys.stdin Read else: Read myfile = open(sys.argv[1]) Read Read for i in myfile: Read print "Read",i, Read Read # Also using ">" to redirect stdout to a file $ ./cli.py - < cli.py > cli.txt $ less cli.txt $ less cli.txt # Using "<< EOF" to pre-specify the contents $ ./cli.py - << EOF > Hello world > this > is some more text > EOF Read Hello world Read this Read is some more text # Using the "<< EOF" trick as a text editor $ cat > test.py << EOF > #!/usr/bin/env python > print "Hello world" > EOF $ chmod "a+x" test.py $ ./test.py Hello world # Redirecting nothing to a file is a cute way to make a zero length file $ > empty.txt