gpt4 book ai didi

centos - 用于更正服务器上所有 cPanel 用户的文件权限的脚本

转载 作者:行者123 更新时间:2023-12-04 19:35:24 27 4
gpt4 key购买 nike

我正在尝试使用以下脚本,该脚本旨在修复服务器上所有 cPanel 用户的文件权限。该脚本在第 32 行返回一个错误。谁能告诉我我在那里做错了什么?

#!/bin/bash
# Script to fix permissions of accounts

if [ "$#" -lt "1" ];then
echo "Must specify user"
exit;
fi

USER=$@

for user in $USER
do

HOMEDIR=$(egrep "^${user}:" /etc/passwd | cut -d: -f6)

if [ ! -f /var/cpanel/users/$user ]; then
echo "$user user file missing, likely an invalid user"
elif [ "$HOMEDIR" == "" ];then
echo "Couldn't determine home directory for $user"
else
echo "Setting ownership for user $user"
chown -R $user:$user $HOMEDIR
chmod 711 $HOMEDIR
chown $user:nobody $HOMEDIR/public_html $HOMEDIR/.htpasswds
chown $user:mail $HOMEDIR/etc $HOMEDIR/etc/*/shadow $HOMEDIR/etc/*/passwd

echo "Setting permissions for user $USER"

find $HOMEDIR -type f -exec chmod 644 {} ; -print
find $HOMEDIR -type d -exec chmod 755 {} ; -print
find $HOMEDIR -type d -name cgi-bin -exec chmod 755 {} ; -print
find $HOMEDIR -type f ( -name "*.pl" -o -name "*.perl" ) -exec chmod 755 {} ; -print
fi

done

chmod 750 $HOMEDIR/public_html

if [ -d "$HOMEDIR/.cagefs" ]; then
chmod 775 $HOMEDIR/.cagefs
chmod 700 $HOMEDIR/.cagefs/tmp
chmod 700 $HOMEDIR/.cagefs/var
chmod 777 $HOMEDIR/.cagefs/cache
chmod 777 $HOMEDIR/.cagefs/run
fi
错误输出
$ ./fixperms.sh username
./fixperms.sh: line 32: syntax error near unexpected token `('
./fixperms.sh: line 32: ` find $HOMEDIR -type f ( -name "*.pl" -o -name "*.perl" ) -exec chmod 755 {} ; -print'

最佳答案

试试这样:

#!/bin/bash
# Script to fix permissions of accounts

if [ "$#" -lt "1" ];then
echo "Must specify user"
exit;
fi

USER=$@

for user in $USER
do

HOMEDIR=$(egrep "^${user}:" /etc/passwd | cut -d: -f6)
echo $HOMEDIR

if [ ! -f /var/cpanel/users/$user ]; then
echo "$user user file missing, likely an invalid user"
elif [ "$HOMEDIR" == "" ];then
echo "Couldn't determine home directory for $user"
else
echo "Setting ownership for user $user"
chown -R $user:$user $HOMEDIR
chmod 711 $HOMEDIR
chown $user:nobody $HOMEDIR/public_html $HOMEDIR/.htpasswds
chown $user:mail $HOMEDIR/etc $HOMEDIR/etc/*/shadow $HOMEDIR/etc/*/passwd

echo "Setting permissions for user $USER"

find $HOMEDIR -type f -exec chmod 644 {} + -print
find $HOMEDIR -type d -exec chmod 755 {} + -print
find $HOMEDIR -type d -name cgi-bin -exec chmod 755 {} + -print
find $HOMEDIR -type f \( -name \*.pl -o -name \*.perl \) -exec chmod 755 {} + -print
fi

done

chmod 750 $HOMEDIR/public_html

if [ -d "$HOMEDIR/.cagefs" ]; then
chmod 775 $HOMEDIR/.cagefs
chmod 700 $HOMEDIR/.cagefs/tmp
chmod 700 $HOMEDIR/.cagefs/var
chmod 777 $HOMEDIR/.cagefs/cache
chmod 777 $HOMEDIR/.cagefs/run
fi
您需要转义 括号 () 使用\
也可以使用 {} +而不是 {} \;您将大大提高脚本的速度,因为您替换了 -执行结束 从\; + , find 会优化子流程的创建,只调用 chmod 尽可能少的次数

关于centos - 用于更正服务器上所有 cPanel 用户的文件权限的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69507284/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com