简体   繁体   中英

Can't find unexpected operator (Bash error)

I've created a simple Bash script that should create symlinks in /usr/local/{etc,lib,include...} from an inputted directory path

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

The error i'm getting is:

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

and this is what /usr/local/test/ looks like:

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

Why the quotes? Change that to:

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

otherwise $dir will contain etc include bin ... on the first (and only) iteration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM