找不到意外的运算符(Bash错误)

时间:2011-05-22 08:14:14

标签: linux bash unix

我创建了一个简单的Bash脚本,它应该在输入的目录路径中的/ usr / local / {etc,lib,include ...}中创建符号链接

#!/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

2 个答案:

答案 0 :(得分:1)

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

   if [ -e "$dir" ] && [ -d "$dir" ]; then

答案 1 :(得分:0)

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 ...