我编写了一个脚本,允许受限用户访问删除生产网络服务器上的文件。但是,为了防止导致意外文件系统删除/问题的胖指问题,我已经将基本目录硬编码在一个变量中......但最终结果是没有正确地从硬编码目录+用户路径创建所需的路径,如果他们有一个 * 通配符...
我有一个 Apache 2.4.6 服务器,可以为用户缓存 Web 内容。他们有一个 jailkit 用户可以通过 SSH 进入这个盒子。由于这是生产,他们的访问受到严格限制,但是,我想让他们能够按照自己的条件清除特定的缓存目录。为了防止出现严重错误,我将基本缓存目录硬编码到脚本变量中,这样无论如何,脚本都只会针对该路径运行。
到目前为止,此脚本可以很好地遍历所需的缓存清除路径...用户创建一个 .txt 文件,每行定义一个/cachePath,脚本将遍历它并删除这些路径。它适用于/path 和/content/path2/... 但我终生无法使用通配符(即/path/、/content/path2/)。可能有比我到目前为止所做的更性感的方式来处理这个问题(目前有一个 if | else 语句来处理 * 或/* 不包括在下面的脚本中),但我正在尝试处理各种不想要的结果用户在自定义路径上输入的 * 或/*。
#!/bin/bash
#For this to work, a user must create a paths.txt file in their jailed home directory, based off the /mnt/var/www/html cache location. Each location (or file) must be on a new line, and start with a /
#User-created file with custom cache directories to delete
file="/usr/jail/paths.txt"
#Setting this variable to the contents of the user-created cache file
pathToDelete=$(cat $file)
#Hard-coded cache directory to try to prevent deleting anything important outside cache directory
cacheDir="/mnt/var/www/html"
#Let's delete cache
if [ -f $file ];then
echo "Deleting the following cache directories:"
for paths in $pathToDelete
do
echo $cacheDir"$paths"
#rm command commented out until I get expected echo
output
#rm -rfv $cacheDir"$paths"
done
echo "Cache cleared successfully"
mv $file "$file.`date +"%m%d%Y%H%M"`"
else
echo "Nothing to do"
fi
我尝试过使用双引号、单引号、不使用引号,尝试将“pathToDelete”视为数组,但都没有产生所需的输出。例如,如果 paths.txt 只包含“*”,结果是抓取/下的所有目录并将它们添加到“cacheDir”?
/mnt/var/www/html/testing/backup
/mnt/var/www/html/testing/bin
/mnt/var/www/html/testing/boot
/mnt/var/www/html/testing/data
/mnt/var/www/html/testing/dev
/mnt/var/www/html/testing/etc
/mnt/var/www/html/testing/home
/mnt/var/www/html/testing/lib
/mnt/var/www/html/testing/lib64
...
如果 paths.txt 是“./*”,它会从脚本本身的位置添加文件:
/mnt/var/www/html/testing./cacheClear.sh
/mnt/var/www/html/testing./paths.txt
最终,我正在寻找的是:如果/mnt/var/www/html 包含以下目录:
/content/
/content/path/
/content/path/file1.txt
/content/path/file2.txt
/content/path/subdir/
/path2/
/path2/fileA.txt
/path2/fileB.txt
然后一个文件包含
/内容/路径/*
应删除/content/path/file1.txt、file2.txt 和/subdir/,并保留/content/path/目录。
如果paths.txt文件包含
/content/path
/path2/*
然后/content/path 目录和子文件/目录应该被删除,/path2/目录中的文件也将被删除......但是现在,脚本没有看到连接的 $cacheDir
+ $paths
作为真实/预期位置,如果它在其中的任何地方包含 *。没有 * 符号也能正常工作。
最佳答案
找到一个对我的目的来说足够好的版本:
#!/bin/bash
file="/usr/jail/paths.txt"
pathToDelete=$(cat $file)
cacheDir="/mnt/var/www/html"
if [ -f $file ]; then
if [ "$pathToDelete" == "*" ] || [ "$pathToDelete" == "/*" ]; then
echo "Full Clear"
rm -rfv /mnt/var/www/html/*
else
echo "Deleting the following cache directories:"
for i in ${pathToDelete};
do
echo ${cacheDir}${i}
rm -rfv ${cacheDir}${i}
done
echo "Cache cleared successfully"
fi
fi
关于linux - 连接硬编码目录和用户创建的文本文件会在不应该的时候添加根级路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58595402/