`
womendu
  • 浏览: 1478343 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

shell脚本的参数问题

 
阅读更多

一个简单例子

para.sh
########################
#!/bin/sh
#scriptname:para

echo "this script is call $0"
echo "$0 $1 $2"
echo "The number of positionalparameters is $#"

echo "$* is the dollar star"

echo "$@ is the dollar at"

echo " "$*" is the dollar star which has quoation marks"

echo " "$@" is the dollar at which has quoation marks"
################
[root@yqrh5u2]chmod +x para.sh
[root@yqrh5u2 rbf]# ./para.sh 1st_name 2nd_para 3rd_last
this script is call ./para.sh
./para.sh 1st_name 2nd_para
The number of positionalparameters is 3
1st_name 2nd_para 3rd_last is the dollar star
1st_name 2nd_para 3rd_last is the dollar at
1st_name 2nd_para 3rd_last is the dollar star which has quoation marks
1st_name 2nd_para 3rd_last is the dollar at which has quoation marks




Positional Parameter What It References

$0 References the name of the script,脚本的名称

$# Holds the value of the number of positional parameters,参数的个数

$* Lists all of the positional parameters,列出所有的参数

$@ Means the same as $*, except when enclosed in double quotes

"$*" Expands to a single argument (e.g., "$1 $2 $3"),

"$@" Expands to separate arguments (e.g., "$1" "$2" "$3")

$1 .. $9 References up to nine positional parameters,应用对应的参数


$* and $@ 区别,只有单他们有双引号是才有区别:
$ set 'apple pie' pears peaches

6 $ for i in $@
> do
> echo $i
> done

apple
pie
pears
peaches



7 $ set 'apple pie' pears peaches
8 $ for i in "$@" # At last!!
> do
> echo $i
> done

apple pie
pears
peaches



脚本的参数至少是1,
name=${1:?"requires an argument" }

echo Hello $name
:? 将会检验$1是否有值,加入没有,脚本退出,并且报错误信息。

分享到:
评论

相关推荐

    解决Unity不能调用shell脚本传递参数的问题

    解决Unity不能调用shell脚本传递参数的问题

    输出执行操作和打印日志的shell脚本实例

    cat /mnt/log_function.sh #!/bin/bash #log function ####log_correct函数打印正确的输出到日志文件 function log_correct () { DATE=`date “+%Y-%m-%d %H:%M:%S”` ####显示打印日志的时间...log_error打印shell脚本

    Linux shell 脚本 markdown文本编辑

    有关几个shell脚本的编写,如:编写一个shell脚本程序,它带一个命令行参数,这个参数是一个文件。如果这个文件是一个普通文件,则打印文件所有者的名字和最后的修改日期...加入了自己的注释理解

    shell脚本命令行参数简介

    之所以用到命令行参数,关键在于shell脚本需要与运行脚本的人员进行交互。bash shell提供了命令行参数添加在命令后面的数据值)、命令行选项修改命令行为的单字符值)和直接读取键盘输入。 1、命令行参数向shell脚本...

    Shell脚本专家指南

    《Shell脚本专家指南》面向中高级的shell程序员,以及需要解决日常问题的系统管理员,但假定读者能够读懂一般的shell代码。 编辑推荐 《Shell脚本专家指南》是我多年从事系统管理员一职所积累的非常有用的脚本和技术...

    java调用shell(包含有参数的调用)

    java调用shell 包含简单的编写shell文件 调用含有参数的shell文件

    Shell脚本中让进程休眠的方法(sleep用法)

    有时候写Shell的脚本,用于顺序执行一系列的程序。 有些程序在停止之后并没能立即退出,就例如有一个 tomcat 挂了,就算是用 kill -9 命令也还没瞬间就结束掉。 这么如果 shell 还没等其退出就接着执行下一行,这么...

    Linux shell脚本编写基础

    Linux shell脚本编写基础 在进行linux测试时编写脚本是必不可少的,Shell脚本的名称可以随便定义,也不要什么后缀名, 例如可以写abc,smartzip这类名称,运行时只要键入 ./smartzip就能运行脚本了。。 每行命令开头...

    Shell脚本编程详解

    Shell脚本编程详解  Shell命令行的运行  编写、修改权限和执行Shell程序的步骤  在Shell程序中使用参数和变量  表达式比较、循环结构语句和条件结构语句  在Shell程序中使用函数和调用其他Shell程序

    Shell脚本学习笔记

    自己整理的Shell脚本学习笔记,脚本实例可直接运行:1. 变量和字符串;2. 传递参数;3. 基本运算符;4. echo&printf;&test;;5. 流程控制;6. 函数;7. 输入输出重定向&文件嵌入

    shell脚本修改json中某个字段的值

    shell脚本修改json中某个字段的值 - 思路:通过awk来找到旧数据,然后用sed来替换旧数据

    shell执行mysql脚本

    在shell脚本中向SQL文件传递参数,并且在shell脚本中执行SQL文件。

    中北大学操作系统云沙箱实验报告-使用Shell脚本实现局域网MAC地址收集和FTP服务监控

    使用Shell脚本对局域网中的所有主机进行MAC地址的收集,便于日常的维护管理。同时对局域网中部署FTP服务的主机进行监控,能够快速得知哪台具有FTP的主机在提供服务。下图该局域网的拓扑图。 图1局域网拓扑 实现MAC...

    shell脚本学习手册

    1、shell脚本 3 2、shell脚本实践 4 Shell变量 4 1、使用变量 5 2、只读变量 5 3、删除变量 5 4、变量类型 6 5、shell字符串 6 6、Shell数组 7 7、Shell注释 9 Shell传递参数 9 1、实例 9 Shell数组 11 Shell运算符 ...

    使用runtime实现linux命令行或shell脚本多次调用

    使用runtime, 实现java客户端多次调用linux命令或者shell脚本,并返回对应结果或信息

    linux下zip打包shell脚本

    linux下zip打包shell脚本,可根据具体的内容自己给定参数

    shell 查找指定名称的文件 脚本

    shell查找指定名称的文件脚本,参数见脚本注释

    shell脚本程序设计.doc

    Linux操作系统实验三,shell脚本程序设计实验报告,本实验包括两具体的shell程序编写任务。

    Shell脚本通过参数名传递参数的实现代码

    主要介绍了Shell脚本通过参数名传递参数的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    shell编程 创建shell脚本 shell 基础语法 shell流程控制语法 shell函数

    shell编程 创建shell脚本 ①创建第一个shell脚本②运行shell脚本shell 基础语法 ①变量②变量分类与数组③shell传递参数④基本运算符shell流程控制语法 ①if else条件判断②for循环③while循环④until循环⑤case语句...

Global site tag (gtag.js) - Google Analytics