当前位置: > shell编程 >

linux shell 编程4 判断当前的是不是目录、 传递参数的个数

时间:2016-06-12 23:34来源:linux.it.net.cn 作者:IT

文章主要用到了$() 反引号`` 以及 $1,$2 $0 ,$# 等这些特殊符号

1、

 

  1. #!/bin/sh  
  2. #name : test linux shell  
  3. # 通过pwd 命令获取路径,然后在判断是不是目录,  
  4. # 下面的这个命令可以写成两个反引号`` 和$() 是同样的功能  
  5. File=$(pwd)   
  6. echo $File  
  7. if [ "$File" = '/' ]  
  8. then  
  9. echo " this is  directory "  
  10. else  
  11. echo   " this is  file  or  link "  
  12. echo "out  of  this  process"  
  13. exit 1  
  14. fi  

2、
  1. # 判断获取的参数个数  
  2. #!/bin/sh  
  3. #name : test linux shell  
  4. name=$1  
  5. age=$2  
  6. count=$#  
  7. if [ $# -lt 2 ]  
  8. then  
  9. echo "usage: `basename $0` arg1 arg2 arg3 " >&2  
  10. exit 1  
  11. else  
  12. echo "arg1 $1 arg2 $2  and  total $#  and name is $0"  
  13. fi  

 

注:

1、这里的$1 $2  就是获取参数的指定位置,$0 就是文件的名称 $# 是参数的总数

2、  其中的 $()  和 反引号 `` 就是执行括号中命令。

3、 参数怎么传那,就是在执行代码的时候进行传递 : sudo  sh  tests.sh "whd" 12  这样就传递了两个参数




(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容