Bash 脚本 - 将目录名称与字符串进行比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14249210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:13:10  来源:igfitidea点击:

Bash Script - Compare Directory Name to String

bash

提问by Bryan Christophe Green

I'm using this script to run a command within each subdirectory in the 'sites' directory, EXCLUDING the 'all' subdirectory. However, when I run this, the 'all' subdirectory is still being used, even though I use an if statement to exclude it.

我正在使用此脚本在“sites”目录中的每个子目录中运行命令,不包括“all”子目录。但是,当我运行它时,'all' 子目录仍在使用,即使我使用 if 语句来排除它。

for dir in ~/htdocs/drupal/drupal/sites/*
  do
  if [ $dir = "/local/users/drupadm/htdocs/drupal/drupal/all" ]
    then
    continue
  fi
echo $dir
(cd $dir && /opt/webstack/php/5.2/bin/php /local/users/drupadm/drush/drush.php )
done

Bryan

布莱恩

回答by John Kugelman

You left out the sitessub-directory:

您遗漏了sites子目录:

if [ "$dir" = /local/users/drupadm/htdocs/drupal/drupal/sites/all ]

回答by FrankieTheKneeMan

IFS=$'\n'
for dir in `find ~/htdocs/drupal/drupal/sites/ -maxdepth 1 -type d ! -name all -printf '%p\n'`
do
    echo $dir
    (cd $dir && /opt/webstack/php/5.2/bin/php /local/users/drupadm/drush/drush.php )
done
IFS=' '

Exclude that directory in the first place, use find!

首先排除该目录,使用find

It's invoked by

它被调用

find dir_name [tests...]

Tests used by my command:

我的命令使用的测试:

-maxdepth 1

Of immediate children...

直系孩子...

-type d

... find directories ...

... 查找目录 ...

! -name all

... Not named 'all'.

... 未命名为“全部”。

回答by beastofbobmin

Hi you need to do the following

您好,您需要执行以下操作

if [ $dir == "/local/users/drupadm/htdocs/drupal/drupal/all" ]

if [ $dir == "/local/users/drupadm/htdocs/drupal/drupal/all" ]

Thanks

谢谢

回答by ubfan1

Try quoting the $dir, you will fine directories with spaces in their names kill you. You may also run scripts with -vx switches on the invocation to see what's read in and what's executed. bash -vx my script or set -vx whatever.

尝试引用 $dir,您会发现名称中有空格的目录会杀死您。您还可以在调用时使用 -vx 开关运行脚本,以查看读取的内容和执行的内容。bash -vx 我的脚本或设置 -vx 什么的。