傻shell脚本错误

时间:2013-01-19 04:36:32

标签: shell

我有以下用于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

我必须是愚蠢/愚蠢的东西,但是如果我能看到它就会变得愚蠢!

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

您在done循环中忘记了for。语法是:

for statement
do
  statements...
done