向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
相关推荐
哈基咪怎么可能是AI1 小时前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行18 小时前
Linux和window共享文件夹
linux
木心月转码ing1 天前
WSL+Cpp开发环境配置
linux
崔小汤呀2 天前
最全的docker安装笔记,包含CentOS和Ubuntu
linux·后端
何中应2 天前
vi编辑器使用
linux·后端·操作系统
何中应2 天前
Linux进程无法被kill
linux·后端·操作系统
何中应2 天前
rm-rf /命令操作介绍
linux·后端·操作系统
何中应2 天前
Linux常用命令
linux·操作系统
葛立国2 天前
从 / 和 /dev 说起:Linux 文件系统与挂载点一文理清
linux
郑州光合科技余经理3 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php