Python and bash shell scripts

Source: Internet
Author: User
Tags call shell

 

As a beginner shell programmer, it is simple and practical to use shell commands, such as writing a few lines and performing some automated operations, but it involves logical control and

If/FI, Case/esac ...... And the weird Boolean judgment of ancient times ...... There are also complicated and well-qualified SED, awk ...... I thought,

Why not use Python when there are no commands to be written in shell?

When there are commands that must be written in shell, Shell and Python are combined very well, think about the differences between 1 K shell lines and 1 K Python lines, such as reading, writing, debugging, and maintenance.

 

 

QUOTE:

 

Use python to write shell scripts

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? First of all, it is because of the inexplicable syntax of the sh script. It feels like a program that is flushed along with writing. There is no beauty at all. Second, the processing capability of SH scripts 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 the target directory of the target file. 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? 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 (name of the regular expression file), tempfile (temporary file), pwd (Operation/etc/passwd file) are often 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.