(十三)、向shell脚本中传参
2024-09-04 04:35:33
一、向脚本中传递位置参数
向脚本中传递参数的数目理论上可以无数多,但是只有前9个能被访问,使用shift可以改变此限制
$0 | $1 | $2 | $3 | $4 | $5 | $6 | $7 | $8 | $9 |
脚本名字 | first | second | third | fourth | fifth | sixth | seventh | eighth | ninth |
1 $ vim param
2
3 #!/bin/sh
4 echo "This is the script name : $0"
5 echo "This is the first param : $1"
6 echo "This is the second param : $2"
7 echo "This is the third param : $3"
8 echo "This is the fourth param : $4"
9 echo "This is the fifth param : $5"
10 echo "This is the sixth param : $6"
11 echo "This is the seventh param : $7"
12 echo "This is the eighth param : $8"
13 echo "This is the ninth param : $9"
$ chmod u+x param
$ ./param Did You See The Full Moon
This is the script name : Did
This is the first param : You
This is the second param : See
This is the third param : The
This is the fourth param : Full
This is the fifth param : Moon
This is the sixth param :
This is the seventh param :
This is the eighth param :
This is the ninth param :
二、向系统命令中传递参数
1 $ vim findfile
2 #!/bin/sh
3 # findfile
4 find / -name $1 -print #find $1 in /
5
6 $ chmod u+x findfile
7 $ findfile passwd
8 /etc/passwd
9 /etc/uucp/passwd
10 /usr/bin/passwd
三、特定变量参数
$# | 传递到脚本的参数个数 |
$* | 以一个单字符串显示所有向脚本传递的参数 |
$$ | 脚本运行的当前进程ID号 |
$! | 后台运行的最后一个进程的ID号 |
$@ | 与$#相同,但是 使用时加双引号 |
$- | 显示shell使用的当前选项 |
$? | 显示最后命令的退出状态 |
$ vim param #!/bin/sh
echo "This is the script name : $0"
echo "This is the first param : $1"
echo "This is the second param : $2"
echo "This is the third param : $3"
echo "This is the fourth param : $4"
echo "This is the fifth param : $5"
echo "This is the sixth param : $6"
echo "This is the seventh param : $7"
echo "This is the eighth param : $8"
echo "This is the ninth param : $9"
echo "The number of arguments passed : $#"
echo "show all arguments : $*"
echo "did my script go with any errors : $?"
$ chmod u+x param
$ ./param Did You See The Full Moon
This is the script name : Did
This is the first param : You
This is the second param : See
This is the third param : The
This is the fourth param : Full
This is the fifth param : Moon
This is the sixth param :
This is the seventh param :
This is the eighth param :
This is the ninth param
The number of arguments passed : 6
show all arguments : Did You See The Full Moon
did my script go with any errors : 0
最新文章
- 51nod1118(水题)
- equals和=,==的区别
- 你的iOS静态库该减肥了
- PAT (Basic Level) 1013. 数素数 (20)
- sealed 密封类,不能被其他类继承,但可以继承其他类
- vim 常用快捷键 二[转]
- Java线程:锁
- 【Ubuntu 16】网络配置文件
- javascript实现jsp页面的打印预览
- mvc中路由的映射和实现IHttpHandler挂载
- Blocks [POJ3734] [矩阵快速幂]
- SqlServer中循环和条件语句
- ORA-27154: post/wait create failed ORA-27300 ORA-27301 ORA-27302
- 试验IFTTT同步发微博
- 异步核心接口IAsyncResult的实现
- PHP7 ci框架session存文件,登录的时候session不能读取
- markdown | Latex | 书写测试
- c++ 利用new动态的定义二维数组
- 6.1 Controllers -- Introduction
- (转)Windows上使用CMake
热门文章
- 关于element中修改组件使用深度选择器/deep/的问题
- python - os.sep用法
- golang 自学系列(四)——debug for vscode
- chrome浏览器查看当前页面cookie
- Fiddler 4 断点调试(修改response请求参数)
- linux下的bash shell
- MySQL索引(一)索引基础
- PyQt(Python+Qt)学习随笔:Qt Designer中部件的windowTitle和windowOpacity属性
- PyQt(Python+Qt)学习随笔:model/view架构中类QStandardItemModel的使用方法
- SZhe_Scan碎遮:一款基于Flask框架的web漏洞扫描神器