0

我有以下用于 OSX 的 shell 脚本,我想用它来更正/修复用户目录的权限,但出现错误:

bash-3.2# sh fix-perms.sh 
fix-perms.sh: line 23: syntax error near unexpected token `else'
fix-perms.sh: line 23: `else'

我的脚本:

#!/bin/sh

# Script fixes permissions in Users directory

#  Set PATH as shortcut
#
PATH=/bin:/usr/bin:/sbin:/usr/sbin export PATH

# First verify volume is mounted before executing

if [ -d /Volumes/UserStorage ] ;
then

        USERS=`mktemp -t users-XXXXX.log`
        ls -1 /Volumes/UserStorage/Users > $USERS

        for i in `cat $USERS`; do
        chmod -R 700 /Volumes/UserStorage/Users/$i

        find /Volumes/UserStorage/Users/$i -type d -exec chmod 701 {} \;
        exit 0

else

echo Volume Not Mounted

exit 1

fi

这一定是我想念的愚蠢/愚蠢的东西,但如果我能看到它就该死!

提前感谢您的提示/见解。

4

2 回答 2

2

问:您是否意识到您在执行循环中缺少“完成”?

问:如果你把“then”和分号放在同一行会发生什么?

以下是更多示例:

http://www.thegeekstuff.com/2010/06/bash-if-statement-examples/

这是您的原始代码,稍作修改:

#!/bin/sh
# Script fixes permissions in Users directory
#  Set PATH as shortcut
#
PATH=/bin:/usr/bin:/sbin:/usr/sbin export PATH

# First verify volume is mounted before executing
if [ -d /Volumes/UserStorage ] ; then
        USERS=`mktemp -t users-XXXXX.log`
        ls -1 /Volumes/UserStorage/Users > $USERS

        for i in `cat $USERS`; do
          chmod -R 700 /Volumes/UserStorage/Users/$i

          find /Volumes/UserStorage/Users/$i -type d -exec chmod 701 {} \;
          exit 0
        done

else
  echo Volume Not Mounted
  exit 1
fi
于 2013-01-19T04:39:03.720 回答
1

你忘done了你的for循环。语法是:

for statement
do
  statements...
done
于 2013-01-19T04:39:24.330 回答