检查文件夹&内部文件通过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
在此示例中,命名约定如下:
文件夹名称:folder1,folder2,folder3
文件名:folder1_file1,folder2_file2。
答案 0 :(得分:0)
第一个问题是
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.