【问题标题】:Check weather the path is directory or file in Shell script检查天气路径是Shell脚本中的目录或文件
【发布时间】:2016-09-02 11:54:49
【问题描述】:

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

#!/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 语法。

如果路径是目录,则打印 .. 是目录,如果是文件,则打印文件。

【问题讨论】:

  • 你的问题是什么?
  • 运行脚本时得到的实际输出是什么?
  • /var/log/symantec/sgs-td,/var/lib/mysql/mysql-slow.log,/var/lib/mysql/mysql-error.log, 无效跨度>

标签: shell sh


【解决方案1】:

最大的小错误是我在声明时将逗号放在数组列表中。 Rest Code 没有问题,运行良好。

#!/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

【讨论】:

    猜你喜欢
    • 2011-06-15
    • 2010-09-10
    • 2013-03-15
    • 2015-01-08
    • 2017-01-12
    • 2018-12-18
    • 1970-01-01
    • 2013-12-10
    • 2012-09-08
    相关资源
    最近更新 更多