SHELL 脚本命令总结(一)

1. source命令

直接运行和source运行的区别,比如cd命令,如果直接运行,就达不到效果,还用source运行就能达到效果。

source会在当前shell环境中执行,如果直接执行,会开启一个子shell,在子shell中进入了该目录,脚本执行完后会退出子shell回到父shell环境

2. if语句

判断文件夹是否存在:

myPath="/var/log/httpd/"
if [ ! -d "$myPath"]; then
mkdir "$myPath"
elif [ "$1"x = "projectx"]; then
echo "build $1"
else
exit
fi
摘自: http://blog.csai.cn/user1/14572/archives/2007/12090.html

3. 时间函数

按当前时间创建文件夹

da=`date +%y%m%d%H%M`
mkdir "dir$da"

4. 传递参数

  #!/bin/sh   
    
  if   [   $#   !=   2   ]; then   
          echo   "Usage:   test.sh   input1   input2"   
  else   
          echo   "Your   input   is:"   $1   $2   
  fi  

  这里$#是参数个数,   $1   $2   就是参数啦  

5. 文件操作

   判断文件是否存在

#!/bin/bash
filename=/home/jifeng/shell/file
if [ -e $filename ]
then
 echo "$filename exited"
fi
     判断文件是否为空
#!/bin/bash
filename=/home/jifeng/shell/file
echo $filename
if [[ ! -s $filename ]]
then
 echo "file is null"
else
 echo "file is not null"
fi

 更详细的文件操作

   遍历一个目录下的所有文件

#!/bin/bash
readpath="/home/jifeng/AndesProject"
for file in $readpath/*
do
       echo "$file"
done

参考:

http://wiki.ubuntu.org.cn/Shell编程基础

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值