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

标签 linux bash unix

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

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

关于linux - 找不到意外的运算符(Bash 错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6086793/

相关文章:

linux下工具安装Java异常

linux - gnuplot bash 脚本变量

LInux Kernel API 查找vma对应的虚拟地址

bash 变量分别捕获 stderr 和 stdout 或获取退出值

linux - 将度坐标转换为十进制

unix - Linux : How to AWK to find maximums in every unique line

在 GTK+ gui 中调用外部函数

linux - 如何将 shuf 命令用作变量

bash - 使用 bash 使用 wget 下载特定网站文件夹的所有网页

bash - 使用文件进行输入时无法设置 ffmpeg header