• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >linux shell > Shell脚本传参数方法总结

Shell脚本传参数方法总结

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了shell脚本参数,shell脚本接收参数,shell脚本传参数,shell脚本获取参数,shell脚本 p参数等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

一、接收固定长度的参数
[root@svn shell_example]# cat params.sh
#!/bin/bash
#传参测试脚本
echo "My name is `basename $0` -I was called as $0"
echo "My first parameter is : $1"
echo "My second parameter is : $2"
</div>
空参数执行
[root@svn shell_example]# sh params.sh
My name is params.sh -I was called as params.sh
My first parameter is :
My second parameter is :
</div>
传递2个参数执行
[root@svn shell_example]# sh params.sh one two
My name is params.sh -I was called as params.sh
My first parameter is : one
My second parameter is : two
</div>
二、那如果还有参数怎么办呢?还要一个个加上来吗?答案是否定的

以下用法应该不陌生,就是直接执行脚本本身,没有附带任何参数,那么脚本讲抛出帮助信息.即怎么使用此脚本.见红字部分
[root@svn shell_example]# sh params_v2.sh
My name is params_v2.sh -I was called as params_v2.sh
I was called with 0 parameters.
Usage: params_v2.sh first second
You provided 0 parameters,but 2 are required.
</div>
代码如下
[root@svn shell_example]# cat params_v2.sh
#!/bin/bash
# 这是个测试脚本传参的测试例子

echo "My name is `basename $0` -I was called as $0"
echo "I was called with $# parameters."

if [ "$#" -eq "2" ];then
    echo "My first parameter is $1"
    echo "My second parameter is $2"
else
    echo "Usage: `basename $0` first second"
    echo "You provided $# parameters,but 2 are required."
fi
</div>

详细的执行过程如下
不传参数执行
[root@svn shell_example]# sh params_v2.sh
My name is params_v2.sh -I was called as params_v2.sh
I was called with 0 parameters.
Usage: params_v2.sh first second
You provided 0 parameters,but 2 are required.
</div>
传递3个参数执行
[root@svn shell_example]# sh params_v2.sh one two three
My name is params_v2.sh -I was called as params_v2.sh
I was called with 3 parameters.
Usage: params_v2.sh first second
You provided 3 parameters,but 2 are required.
</div>
传递2个参数执行
[root@svn shell_example]# sh params_v2.sh one two
My name is params_v2.sh -I was called as params_v2.sh
I was called with 2 parameters.
My first parameter is one
My second parameter is two
</div>

问题来了,要是后期还要加参数怎么办呢?或者我也不确定到底会传几个参数.
解决方法如下,详细执行结果如下
[root@svn shell_example]# cat manyparams.sh
#!/bin/bash
#这是个测试脚本传N个参数的例子

echo "我的名字是 `basename $0` - 我是调用自 $0"
echo "我有 $# 参数"

count=1
while [ "$#" -ge "1" ];do
    echo "参数序号为 $count 是 $1"
    let count=count+1
    shift
done
</div>

一个参数执行

[root@svn shell_example]# sh manyparams.sh one

我的名字是 manyparams.sh - 我是调用自 manyparams.sh
我有 1 参数
参数序号为 1 是 one

5个参数执行

[root@svn shell_example]# sh manyparams.sh one two three four five
</div>

我的名字是 manyparams.sh - 我是调用自 manyparams.sh
我有 5 参数
参数序号为 1 是 one
参数序号为 2 是 two
参数序号为 3 是 three
参数序号为 4 是 four
参数序号为 5 是 five

</div>

您可能想查找下面的文章:

  • Shell脚本传递参数的3种方法比较
  • Shell脚本传参数方法总结
  • Shell脚本中判断输入参数个数的方法
  • Shell最多支持多少个参数?

相关文章

  • Linux用户配置sudo权限(visudo)的方法
  • 实现自动清除日期目录shell脚本实例代码
  • Shell脚本调快调慢系统时间(测试服务器时使用)
  • 获取站点的各类响应时间(dns解析时间,响应时间,传输时间)
  • linux中mysql备份shell脚本代码
  • linux命令scp和sftp详细介绍
  • shell使用mysqld_multi自动做多实例从库脚本
  • 分享20个Unix/Linux 命令技巧
  • linux 随机密码生成工具mkpasswd详解及实例
  • linux find命令之exec简单概述

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • linux 检测远程端口是否打开方法总结
    • Shell脚本编写的八条可靠建议(值得收藏)
    • Shell脚本实现监控kingate并自动启动
    • Linux Shell中三种引号的用法及区别
    • Bash Shell中忽略大小写的设置方法
    • Shell脚本实现自动修改IP、主机名等功能分享
    • 如何调试Linux shell脚本
    • Linux makefile 和shell文件相互调用实例详解
    • Linux下压缩与解压命令详解
    • linux命令scp和sftp详细介绍

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有