Ubuntu Server执行grantperm.sh脚本遇fi语法错误求助
解决NextCloud权限脚本的语法错误问题
看起来你的grantperm.sh脚本有几个明显的语法问题,导致执行时触发了unexpected token 'fi'错误,我来帮你一步步排查修复:
1. 核心问题:未匹配的fi语句
错误提示里的line 29: syntax error near unexpected token 'fi'说明脚本里的fi没有对应的if语句——要么是你粘贴的脚本内容被截断了(最后一行chown -R ${htu...明显不完整),要么是脚本里不小心多写了fi,或是漏写了对应的if开头语句。
2. 隐藏的坑:中文引号导致解析失败
看你提供的脚本片段,printf用的是中文全角引号“”,比如:
printf “Creating possible missing Directories”
这在bash里会被识别为无效字符,必须换成英文半角引号"",否则shell会把引号内的内容当成混乱的命令,进而引发后续语法报错。
3. 修正后的完整权限脚本示例
我给你一个完整且正确的NextCloud权限配置脚本,你可以替换掉原来的内容后再执行:
#!/bin/bash ocpath='/var/www/nextcloud' htuser='www-data' htgroup='www-data' rootuser='root' printf "Creating possible missing Directories\n" mkdir -p $ocpath/data mkdir -p $ocpath/updater printf "chmod Files and Directories\n" find ${ocpath}/ -type f -print0 | xargs -0 chmod 0640 find ${ocpath}/ -type d -print0 | xargs -0 chmod 0750 printf "chown Directories\n" chown -R ${rootuser}:${htgroup} ${ocpath}/ chown -R ${htuser}:${htgroup} ${ocpath}/apps/ chown -R ${htuser}:${htgroup} ${ocpath}/config/ chown -R ${htuser}:${htgroup} ${ocpath}/data/ chown -R ${htuser}:${htgroup} ${ocpath}/themes/ chown -R ${htuser}:${htgroup} ${ocpath}/updater/ chmod +x ${ocpath}/occ printf "Setting permissions for .htaccess\n" if [ -f ${ocpath}/.htaccess ] then chmod 0644 ${ocpath}/.htaccess chown ${rootuser}:${htgroup} ${ocpath}/.htaccess fi if [ -f ${ocpath}/data/.htaccess ] then chmod 0644 ${ocpath}/data/.htaccess chown ${rootuser}:${htgroup} ${ocpath}/data/.htaccess fi
4. 执行前的准备步骤
- 将上面的代码保存为
grantperm.sh - 给脚本添加执行权限:
chmod +x grantperm.sh - 再执行脚本:
sudo ./grantperm.sh
5. 后续排查语法错误的小技巧
如果以后再遇到类似shell脚本语法问题,可以用bash的静态检查功能,不用实际执行就能定位问题:
bash -n grantperm.sh
它会直接告诉你哪一行存在语法错误,方便快速修复。
内容的提问来源于stack exchange,提问作者Cristian
相关产品推荐
相关产品推荐

