shell脚本中如何获取命令的参数(1)
2014-04-19 18:37
579 查看
最近在学习shell脚本编程,了解到在脚本当中经常需要获取用户的给定的参数,例如在终端环境下自己敲命令的时候通常也得跟一些参数在后面,现在也向大家介绍一下我在资料上学到的方式。
shell脚本中的有许多默认的参数,需要读者朋友注意和了解的
$# :参数的个数
$* :参数的全部数据当作一个单词处理
$@ :将命令行中提供的所有参数作为同一个字符串中的多个单词处理,允许进行迭代
$n :其中n表示参数列表当中的第几个参数
现在通过一个综合实例展示一下:
#!/bin/bash
# test -------$#
echo "You have input $# parameters"
# test --------$n which will display the name of shell script
echo "The name of command is $0"
# test -------$@ will display the whole parameter
echo 'the value of $@ as follows'
for parameter in "$@"
do
echo "The parameter is :$parameter"
done
# test ------$*
echo ' the vaule of $* as follows'
for parameter in "$*"
do
echo "The patameter is :$parameter"
done
shift在命令参数的获取有着很大的作用,shift命令能够改变命令行参数的相对位置,使用shift命令时,默认将每个参数变量左移一个位置,这个适用与参数不明确的场合,下面是实例;
上面介绍在shell脚本当中的编程便来那个获取方式,现在重点讨论如何对变量选项进行人性化的处理,下面讨论进阶一点的知识,见详解(2)
shell脚本中的有许多默认的参数,需要读者朋友注意和了解的
$# :参数的个数
$* :参数的全部数据当作一个单词处理
$@ :将命令行中提供的所有参数作为同一个字符串中的多个单词处理,允许进行迭代
$n :其中n表示参数列表当中的第几个参数
现在通过一个综合实例展示一下:
#!/bin/bash
# test -------$#
echo "You have input $# parameters"
# test --------$n which will display the name of shell script
echo "The name of command is $0"
# test -------$@ will display the whole parameter
echo 'the value of $@ as follows'
for parameter in "$@"
do
echo "The parameter is :$parameter"
done
# test ------$*
echo ' the vaule of $* as follows'
for parameter in "$*"
do
echo "The patameter is :$parameter"
done
shift在命令参数的获取有着很大的作用,shift命令能够改变命令行参数的相对位置,使用shift命令时,默认将每个参数变量左移一个位置,这个适用与参数不明确的场合,下面是实例;
上面介绍在shell脚本当中的编程便来那个获取方式,现在重点讨论如何对变量选项进行人性化的处理,下面讨论进阶一点的知识,见详解(2)
相关文章推荐
- linux shell时间戳转日期字符串
- Hadoop中HDFS文件系统下的Shell操作
- shell写的算术乘法表
- PowerShell3.0入门视频(由Jeffrey Snover和Jason Helmick主讲)
- shell脚本(一键安装mysql)
- Hbase shell 命令详解
- shell 数组
- grep的基本用法
- 生成包含随机字符串的文件的脚本
- 超棒的PowerShell内建的帮助系统
- shelll Test
- shell程序之逐行读取一文件中的参数且使用此参数每次执行5分钟
- Linux shell 用法和技巧
- OD: Universal Shellcode
- Shell编程基础
- coding--shell脚本、c语言函数参数列表
- HBase sh 4000 ell 简单使用
- linux远程管理的工具xshell和xftp
- Linux Shell - Trying to split a string into two variables
- How do I grab an INI value within a Linux shell script?