bash:从配置中创建存在和不存在的文件列表
如何从包含的文件存在或不存在的源创建两个变量 例如,在Apache配置中,可以包含文件,但这些文件可能不存在 Apachebash:从配置中创建存在和不存在的文件列表,bash,Bash,如何从包含的文件存在或不存在的源创建两个变量 例如,在Apache配置中,可以包含文件,但这些文件可能不存在 Apachehttpd.confsnippet: Include /var/site/conf/apache/1.conf Include /var/site/conf/apache/2.conf Include /var/site/conf/apache/3.conf Include /var/site/conf/apache/4.conf 在上面的include示例中,/var/s
httpd.conf
snippet:
Include /var/site/conf/apache/1.conf
Include /var/site/conf/apache/2.conf
Include /var/site/conf/apache/3.conf
Include /var/site/conf/apache/4.conf
在上面的include示例中,/var/site/conf/apache/4.conf
不存在
在审核Apache配置时,如何列出不存在的include并继续处理存在的配置文件
到目前为止,我已经得到了下面的代码,但我缺少了一些可以使这项工作正常进行的东西:
\u Apache\u includes=$(cat/etc/httpd/conf/httpd.conf | grep unique|u site|u config | cut-f2-d“”)
如果测试-f“$\u Apache\u includes”;然后
$\u Apache\u includes=$\u Apache\u是否存在
其他的
$\u Apache\u includes=$\u Apache\u不存在
fi
echo“$\u Apache\u是否存在”
echo“$\u Apache\u不存在\u不存在”
对于_Config_file_check in$_Apache_includes;做
_Config_domains=$(ls-la$_Apache_确实存在| grep-e ServerName-e ServerAlias | grep-v“#”| sed“s/^[\t]*/“| cut-f2-d”“| grep-v^$| sort-u)
_Config_ip=$(getent主机$_Config_域| awk'{print$1}')
echo“Config File:$\u Config\u File”-$\u Config\u domains-$\u Config\u ip>>/root/server fixes/audit/domains/domain\u list\u$\ u date\u now.txt
回响
完成
预期产出:
缺少的文件列表包括:
/var/site/conf/apache/4.conf
正确配置的域列表:
配置文件:/var/unique\u site\u Config/conf/apache/1.conf-example.com-93.184.216.34
配置文件:/var/unique\u site\u Config/conf/apache/1.conf-www.example.com-93.184.216.34
配置文件:/var/unique\u site\u Config/conf/apache/2.conf-example2.com-173.231.210.103
配置文件:/var/unique\u site\u Config/conf/apache/3.conf-example3.com-23.20.239.12您需要逐行单独处理要测试的文件
ls -1 |\
while IFS='\n' read -r file;
do if [ -f "${file}" ];
then # process the ${file} or write to a tempfile
else echo "${file} does not exist";
fi; done
对于此测试文件
$ cat file
Include 1.conf
Include 2.conf
Include 3.conf
Include 4.conf
这将显示哪些配置存在,哪些不存在
confs=$(grep Include file) # get confs to var
confs=${confs//Include/} # remove 'Include'
# loop through confs and check if they exist
for conf in $confs; { [[ -e $conf ]] && echo "$conf exist" || echo "$conf not exist"; }
和创建不存在配置的列表
for conf in $confs; { [[ -e $conf ]] && exist+="$conf " || not_exist+="$conf "; }
printf "exist: $exist\nnot_exist: $not_exist"
代码:
输出:
Files that are missing:
/private/etc/apache2/other/*.conf
/private/etc/apache2/extra/httpd-manual.conf
/private/etc/apache2/extra/httpd-userdir.conf
/private/etc/apache2/extra/httpd-languages.conf
/private/etc/apache2/extra/httpd-autoindex.conf
/private/etc/apache2/extra/httpd-mpm.conf
grep
匹配第一个单词为“Include”的行
sed
匹配从行中第一个“\”开始到字符串末尾的字符串
echo-e
确保\n
将被解释为换行符apachectl configtest
不是您要搜索的内容<代码>继续处理配置文件吗?“处理”是什么意思?你是如何处理的?@KamilCuk我正在审核系统管理员的配置。我想谈谈配置问题。apachectl configtest不会抱怨缺少配置。您可以将所有这些$(cat/etc/httpd/conf/httpd.conf | grep unique_site_config | cut-f2-d“”)替换为$(awk'/unique_site_config/{print$2}“/etc/httpd/conf/httpd.conf
ls-la$\u Apache\u是否存在| grep-e ServerName
-你的意思是grep-e ServerName$\u Apache\u是否存在
?你的脚本有很多错误和bug。在这里发布之前请检查它。脚本与你的问题有何关联?你想列出其中的内容,但你的脚本需要检查ServerName和独特的站点配置。如果您对包含感兴趣,为什么脚本中没有grep Include
?请发布您期望的示例输出。例如,对于您提供的输入,包含Include的4行,您只想打印/var/site/conf/apache/4.conf之类的内容,但它不存在
对吗?1.存在
是吗在子shell中分配,因此它将不存在于子shell之外。2.“\n”
不是换行符。3.4.读取文件
将使检查失败,如果文件名中有前导空格或尾随空格,或者文件名中有任何\slash。第1点和第2点仍然有效。在读取文件时不能执行echo 123;do exists=“$file”;完成;echo$exists
-变量exists
将为空,echo$exists
将打印空行。
在子shell中运行。使用IFS='\n'
将删除文件名中的前导和尾随新行。“\n”
将被解析为n
@KamilCuk我认为OP只想处理存在的文件,而不是将它们存储在新列表中,如果他不想,他也可以将其保存到文件中。ls-1
就是一个例子,我不会复制OP的所有代码。我修改了while-read循环,现在只有带换行符的文件名将失败。@KamilCuk我刚才做了seq 10 |读取行;do test=“${test}${line}”;完成;在一个新的shell上回显“${test}”
。我也做了echo“test”;而IFS='\n'读取-r行;do echo“>${line}这很奇怪。第一个代码段为我打印了空行。谢谢你的建议。我可以使用它,只需稍加修改就可以完全完成我想要的。理解我的问题真是太好了!有两个变量:$exists,其中包含存在的文件,$missing,其中包含存在的文件missing@DaniedeJagerY你不能投票,但你应该能够接受答案,不是吗?如果不能,请原谅。
Files that are missing:
/private/etc/apache2/other/*.conf
/private/etc/apache2/extra/httpd-manual.conf
/private/etc/apache2/extra/httpd-userdir.conf
/private/etc/apache2/extra/httpd-languages.conf
/private/etc/apache2/extra/httpd-autoindex.conf
/private/etc/apache2/extra/httpd-mpm.conf