Shell脚本中的位置变量参数(特殊字符)实例讲解

2019/7/10 21:46:52

本文主要是介绍Shell脚本中的位置变量参数(特殊字符)实例讲解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

$# : 传递到脚本的参数个数
$* : 以一个单字符串显示所有向脚本传递的参数。与位置变量不同,此选项参数可超过 9个
$$ : 脚本运行的当前进程 ID号
$! : 后台运行的最后一个进程的进程 ID号
$@ : 与$#相同,但是使用时加引号,并在引号中返回每个参数
$- : 显示shell使用的当前选项,与 set命令功能相同
$? : 显示最后命令的退出状态。 0表示没有错误,其他任何值表明有错误。

复制代码 代码如下:

#!/bin/sh
#param.sh

# $0:文件完整路径名
echo "path of script : $0"
# 利用basename命令文件路径获取文件名
echo "name of script : $(basename $0)"
# $1:参数1
echo "parameter 1 : $1"
# $2:参数2
echo "parameter 2 : $2"
# $3:参数3
echo "parameter 3 : $3"
# $4:参数4
echo "parameter 4 : $4"
# $5:参数5
echo "parameter 5 : $5"
# $#:传递到脚本的参数个数
echo "The number of arguments passed : $#"
# $*:显示所有参数内容i
echo "Show all arguments : $*"
# $:脚本当前运行的ID号
echo "Process ID : $"
# $?:回传码
echo "errors : $?"

输入./param.sh hello world

复制代码 代码如下:

[firefox@fire Shell]$ ./param.sh hello world
path of script : ./param.sh
name of script : param.sh
parameter 1 : hello
parameter 2 : world
parameter 3 :
parameter 4 :
parameter 5 :
The number of arguments passed : 2
Show all arguments : hello world
Process ID : 5181
errors : 0


这篇关于Shell脚本中的位置变量参数(特殊字符)实例讲解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


暂无数据...
扫一扫关注最新编程教程