【问题标题】:Check folders & inside files are exists via shell script通过 shell 脚本检查文件夹和内部文件是否存在
【发布时间】:2016-05-14 00:58:49
【问题描述】:

通过 shell 脚本检查文件夹和内部文件是否存在

这是我用来检查文件夹以及其中是否包含文件的脚本。但它不采用文件名。请帮我解决问题。

 #!/bin/bash

 folder1="/root/scripts/1/"
 folder2="/root/scripts/2/"
 folder3="/root/scripts/3/"
 file1=start.sh
 file2=stop.sh

 CHKFOLDERS="folder1,folder2,folder3"
 CHKFILES="file1,file2"

# Check all passed path & programs are exist or not with needed permission like execute.

for fol in $(echo $CHKFOLDERS | sed "s/,/ /g"); do
 if [ ! -d "$fol" ]; then
  echo -e " $fol folder are not correct please check."
  exit 1
 fi

 for infile in $(echo $CHKFILES | sed "s/,/ /g"); do
  if [ ! -x "$((fol))/${fol}_${infile}" ]; then
    echo -e " \"${fol}_${infile}\" script is not correct or not executable please ckeck this."
        exit 2
        fi
      done
done

在本例中,命名约定如下:

文件夹名称:文件夹1、文件夹2、文件夹3

文件名:folder1_file1、folder2_file2。

【问题讨论】:

    标签: linux bash shell


    【解决方案1】:

    第一个问题是

    CHKFOLDERS="folder1,folder2,folder3"
    

    改变它
    CHKFOLDERS="$folder1,$folder2,$folder3"
    

    第二个是 if:

    if [ ! -x "$((fol))/${fol}_${infile}" ]; then
    

    改变它:

    if [ ! -x "${fol}/${infile}" ]; then
    

    也改变回声

    echo -e " \"${fol}_${infile}\" script is not correct or not executable please ckeck this."
    

    与 echo -e " \"${fol}/${infile}\" 脚本不正确或无法执行,请检查一下。"

    另外最好更改文件夹声明

    folder1="/root/scripts/1/"
    

    folder1="/root/scripts/1"
    

    这是我修改后的脚本:

    #!/bin/bash
    
     folder1="/tmp/scripts/1"
     folder2="/tmp/scripts/2"
     folder3="/tmp/scripts/3"
    
    
    
    
    file1=start.sh
     file2=stop.sh
    
     CHKFOLDERS="$folder1,$folder2,$folder3"
     CHKFILES="file1,file2"
    
    # Check all passed path & programs are exist or not with needed permission like execute.
    
    for fol in $(echo $CHKFOLDERS | sed "s/,/ /g"); do
     echo "folder[$fol]"
     if [ ! -d "$fol" ]; then
      echo -e " $fol folder are not correct please check."
      exit 1
     fi
    
     for infile in $(echo $CHKFILES | sed "s/,/ /g"); do
       echo "infile[$infile]"
      if [ ! -x "${fol}/${infile}" ]; then
        echo -e " \"${fol}/${infile}\" script is not correct or not executable please ckeck this."
            exit 2
            fi
          done
    done
    

    输出:

    sh-4.3$scriptTest.sh                                                                                                                                                                                                                            
    folder[/tmp/scripts/1]                                                                                                                                                                                                                                 
    infile[file1]                                                                                                                                                                                                                                          
    "/tmp/scripts/1/file1" script is not correct or not executable please ckeck   this.                                                                                                                                                                      
    

    【讨论】:

    • 感谢 Claudio,在您的示例文件名中获取为“file1”,但我需要将文件获取为:start.sh 和 stop.sh。我们可以使用 $file1,$file2 来代替
    • 是的,你可以用 CHKFILES="$file1,$file2" 替换 CHKFILES="file1,file2"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 2017-09-11
    • 2020-02-03
    相关资源
    最近更新 更多