找不到意外的运算符(Bash 错误)
我创建了一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
始终在测试表达式中使用双引号保护 bash 变量:
Always protect your bash variables with double quotes in test expressions:
为什么要加引号?将其更改为:
否则
$dir
将在第一次(也是唯一的)迭代中包含etc include bin ...
。Why the quotes? Change that to:
otherwise
$dir
will containetc include bin ...
on the first (and only) iteration.