欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 运维知识 > linux >内容正文

linux

linux script $,linux – 这在shell脚本中是什么意思SCRIPTNAME =“${0 ## * /}”?

发布时间:2024/7/19 linux 43 豆豆
生活随笔 收集整理的这篇文章主要介绍了 linux script $,linux – 这在shell脚本中是什么意思SCRIPTNAME =“${0 ## * /}”? 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

此代码来自apache2服务启动脚本.

这是什么意思?

SCRIPTNAME="${0##*/}"

解决方法:

它找到正在运行的脚本的名称,删除其目录.例如,如果脚本是/etc/init.d/httpd,那么这将设置SCRIPTNAME = httpd.

$0或${0}是正在执行的脚本的名称. ##运算符用于删除与模式* /匹配的任何前导字符串. *是通配符,因此* /表示“任何字符串后跟正斜杠”.

这样做的效果是从$0中删除任何前导目录名称,只留下脚本的名称.

来自man bash:

${parameter#word}

${parameter##word}

The word is expanded to produce a pattern just as in pathname expansion. If the pattern

matches the beginning of the value of parameter, then the result of the expansion is the

expanded value of parameter with the shortest matching pattern (the “#” case) or the longest

matching pattern (the “##” case) deleted. If parameter is @ or *, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant

list. If parameter is an array variable subscripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

标签:linux,sh

来源: https://codeday.me/bug/20190902/1790603.html

总结

以上是生活随笔为你收集整理的linux script $,linux – 这在shell脚本中是什么意思SCRIPTNAME =“${0 ## * /}”?的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。