Use python to write shell scripts

Source: Internet
Author: User
Tags call shell

Reprinted from

Http://blog.163.com/skynjl/blog/static/603053922009127101558356/

 

 

Today, my colleague asked me to write a shell script. Although I am deeply influenced by * nix, it is annoying to execute sh scripts that are everywhere in * nix. Why? The first reason is the inexplicable syntax of the sh script.ProgramThere is no beauty at all. Second, the processing capability of the sh script is still relatively weak. In terms of text processing, XML processing, and network programming, a bunch of programs such as Perl and awk are basically required. I also don't like these programs very much. Besides, it takes time to learn third-party software. It's better to use python.

Can Python be used as a shell script? First, we will introduce a function:

OS. System (command)

This function can call shell to run the command line command and return its return value. Enter OS. System ("ls-L") in the python interpreter to list the files in the current directory. It can be said that through this function, Python has all the capabilities of shell. Haha .. However, this command is usually not required. Because the commands commonly used by shell are usually corresponding and concise in Python.

The most common shell is the LS command, and the corresponding method of python is: OS. listdir (dirname). This function returns the string list, which contains all file names but does not contain ". "and ".. ". If you want to traverse the entire directory, it will be a little more complicated. Let's wait. First, try the following in the interpreter:

>>> OS. listdir ("/")

['Tmp ', 'misc', 'opt', 'root ','. autorelabel ', 'sbin', 'srv ','. autofsck ', 'mnt', 'usr', 'var', 'etc ', 'selinux', 'lib', 'net', 'lost + found ', 'sys ', 'Media', 'dev', 'proc', 'boot', 'home', 'bin']

In this way, all subsequent commands can directly run the viewing results in the python interpreter.

Corresponding to the CP command: shutil. copy (SRC, DEST). This function has two parameters: the SRC Parameter refers to the name of the source file, and the Dest parameter refers to the name of the target file or target directory. If DEST is a directory name, a file with the same name will be created under that directory. Similar to the shutil. Copy function, the shutil. copy2 (SRC, DEST) function copies the last access time and last update time.

However, the shell CP command can also copy directories, but the python shutil. Copy command cannot. The first parameter can only be a file. What should I do? In fact, Python also has a shutil. copytree (SRC, DST [, symlinks]). The parameter has an additional symlinks, which is a Boolean value. If it is true, a symbolic link is created.

What about moving or renaming files and directories? It is estimated that shutil. Move (SRC, DST) was guessed by a smart friend .. Similar to the MV command, if SRC and DST are on the same file system. move is just a simple change of the name, if SRC and DST are on different file systems, shutil. move will first copy SRC to DST and then delete the SRC file. As you can see, most of my friends may already have an eye on Python's capabilities. Next I will list a table and introduce other functions:

OS. chdir (dirname)

Switch the current working directory to dirname

OS. getcwd ()

Returns the current working directory path.

OS. chroot (dirname)

Use dirname as the root directory of the process. Similar to the chroot command in * nix

OS. chmod (path, Mode)

Change the path permission bit. Mode can be a combination of the following values (OR:

OS. s_isuid

OS. s_isgid

OS. s_enfmt

OS. s_isvtx

OS. s_iread

OS. s_iwrite

OS. s_iexec

OS. s_irwxu

OS. s_irusr

OS. s_iwusr

OS. s_ixusr

OS. s_irwxg

OS. s_irgrp

OS. s_iwgrp

OS. s_ixgrp

OS. s_irwxo

OS. s_iroth

OS. s_iwoth

OS. s_ixoth

What are the meanings of them? I will not elaborate on them. Basically, R represents read, W represents write, and X represents execution permission. USR indicates the user, GRP indicates the group, and oth indicates others.

OS. chown (path, uid, GID)

Change the owner of a file. When UID and GID are-1, the original owner is not changed.

OS. Link (SRC, DST)

Create a hard connection

OS. mkdir (path, [mode])

Create a directory. For more information about the meaning of mode, see OS. chmod (). The default value is 0777.

OS. makedirs (path, [mode])

Similar to OS. mkdir (), however, a non-existing parent directory is created first.

OS. readlink (PATH)

Returns the path to which the symbolic link points.

OS. Remove (PATH)

Deleting a file cannot be used to delete a directory.

OS. rmdir (PATH)

Deleting folders cannot be used to delete objects.

OS. symlink (SRC, DST)

Create a symbolic link

Shutil. rmtree (path [, ignore_errors [, onerror])

Delete folder

If you have introduced so much, you only need to check the documents of the OS and shutil modules .. Note the following when writing shell scripts:

1. environment variables. The Python environment variables are stored in the OS. Environ dictionary. You can use the normal dictionary to modify them. When other programs are started using the system, the environment variables are automatically inherited. For example:

OS. Environ ["fish"] = "nothing"

Note that the value of the environment variable can only be a string. Unlike shell, Python does not have the export environment variable concept. Why not? Because Python does not need to have :-)

2. the OS. Path module contains many functions about path name processing. In Shell, path name processing is not very important, but it is often used in Python. The two most common examples are separation and merge directory names and file names:

OS. Path. Split (PATH)-> (dirname, basename)

This function separates a path into two parts, for example, OS. path. split ("/Foo/bar. dat) returns ("/foo", "bar. dat ")

OS. Path. Join (dirname, basename)

This function combines the directory name and file name into a complete path name, such as OS. path. join ("/Foo", "bar. dat) returns "/Foo/bar. dat ". This function is the opposite of OS. Path. Split.

There are also these functions:

OS. Path. abspath (PATH)

Convert path to absolute path

OS. Path. expanduser (PATH)

Include "~" In Path "~" And "~ Convert user to user directory

OS. Path. expandvars (PATH)

Replace the "$ name" and "$ {name}" contained in the path based on the value of the environment variable, such as the Environment Variable fish = nothing, that OS. path. expandvars ("$ fish/ABC") returns "Nothing/ABC"

OS. Path. normpath (PATH)

Remove the "." and "." contained in the path ".."

OS. Path. splitext (PATH)

Separate the path into the basic name and extension. For example, OS. Path. splitext ("/Foo/bar.tar.bz2") returns ('/Foo/bar.tar', '.bz2 ′). Note the difference between it and OS. Path. Split ().

3. in the OS module, there is a very useful function called OS. stat () is not introduced because OS. the path module contains a group of functions that have the same functions, but the name is easier to remember.

OS. Path. exists (PATH)

Determine whether a file or directory exists

OS. Path. isfile (PATH)

Determine whether the path points to a common file rather than a directory

OS. Path. isdir (PATH)

Determine whether the path points to a directory rather than a common file

OS. Path. islink (PATH)

Determine whether the path points to a symbolic link

OS. Path. ismount (PATH)

Determine whether the path points to a mount point)

OS. Path. getatime (PATH)

Returns the last access time of the file or directory pointed to by path.

OS. Path. getmtime (PATH)

Returns the last modification time of the file or directory pointed to by path.

OS. Path. getctime (PATH)

Returns the creation time of the file pointed to by path.

OS. Path. getsize (PATH)

Returns the size of the file pointed to by path.

4. OS, shutil, glob (Regular Expression file name), tempfile (temporary file), and PWD (Operation/etc/passwd file) are frequently used to write shell scripts in Python ), GRP (Operation/etc/group file), commands (get the output of a command ). The previous two articles have basically been introduced. The next few articles are very simple. Just read the documents.

5. SYS. argv is a list that stores the command line parameters of the python program. Here, SYS. argv [0] is the name of the program.

You can't just say you don't want to practice it. Next we will write a simple script for copying files. My colleague who asked me to write a script two days ago had a directory of tens of thousands of files. He wanted to copy these files to other directories and could not directly copy the directory itself. He tried "CP src/* DEST/" and reported a command line too long error, asking me to write a script for him. Start with Python:

Import sys, OS. Path, shutil

For f in OS. listdir (SYS. argv [1]):

Shutil. Copy (OS. Path. Join (SYS. argv [1], F), SYS. argv [2])

Try the post in the linuxapp version-rename all the files in a folder to 10001 ~ 10999. You can write as follows:

Import OS. Path, sys

Dirname = SYS. argv [1]

I = 10001.

For f in OS. listdir (dirname ):

Src = OS. Path. Join (dirname, F)

If OS. Path. isdir (SRC ):

Continue

OS. Rename (SRC, STR (I ))

I + = 1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.