找不到意外的运算符(重击错误) [英] Can't find unexpected operator (Bash error)

查看:66
本文介绍了找不到意外的运算符(重击错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我遇到的错误是:

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

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

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

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

推荐答案

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

Always protect your bash variables with double quotes in test expressions:

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

这篇关于找不到意外的运算符(重击错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆