[英]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) 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
我希望這個腳本只顯示錯誤/訪問,而沒有命令“stat /boot/grub/grub.cfg”的 output,例如“文件或文件夾不存在”或類似的東西。 好像命令在后台運行,它只會告訴我是否可以。 與 testssh.sh 相同。
在此處輸入圖像描述沒有“stat: cannot statx '/boot/grub/user.cfg': No such file or directory” 只有“ERROR: Access: (0444/-r--r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 未在 stat /boot/grub/user.cfg 上設置”。
在對文件運行stat
之前檢查該文件是否存在。
if [ -e /boot/grub/grub.cfg ]; then
# run stat on /boot/grub/grub.cfg
fi
您可能還想查看stat
的聯機幫助頁以僅 output 您想要的數據,而不是進行通配符匹配。 並使用函數讓你的代碼保持干爽。
這是不依賴於 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.