background preloader

Python and BASH

Facebook Twitter

Edharn / EdGit. Linux and Unix df command help and examples. Bash while loop calling a Python script. Work the Shell - Understanding Exit Codes. Last month, we looked at signals, the rudimentary mechanism that processes on a Linux box can use to communicate events and state changes.

Work the Shell - Understanding Exit Codes

We talked about how each of the signals can be sent manually to a running process with the kill command, and how shell scripts then can catch and respond to specific signals (though not all of them—some cannot be caught because they're actually handled by the operating system itself). Analogous to signals, exit codes turn out to be an easy way for processes to communicate state back to the calling parent process, in a way that most Linux users just ignore. Not anymore—this month, we're going to take a closer look. Let's start with a simple Linux command that everyone's probably already mastered: mv, which moves a file or directory from one spot in the filesystem to another (and/or renames it). As you know, you can generate errors if the target is missing, destination is missing and so on.

. $ mv missing ~/missing2 mv: cannot move `missing' to `... #! $ . #! Writing shell scripts - Lesson 9: Flow Control - Part 1. In this lesson, we will look at how to add intelligence to our scripts.

Writing shell scripts - Lesson 9: Flow Control - Part 1

So far, our script has only consisted of a sequence of commands that starts at the first line and continues line by line until it reaches the end. Most programs do more than this. They make decisions and perform different actions depending on conditions. The shell provides several commands that we can use to control the flow of execution in our program. These include: if exit for while until case break continue if The first command we will look at is if. . # First form if condition ; then commandsfi # Second form if condition ; then commandselse commandsfi # Third form if condition ; then commandselif condition ; then commandsfi In the first form, if the condition is true, then commands are performed.

In the second form, if the condition is true, then the first set of commands is performed. In the third form, if the condition is true, then the first set of commands is performed. What is a "condition"? 17.1. subprocess — Subprocess management — Python v2.7.5 documentation. Execute a child program in a new process.

17.1. subprocess — Subprocess management — Python v2.7.5 documentation

On Unix, the class uses os.execvp()-like behavior to execute the child program. On Windows, the class uses the Windows CreateProcess() function. The arguments to Popen are as follows. args should be a sequence of program arguments or else a single string. By default, the program to execute is the first item in args if args is a sequence. On Unix, if args is a string, the string is interpreted as the name or path of the program to execute. Note shlex.split() can be useful when determining the correct tokenization for args, especially in complex cases: >>> import shlex, subprocess>>> command_line = raw_input()/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'">>> args = shlex.split(command_line)>>> print args['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]>>> p = subprocess.Popen(args) # Success!

Python for Bash scripters: A well-kept secret. Python [sys] 07 Command-line Arguments.