英文:
Check some settings in debian/ubuntu and the output should be similar to testssh
问题
我有一个检查Debian/Ubuntu中一些设置的脚本,例如:
grub=$(stat /boot/grub/grub.cfg)
if [[ $grub = "(0444/-r--r--r--)" ]]
then
echo "Access: (0444/-r--r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 已设置在 stat /boot/grub/grub.cfg。"
else
echo "错误:Access: (0444/-r--r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 在 stat /boot/grub/grub.cfg 上未设置。"
stat /boot/grub/grub.cfg
fi
user=$(stat /boot/grub/user.cfg)
if [[ $user = "(0444/-r--r--r--)" ]]
then
echo "Access: (0444/-r--r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 已设置在 stat /boot/grub/user.cfg。"
else
echo "错误:Access: (0444/-r--r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 在 stat /boot/grub/user.cfg 上未设置。"
stat /boot/grub/user.cfg
fi
并且我希望这个脚本只显示“错误/访问”而不显示“stat /boot/grub/grub.cfg”的命令输出,比如“文件或文件夹不存在”或类似的内容。
就好像命令在后台运行,只有当它正常或不正常时才显示给我。与testssh.sh一样。
[点击这里查看图像描述](https://i.stack.imgur.com/1zN23.png)
没有“stat: cannot statx '/boot/grub/user.cfg': No such file or directory”只显示“错误:Access: (0444/-r--r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 在 stat /boot/grub/user.cfg 上未设置。”。
英文:
I have a script that checks some settings in debian/ubuntu, for example:
grub=$(stat /boot/grub/grub.cfg)
if [[ $grub = *"(0444/-r--r--r--)"* ]]
then
echo "Access: (0444/-r--r--r--) Uid: ( 0/ root) Gid: ( 0/ root) is set on stat /boot/grub/grub.cfg."
else
echo "ERROR: Access: (0444/-r--r--r--) Uid: ( 0/ root) Gid: ( 0/ root) is not set on stat /boot/grub/grub.cfg."
stat /boot/grub/grub.cfg
fi
user=$( stat /boot/grub/user.cfg )
if [[ $user = *"(0444/-r--r--r--)"* ]]
then
echo "Access: (0444/-r--r--r--) Uid: ( 0/ root) Gid: ( 0/ root) is set on stat /boot/grub/user.cfg"
else
echo "ERROR: Access: (0444/-r--r--r--) Uid: ( 0/ root) Gid: ( 0/ root) is not set on stat /boot/grub/user.cfg"
stat /boot/grub/user.cfg
fi
And I want this script to show me only ERROR/Access without the output of the command "stat /boot/grub/grub.cfg" such as "the file or folder does not exist" or something similar.
As if the command run background, and it would only show me if it's ok or not. Same as testssh.sh .
enter image description here
Without "stat: cannot statx '/boot/grub/user.cfg': No such file or directory" only "ERROR: Access: (0444/-r--r--r--) Uid: ( 0/ root) Gid: ( 0/ root) is not set on stat /boot/grub/user.cfg
".
答案1
得分: 0
检查在运行 stat
命令之前文件是否存在。
if [ -e /boot/grub/grub.cfg ]; then
# 对 /boot/grub/grub.cfg 运行 stat 命令
fi
您还可以查看 stat
的 man 手册,以仅输出所需的数据,而不是进行通配符匹配,并使用函数来使您的代码更加干燥。
这是一个不依赖于 bashisms 的脚本版本:
checkperms() {
if [ -e "$1" ]; then
local perms
perms=$(stat -c %a "$1")
if [ "${perms}" = 444 ]; then
echo "文件 $1 的访问权限正常"
else
echo "错误:文件 $1 的访问权限不正确" 1>&2
stat "$1"
fi
fi
}
checkperms /boot/grub/grub.cfg
checkperms /boot/grub/user.cfg
英文:
Check for the existence of the file before running stat
on it.
if [ -e /boot/grub/grub.cfg ]; then
# run stat on /boot/grub/grub.cfg
fi
You also might want to check the manpage for stat
to only output the data you want, rather than doing wildcard matching.
and use functions to keep your code DRY.
Here's a version of your script that does not depend on bashisms:
checkperms() {
if [ -e "$1" ]; then
local perms
perms=$(stat -c %a "$1")
if [ "${perms}" = 444 ]; then
echo "Access OK for $1"
else
echo "ERROR: wrong Access for $1" 1>&2
stat "$1"
fi
fi
}
checkperms /boot/grub/grub.cfg
checkperms /boot/grub/user.cfg
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论