向bash shell脚本传参

例子:

powershell 复制代码
~ script % touch parameter.sh
~ script % chmod 755 parameter.sh
~ % vim parameter.sh

parameter.sh:

powershell 复制代码
#!/usr/bin/env bash

echo the name of current script is $0

echo the first parameter is $1

echo the second parameter is $2

echo all parameters: $@

exit 0 

执行:

powershell 复制代码
script % ./parameter.sh a b
the name of current script is ./parameter.sh
the first parameter is a
the second parameter is b
all parameters: a b
  • $0 带全路径的脚本名称
  • $1 第1个参数
  • $2 第2个参数
  • $3 第3个参数
    ...
  • ${10} 第10个参数
    ...
  • ${255} 第255个参数
    最多可以有255个参数
  • @: 获取所有参数,除了脚本名称,即@等于1\~255的参数的集合

我们可以通过查询$?了解脚本程序退出的状态。因为上面的脚本加了exit 0表示退出时的状态是0,一般来说,当程序出现异常导致退出时,状态值是个非0的整数。 如果我们加上这一句exit 10,执行完脚本后,再查询一个退出状态,就会得到10,查询的方式:

powershell 复制代码
~ script % echo $?
10
相关推荐
有谁看见我的剑了?7 小时前
linux 添加硬盘后系统识别不到硬盘处理
linux·运维·服务器
九转成圣8 小时前
Java 性能优化实战:如何将海量扁平数据高效转化为类目字典树?
java·开发语言·json
SmartRadio8 小时前
ESP32-S3 双模式切换实现:兼顾手机_路由器连接与WiFi长距离通信
开发语言·网络·智能手机·esp32·长距离wifi
laowangpython8 小时前
Rust 入门:GitHub 热门内存安全编程语言
开发语言·其他·rust·github
我叫汪枫8 小时前
在后台管理系统中,如何递归和选择保留的思路来过滤菜单
开发语言·javascript·node.js·ecmascript
_.Switch8 小时前
东方财富股票数据JS逆向:secids字段和AES加密实战
开发语言·前端·javascript·网络·爬虫·python·ecmascript
软件技术NINI8 小时前
webkit简介及工作流程
开发语言·前端·javascript·udp·ecmascript·webkit·yarn
Brendan_0018 小时前
JavaScript的Stomp.over
开发语言·javascript·ecmascript
念2348 小时前
f5 shape分析
开发语言·javascript·ecmascript
yc_12248 小时前
用 Visual Studio 远程调试 Linux:从零到流畅的完整指南
linux·ide·visual studio