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

Parse URL in shell script

WBOY
WBOYforward
2024-02-10 09:00:10777browse

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

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)

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete