永发信息网

如何正确处理Shell 函数传人参数中包含的特殊字符

答案:3  悬赏:50  手机版
解决时间 2021-02-26 00:20
  • 提问者网友:蓝莓格格巫
  • 2021-02-25 12:56
如何正确处理Shell 函数传人参数中包含的特殊字符
最佳答案
  • 五星知识达人网友:渊鱼
  • 2021-02-25 14:32
处理Shell 函数传人参数中包含的特殊字符,要根据shell函数内部实现来具体问题具体分析。
一般来说,将入参作为参数再传入其他函数时,不需 ,直接传入。
如果要查找入参中的特殊字符,需要用\转义
全部回答
  • 1楼网友:愁杀梦里人
  • 2021-02-25 16:15
步骤方法: 在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数... 带参数的函数示例:#!/bin/bash funWithParam(){ echo "The value of the first parameter is $1 !" echo "The value of the second parameter is $2 !" echo "The value of the tenth parameter is $10 !" echo "The value of the tenth parameter is ${10} !" echo "The value of the eleventh parameter is ${11} !" echo "The amount of the parameters is $# !" echo "The string of the parameters is $* !" } funWithParam 1 2 3 4 5 6 7 8 9 34 73 输出: The value of the first parameter is 1 ! The value of the second parameter is 2 ! The value of the tenth parameter is 10 ! The value of the tenth parameter is 34 ! The value of the eleventh parameter is 73 ! The amount of the parameters is 12 ! The string of the parameters is 1 2 3 4 5 6 7 8 9 34 73 !" 注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。 另外,还有几个特殊字符用来处理参数: 参数处理 说明 $# 传递到脚本的参数个数 $* 以一个单字符串显示所有向脚本传递的参数 $$ 脚本运行的当前进程ID号 $! 后台运行的最后一个进程的ID号 $@ 与$#相同,但是使用时加引号,并在引号中返回每个参数。 $- 显示Shell使用的当前选项,与set命令功能相同。 $? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。
  • 2楼网友:煞尾
  • 2021-02-25 15:17
我看些网我文件非相应命令执行起太慢快点 授鱼授渔: ls $1 其$1传入脚本第参数面套说明些我自看书总结结合着习吧: 匹配字符串串度2种其$substring则表达式: 1. expr match "$string" '$substring' 2. expr "$string" : '$substring' 串删除-------构造文件名候操作用 ${string#substring} #string删除短匹配substring字符串 ${string##substring} #删除匹配 ${string%substring} #string结尾位置删除短匹配substring字符串 ${string%%substring} # 匹配
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯