非常感谢您提前帮助!
我在bash中有这个代码:
for d in this_folder/*
do
plugin=$(basename $d)
echo $plugin'?'
read $plugin
done
这就像魅力一样。对于'this_folder'中的每个文件夹,将其作为一个问题回显并将输入存储到具有相同名称的变量中。
但是现在我想排除一些文件夹,例如,它会询问该目录中的每个文件夹,只要它们不是以下任何文件夹:global,plugins和css。
任何想法我怎样才能做到这一点?
谢谢!
更新:
这是最终代码的样子:
base="coordfinder|editor_and_options|global|gyro|movecamera|orientation|sa"
> vt_conf.sh
echo "# ========== Base" >> vt_conf.sh
for d in $orig_include/@($base)
do
plugin=$(basename $d)
echo "$plugin=y" >> vt_conf.sh
done
echo '' >> vt_conf.sh
echo "# ========== Optional" >> vt_conf.sh
for d in $orig_include/!($base)
do
plugin=$(basename $d)
echo "$plugin=n" >> vt_conf.sh
done
答案 0 :(得分:9)
如果你有最新版本的bash,你可以使用扩展的globs(shopt -s extglob
):
for d in this_folder/!(global|plugins|css)/
do
plugin=$(basename "$d")
echo $plugin'?'
read $plugin
done
答案 1 :(得分:7)
您可以使用continue
跳过循环的一次迭代:
for d in this_folder/*
do
plugin=$(basename $d)
[[ $plugin =~ ^(global|plugins|css)$ ]] && continue
echo $plugin'?'
read $plugin
done
答案 2 :(得分:1)
如果您只想排除名为global,css,plugins的目录。这可能不是一个优雅的解决方案,但会做你想要的。
for d in this_folder/*
do
flag=1
#scan through the path if it contains that string
for i in "/css/" "/plugins/" "/global/"
do
if [[ $( echo "$d"|grep "$i" ) && $? -eq 0 ]]
then
flag=0;break;
fi
done
#Only if the directory path does NOT contain those strings proceed
if [[ $flag -eq 0 ]]
then
plugin=$(basename $d)
echo $plugin'?'
read $plugin
fi
done
答案 3 :(得分:0)
您可以使用find
和awk
来构建目录列表,然后将结果存储在变量中。有点像这样(未经测试):
dirs=$(find this_folder -maxdepth 1 -type d -printf "%f\n" | awk '!match($0,/^(global|plugins|css)$/)')
for d in $dirs; do
# ...
done
更新2019-05-16:
while read -r d; do
# ...
done < <(gfind -maxdepth 1 -type d -printf "%f\n" | awk '!match($0,/^(global|plugins|css)$/)')
答案 4 :(得分:0)
How to exclude some files from the loop in shell script被标记为该问题的重复,并被关闭,该问题专门询问有关在BASH脚本中排除 文件 的问题。正是我需要的(在脚本中检查本地URL中链接片段(在#之后的部分)的有效性。这是我的解决方案。
for FILE in *
do
## https://linuxize.com/post/how-to-check-if-string-contains-substring-in-bash/
if [[ "$FILE" == *"cnp_"* ]]
then
echo 'cnp_* file found; skipping'
continue
fi
## rest of script
done
输出:
cnp_* file found; skipping
----------------------------------------
FILE: 1 | NAME: linkchecker-test_file1.html
PATH: /mnt/Vancouver/domains/buriedtruth.com/linkchecker-tests/linkchecker-test_file1.html
RAW LINE: #bookmark1
FULL PATH: /mnt/Vancouver/domains/buriedtruth.com/linkchecker-tests/linkchecker-test_file1.html#bookmark1
LINK: /mnt/Vancouver/domains/buriedtruth.com/linkchecker-tests/linkchecker-test_file1.html
FRAGMENT: bookmark1
STATUS: OK
...
我的测试目录包含3个文件,其中一个我想排除(旧网站的网络抓取:一个包含大量不赞成使用的链接片段的索引)。
[victoria@victoria link_fragment_tester]$ tree
.
├── cnp_members-index.html
├── linkchecker-test_file1.html -> /mnt/Vancouver/domains/buriedtruth.com/linkchecker-tests/linkchecker-test_file1.html
└── linkchecker-test_file2.html -> /mnt/Vancouver/domains/buriedtruth.com/linkchecker-tests/linkchecker-test_file2.html
0 directories, 3 files
[victoria@victoria link_fragment_tester]$
答案 5 :(得分:0)
拆分路径,检查每个要忽略的文件夹名称。
如果您想增加以后要忽略的文件夹数量,这种方式忽略文件夹很方便。 (它们在这里存储在被忽略的文件夹中)。
在代码中添加了注释。
这在 Ubuntu 20.04.2 中对我来说很好
select race_name ,(select sum(quantity) from food where animal_id in (select animal_id from animal a where r.race_code = a.race_code))
from race r