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

发布于 2024-11-09 03:26:31 字数 611 浏览 0 评论 0原文

我创建了一个简单的 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/测试/看起来像:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沐歌 2024-11-16 03:26:31

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

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

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

   if [ -e "$dir" ] && [ -d "$dir" ]; then
我ぃ本無心為│何有愛 2024-11-16 03:26:31
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 ...

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文