Home > Backend Development > Golang > Parse URL in shell script

Parse URL in shell script

WBOY
Release: 2024-02-10 09:00:10
forward
794 people have browsed it

Parse URL in shell script

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!

Question content

My URL is as follows:

sftp://[email protected]/some/random/path
Copy after login

I want to extract the user, host and path from this string. Any part can be of random length.

Workaround

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)
Copy after login

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!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template