Shell 语法错误:“(“意外(应为“fi”)

Shell 语法错误:“(“意外(应为“fi”),shell,scripting,syntax-error,sh,Shell,Scripting,Syntax Error,Sh,我猜问题在于,您试图在阵列中捕获命令输出,而不实际使用命令替换。您希望这样: filein="users.csv" IFS=$'\n' if [ ! -f "$filein" ] then echo "Cannot find file $filein" else #... groups=(`cut -d: -f 6 "$filein" | sed 's/ //'`) fullnames=(`cut -d: -f 1 "$filein"`) userid=(`

我猜问题在于,您试图在阵列中捕获命令输出,而不实际使用命令替换。您希望这样:

filein="users.csv"

IFS=$'\n'

if [ ! -f "$filein" ]

then

  echo "Cannot find file $filein"

else

  #...

  groups=(`cut -d: -f 6 "$filein" | sed 's/ //'`)

  fullnames=(`cut -d: -f 1 "$filein"`)

  userid=(`cut -d: -f 2 "$filein"`)

  usernames=(`cut -d: -f 1 "$filein" | tr [A-Z] [a-z] | awk '{print substr($1,1,1) $2}'`)


  #...

  for group in ${groups[*]}

  do

    grep -q "^$group" /etc/group ; let x=$?

    if [ $x -eq 1 ]

    then

      groupadd "$group"

    fi

  done


 #...

  x=0

  created=0

  for user in ${usernames[*]}

  do

    useradd -n -c ${fullnames[$x]} -g "${groups[$x]}" $user 2> /dev/null

    if [ $? -eq 0 ]

    then

      let created=$created+1

    fi


 #...

    echo "${userid[$x]}" | passwd --stdin "$user" > /dev/null

    #...

    echo "Welcome! Your account has been created.  Your username is $user and temporary 
password is \"$password\" without the quotes." | mail -s "New Account for $user" -b root $user

    x=$x+1

    echo -n "..."

    sleep .25

  done

  sleep .25

  echo " " 

  echo "Complete. $created accounts have been created."

fi

请注意,在内集前面有一组额外的括号,括号中有$。

要格式化代码,请完全粘贴您拥有的内容,高亮显示所有内容,然后单击{}icon.SO不是调试服务。如果您遇到特定问题,请发布复制该问题的最小数量的代码。另请参阅-如果遵循了其中的说明,则通过不满意的反馈/否决票,此问题将得到相当少的解决。@Richard,为什么要重新添加额外的垂直空格?我认为r2比r多得多eadable比r3更可靠。如果没有一些安全性,这不是一个好的做法-例如,如果cut发出*,它将进行全局扩展;如果它发出一个包含IFS字符的值,它将在同一个值上拆分,等等。readarray-t groupsgroups=( $( cut... ) )