shell文件操作基础

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zxl1033394132/article/details/54861863

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`

猜你喜欢

转载自blog.csdn.net/zxl1033394132/article/details/54861863