040-shell参数$?,$0,$#等
040-shell参数$?,$0,$#等
侠客行之石头 发表于1个月前
040-shell参数$?,$0,$#等
  • 发表于 1个月前
  • 阅读 5
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 十分钟定制你的第一个小程序>>>   

shell脚本中经常会遇到参数

 printf "The complete list is %s\n" "$$"

shell本身的PID(processID)
 printf "The complete list is %s\n" "$!"

Shell 最后运行后台Process 的PID
 printf "The complete list is %s\n" "$?"

上一次运行命令的返回值,正常为0
 printf "The complete list is %s\n" "$*"

所有参数
 printf "The complete list is %s\n" "$@"

所有参数列表。如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。
 printf "The complete list is %s\n" "$#"

添加shell参数的个数
 printf "The complete list is %s\n" "$0"

shell本身的文件名
 printf "The complete list is %s\n" "$1""$2"

第一个参数,第二个参数
 

[root@cnsz142728 scripts]# vim parameter.sh
  1 #!/bin/bash
  2  printf "The complete list is %s\n" "$$"
  3  printf "The complete list is %s\n" "$!"
  4  printf "The complete list is %s\n" "$?"
  5  printf "The complete list is %s\n" "$*"
  6  printf "The complete list is %s\n" "$@"
  7  printf "The complete list is %s\n" "$#"
  8  printf "The complete list is %s\n" "$0"
  9  printf "The complete list is %s\n" "$1"
 10  printf "The complete list is %s\n" "$2"

"parameter.sh" 10L, 381C written                                                                                                                                                                                          
[root@cnsz142728 scripts]# ./parameter.sh 123 45
The complete list is 30763
The complete list is 
The complete list is 0
The complete list is 123 45
The complete list is 123
The complete list is 45
The complete list is 2
The complete list is ./parameter.sh
The complete list is 123
The complete list is 45

对照着来,就明白各个参数的含义。

共有 人打赏支持
粉丝 3
博文 65
码字总数 20932
×
侠客行之石头
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: