在 shell 脚本中解析 URL [英] Parse URL in shell script

查看:45
本文介绍了在 shell 脚本中解析 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下网址:

sftp://user@host.net/some/random/path

我想从这个字符串中提取用户、主机和路径.任何部分都可以是随机长度.

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

推荐答案

使用 Python(此工作的最佳工具,恕我直言):

Using Python (best tool for this job, IMHO):

#!/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)

进一步阅读:

这篇关于在 shell 脚本中解析 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆