(十三)、向shell脚本中传参
一、向脚本中传递位置参数
向脚本中传递参数的数目理论上可以无数多,但是只有前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
(十三)、向shell脚本中传参的更多相关文章
- shell脚本调用传参【转载】
转自:https://www.cnblogs.com/cisum/p/8010658.html 1.直接使用$0,$1,$2,$3 $0是脚本的名字,就是按顺序来 #!/bin/bash # auth ...
- 【原】Gradle调用shell脚本和python脚本并传参
最近由于项目自动化构建的需要,研究了下gradle调用脚本并传参的用法,在此作个总结. Pre build.gradle中定义了$jenkinsJobName $jenkinsBuild两个Jenki ...
- shell 脚本中的入参获取与判断
1.获取shell脚本的入参个数: $# 2.获取shell脚本的第n个入参的字符个数/字符串长度(注意这里的n需要替换为具体的数字,如果这个数字超过实际的入参个数,结果为0): ${#n}
- shell脚本中的各种表达式介绍和使用
#前言:在shell脚本中,有各种的表达式,包括有条件测试表达式,文件表达式,字符串表达式,整数表达式,接下来我们来了解一下他们的使用方法 1.条件测试表达式 #首先来看一下条件测试语法 #条件测试语 ...
- ftp在shell脚本中的使用方法
1. ftp自动登录批量下载文件. #####从ftp服务器上的/home/data 到 本地的/home/databackup#####!/bin/bashftp -n<<!open 1 ...
- shell脚本中执行另一个shell脚本
分类: 可以在一个shell脚本中执行另一个shell脚本(或非可执行文件,主要用于取得一些变量的值),方法是: . 文件名(包括路径) 或 变量=文件名(包括路径) . $变量 注意,圆点后面有 ...
- shell脚本中出现^M
在Windows中编辑的shell脚本,传到linux系统中,在末尾发现出现了很多^M字符 1.问题分析 在windows下使用notepad++写的脚本上传到Linux下,在使用vim编辑的时候我们 ...
- [shell]上一个命令执行完成,才执行下一个操作 | shell脚本中判断上一个命令是否执行成功
shell脚本中判断上一个命令是否执行成功 shell中使用符号“$?”来显示上一条命令执行的返回值,如果为0则代表执行成功,其他表示失败.结合if-else语句实现判断上一个命令是否执行成功. 场 ...
- shell脚本中的日期处理
Ps:这篇文章只是为了做个分类,以后有看到比较好的时间处理命令都会列在这里,您如果有什么好的时间处理命令,可以评论中添加,我会定期查看更新,谢谢! 1.定义一个参数DATE_TODAY,用于记录当天时 ...
随机推荐
- 蓝桥杯——快速排序(2018JavaB组第5题9分)
快速排序(18JavaB5,9') 以下代码可以从数组a[]中找出第k小的元素. 它使用了类似快速排序中的分治算法,期望时间复杂度是O(N)的. 请仔细阅读分析源码,填写划线部分缺失的内容. impo ...
- Qt5字符串编码转换学习
目录 1.通过Python3示例探索常用字符串编码 UTF8 ANSI Unicode 小结 2.Qt5中使用QTextCodec进行编码转换 小结 1.通过Python3示例探索常用字符串编码 下面 ...
- 【刷题笔记】DP优化-单调队列优化
单调队列优化 眼界极窄的ZZ之前甚至不会单调队列--(好丢人啊) 单调队列优化的常见情景: 转移可以转化成只需要确定一个维度,而且这个维度的取值范围在某个区间里 修剪草坪 这个题学长讲的好像是另外一个 ...
- java43
自定义日期格式 import java.text.DateFormat; import java.text.ParseException; import java.util.Date; public ...
- Python【Python基础】
python的使用 1.python的两个版本:python2.0与python3.0.这两个版本的区别在于python3是不向下兼容python2的组件和扩展的,但是在python2.6和2.7的两 ...
- 因为一个Docker问题,我顺手整理从安装到常用命令操作手册
今天,自己写了一部分业务代码,是常规代码的另外一种方式,不能在公司的服务器上测试,就自己在PC端搭建了一套和公司集群一样的模板,因为公司的业务模块的测试有单独的服务器(这一块还是我很稀罕的),但是,第 ...
- 从内存泄露、内存溢出和堆外内存,JVM优化参数配置参数
内存泄漏 内存泄漏是指程序在申请内存后,无法释放已申请的内存空间,无用对象(不再使用的对象)持续占有内存或无用对象的内存得不到及时释放,从而造成内存空间的浪费.内存泄漏最终会导致OOM. 造成内存泄漏 ...
- PyQt(Python+Qt)学习随笔:QListView的wordWrap属性
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QListView的wordWrap属性与QTableView的wordWrap属性功能完全相同,用 ...
- 团队作业4-Day1
团队作业4-Day1 1. 各个成员在 Alpha 阶段认领的任务 Alpha任务分配 人员 小程序样式实现 吴安冬+吴梓华 小程序js代码及云数据实现 庾艺锋+白军强 项目测试 王泽鑫+赵玮锋 2. ...
- 查询时间倒退一天-项目中惊现神秘BUG-JsonFormat使用采坑记
一.问题由来 前一天下午正在写代码的时候,领导突然走过来跟我说,让我去看一个神秘的BUG,说是在数据库中查询时的一个日期 返回到页面后,查询时间倒退了一天.一听到这个BUG,我就感觉很奇怪,还有这样的 ...