linux shell脚本判断文件或文件夹是否存在循环操作

简介: linux shell脚本判断文件或文件夹是否存在循环操作

我最近写的脚本 你改改就能用

3.sh中全是 /data/test 这样的指向目录的数据

#!/bin/bash
#set -x

#循环读取文件目录
for line in `cat /root/shizonghao/3.sh`
do
#判断当前文件是否存在
if [ ! -x "$line" ]; then
#如果不存在记录日志
  echo $line>>test.log
fi
done

#shell判断文件夹是否存在

#如果文件夹不存在,创建文件夹

if [ ! -d “/Top” ]; then

mkdir -p /Topfi

#shell判断文件,目录是否存在或者具有权限

folder="/Top"

file="/Top/test.txt"

-x 参数判断 $folder 是否存在并且是否具有可执行权限

image.png

-d 参数判断 $folder 是否存在

image.png

-f 参数判断 $file 是否存在

image.png

-n 判断一个"变量"是否有值

image.png

判断两个变量的字符串内容是否相同

image.png

说明:判断文件是否存在

myPath="/Top"

myFile="/Top/access.log"


这里的-x 参数判断$myPath是否存在并且是否具有可执行权限

image.png

这里的-d 参数判断$myPath是否存在

image.png

这里的-f参数判断$myFile是否存在

image.png

其他参数还有-n,-n是判断一个变量是否是否有值

image.png

判断$file字符串内容是否是“123123”相同

image.png

相关文章
|
22小时前
|
监控 Shell Linux
Linux的Shell脚本详解
Linux的Shell脚本详解
|
5天前
|
Shell
shell脚本
shell脚本
11 2
|
8天前
|
Linux Shell
蓝易云 - Linux下查看文件和文件夹大小
这两个命令是查看Linux系统中文件和文件夹大小的基本工具,可以根据需要进行组合使用。
9 0
|
12天前
|
Linux C++
Linux C/C++目录和文件的更多操作
Linux C/C++目录和文件的更多操作
|
13天前
|
Ubuntu Java Linux
Linux centos7 ubuntu 一键安装Java JDK 脚本 shell 脚本
Linux centos7 ubuntu 一键安装Java JDK 脚本 shell 脚本
|
13天前
|
Ubuntu Shell Linux
linux shell 后台执行脚本的方法 脚本后台运行 后台运行程
linux shell 后台执行脚本的方法 脚本后台运行 后台运行程
|
13天前
|
监控 Shell Linux
shell linux中用shell写一个占用CPU的脚本
shell linux中用shell写一个占用CPU的脚本
|
1月前
|
Shell
删除常规文件及隐藏文件shell脚本
删除常规文件及隐藏文件shell脚本
62 1
|
Shell Android开发 Apache
常用工具类,文件和内存的大小获取,shell脚本的执行
/* * Copyright (C) 2012 The Android Open Source Project * * Licensed under the Apache License, Version 2.
774 0