【发布时间】:2013-04-03 23:56:18
【问题描述】:
我正在尝试遍历与通配符匹配的目录。这在命令行中工作正常。但不适用于 Shell 脚本。有什么想法吗?
for dirs in /var/www/html/my.domain.com/v*; do echo $dirs; done;
以上命令列表
/var/www/html/my.domain.com/v1.0
/var/www/html/my.domain.com/v1.2
/var/www/html/my.domain.com/v2.0
这里是 Shell 脚本版本。它不工作:
dirs=/var/www/html/my.domain.com/v*
for dir in $dirs
do
echo "$dir"
done
也试过了:
dirs=`/var/www/html/my.domain.com/v*` #back quotes
for dir in $dirs
do
echo "$dir"
done
【问题讨论】:
-
这个例子对我有用——没有任何引用。您之前在此脚本中是否使用过
shopt? -
进一步注意,使用反引号引用将启动一个子shell并返回其输出。因此,在反引号中,只有命令才有意义
-
@hek2mgl 不,只是一些其他代码。但无处使用
shopt -
是你的shell脚本
#!/bin/bash或#!/bin/sh的第一行? -
感谢您的信息。也试过不带引号。但没有运气。
标签: linux shell loops for-loop directory