检查天气路径是Shell脚本中的目录或文件

时间:2016-09-02 11:54:49

标签: shell sh

我试图找出数组项是文件路径还是目录路径。

#!/bin/bash

declare -a arr=("/var/log/symantec/sgs-td","/var/lib/mysql/mysql-slow.log","/var/lib/mysql/mysql-error.log", "/var/log/sa")

## now loop through the above array
for i in "${arr[@]}"
do
if [[ -d $i ]]; then
    echo "$i is a directory"
elif [[ -f $i ]]; then
    echo "$i is a file"
else
    echo "$i is not valid"
    exit 1
fi
done

我不了解shell语法。

如果路径是目录,则打印..是目录或文件然后打印其文件。

1 个答案:

答案 0 :(得分:0)

最大的小错误是我在声明时将逗号放在数组列表中。休息代码没有问题,它工作正常。

#!/bin/bash

declare -a arr=("/char/vlog/tec/sg" "/chr/lib/mysql/mysq.log" "/dar/lib/mysql/error.log" "/char/log/sua")

## now loop through the above array
for i in "${arr[@]}"
do
if [[ -d $i ]]; then
    echo "$i is a directory"
elif [[ -f $i ]]; then
    echo "$i is a file"
else
    echo "$i is not valid"
    exit 1
fi
done