找不到意外的操作符(Bash错误)

我创build了一个简单的Bash脚本,它应该从input的目录path在/ usr / local / {etc,lib,include …}中创build符号链接

#!/bin/sh input="$1" for subdir in "etc include bin lib man share sbin"; do dir=$input/$subdir if [ -e $dir ] && [ -d $dir ]; then for file in $dir/*; do ln -s $file /usr/local/$subdir done fi done 

我得到的错误是:

 user@comp:/usr/local# ./update-locallinks /usr/local/test/ [: 6: /usr/local/test/etc: unexpected operator 

这就是/ usr / local / test /的样子:

 user@comp:/usr/local# ls /usr/local/test/ bin etc include lib 

在测试表达式中始终用双引号保护你的bash变量:

  if [ -e "$dir" ] && [ -d "$dir" ]; then 
 for subdir in "etc include bin lib man share sbin"; do 

为什么报价? 将其更改为:

 for subdir in etc include bin lib man share sbin; do 

否则$dir将包含etc include bin ...在第一个(也是唯一)的迭代。