Home > Net >  Check some settings in debian/ubuntu and the output should be similar to testssh
Check some settings in debian/ubuntu and the output should be similar to testssh

Time:01-09

I need help. 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 ".

CodePudding user response:

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
  • Related