php editor Strawberry brings you an article today about parsing URLs in shell scripts. When writing shell scripts, you often encounter situations where you need to parse URLs, such as obtaining URL parameters, determining whether the URL is legal, etc. This article will introduce in detail how to use shell scripts to parse URLs, including techniques and methods such as URL encoding and decoding, obtaining URL parameters, and determining URL legitimacy. Whether you are a beginner or an experienced developer, you can learn practical skills from this article and improve your efficiency and technical level in shell script development. Let’s explore together!
My URL is as follows:
sftp://[email protected]/some/random/path
I want to extract the user, host and path from this string. Any part can be of random length.
Use python (IMHO the best tool for the job):
#!/usr/bin/env python import os from urlparse import urlparse uri = os.environ['NAUTILUS_SCRIPT_CURRENT_URI'] result = urlparse(uri) user, host = result.netloc.split('@') path = result.path print('user=', user) print('host=', host) print('path=', path)
Further reading:
The above is the detailed content of Parse URL in shell script. For more information, please follow other related articles on the PHP Chinese website!