简体   繁体   English

shell脚本循环通过通配符匹配的目录

[英]shell script to loop through wildcard matched directories

I am trying to loop through directories that match a wildcard. 我试图循环通过匹配通配符的目录。 This works fine in command line. 这在命令行中工作正常。 But does not work in Shell script. 但是在Shell脚本中不起作用。 Any idea? 任何的想法?

   for dirs in /var/www/html/my.domain.com/v*; do echo $dirs; done;

The above command lists 以上命令列出

  /var/www/html/my.domain.com/v1.0
  /var/www/html/my.domain.com/v1.2
  /var/www/html/my.domain.com/v2.0

Here is the Shell script version. 这是Shell脚本版本。 Its not working: 它不起作用:

 dirs=/var/www/html/my.domain.com/v*
 for dir in $dirs
 do
   echo "$dir"
 done

Tried this too: 试过这个:

dirs=`/var/www/html/my.domain.com/v*` #back quotes
for dir in $dirs
do
   echo "$dir"
done

Glob patterns like /var/www/html/my.domain.com/v* are a bash feature. /var/www/html/my.domain.com/v*这样的全局模式是一种bash功能。 It seems that bash is not the default shell on your system as you started the script using sh script.sh . 当您使用sh script.sh启动脚本时,bash似乎不是系统上的默认shell。

Make sure that you start the script using bash: 确保使用bash启动脚本:

bash script.sh

If you are about to make the script executable directly using chmod +x script.sh , make sure that you are not missing to use the following shebang in the first line: 如果您要使用chmod +x script.sh直接使脚本可执行,请确保您不会错过在第一行中使用以下shebang

#!/bin/bash

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM