Bash脚本,如何删除尾随子字符串,不区分大小写?
我正在修改读取用户电子邮件的脚本。很简单,太简单了。
echo -n "Please enter your example.com email address: "
read email
email=${email%%@example.com} # removes trailing @example.com from email
echo "email is $email"
这有效,但仅适用于小写@example.com。我如何修改它以删除尾随的@example.com(不区分大小写)?
I am modifying a script that reads in a user email. It is very simple, too simple.
echo -n "Please enter your example.com email address: "
read email
email=${email%%@example.com} # removes trailing @example.com from email
echo "email is $email"
This works, but only for lower case @example.com. How could I modify this to remove the trailing @example.com, case insensitive?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用不区分大小写的正则表达式匹配:
或者,您可以根据字符串的长度截断字符串(只有当您知道用户正确输入域时,这才有意义):
Use case insensitive matching to a regular expression:
Alternatively, you can cut off a string based on its length (which only makes sense if you know that the user entered the domain correctly):
如果您有 bash 4:
否则,也许只需使用 tr:
更新:
如果您只是想剥离主机(任何主机),那么也许这确实是您想要的:
If you have bash 4:
Otherwise, perhaps just use tr:
Update:
If you are just wanting to strip the host (any host) then perhaps this is really what you want:
对于 Bash 3.2 及更高版本:
For Bash 3.2 and greater:
使用 sed 怎么样?
请注意 sed 替换命令中的“i”标志,它请求不区分大小写的匹配。
How about using sed?
Note the 'i' flag in the sed substitution command which requests case-insensitive matching.
这是另一个(虽然有点长)的看法:
Here's yet another (though a bit lengthy) take on it:
要将电子邮件转为小写,您可以使用声明,例如
声明 -l email=$email
and for transfer email to lower case you can use declare, such as
declare -l email=$email