shell文件操作基础

1.判断字符串是否数字

if [[ $test != *[!0-9]* ]]; then
    echo "this is a digest"
fi

可参考:
http://mywiki.wooledge.org/BashFAQ/054

2.判断字符串是否是目录或者文件

test -d $file或者[-d $file]
test -f $file或者[-f $file]

例如:

if test -f $file
then
  echo "$file found"
else
  echo "$file not found"
fi

或者

if [-f $file]
then
  echo "$file found"
else
  echo "$file not found"
fi

3.获取路径名中最后的文件名
Bash Shell本身提供了basename命令,可以直接获取路径名最后的文件名,实现代码如下
resFile=basename /root/test/alice/test.log
结果为test.log
直接用这里写代码片

 basename $file

4.获取路径名中目录名
Bash Shell本身提供了dirname命令,特别方便,可以直接获取路径对应的目录名,实现代码如下:

dirPath=dirname /root/test/alice/test.log
结果为 /root/test/alice

使用方法为:

`dirname $file`
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值