非常感谢您的帮助!
我在bash中有此代码:
for d in this_folder/*
do
plugin=$(basename $d)
echo $plugin'?'
read $plugin
done
工作就像魅力。对于" this_folder"中的每个文件夹,将其作为问题呼应,然后将输入存储到具有相同名称的变量中。
,但是现在我想排除某些文件夹,例如,它会要求该目录中的每个文件夹,只有当它们不是以下任何文件夹时:全局,插件和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
如果您有最近版本的bash,则可以使用扩展地球(shopt -s extglob
):
shopt -s extglob
for d in this_folder/!(global|plugins|css)/
do
plugin=$(basename "$d")
echo $plugin'?'
read $plugin
done
您可以使用continue
跳过循环的一个迭代:
for d in this_folder/*
do
plugin=$(basename $d)
[[ $plugin =~ ^(global|plugins|css)$ ]] && continue
echo $plugin'?'
read $plugin
done
如果您只想排除名为Global,CSS,插件的目录。这可能不是一个优雅的解决方案,但会做您想要的。
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
您可以使用find
和awk
来构建目录列表,然后将结果存储在变量中。符合此(未经测试)的线条:
dirs=$(find this_folder -maxdepth 1 -type d -printf "%fn" | 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 "%fn" | awk '!match($0,/^(global|plugins|css)$/)')
如何将shell脚本中的某些文件排除在此q/a和封闭的欺骗时,q专门询问了不包括 files 在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]$
分开路径,检查每个折叠名称要忽略。
如果要增加以后要忽略的文件夹数量,则忽略文件夹很方便。(他们在这里存储在《忽视的菲尔德》中)。
在代码中添加了评论。
这对我来说很好,在Ubuntu 20.04.2
#!/bin/bash
shopt -s globstar #necessary for search ./**/*
ignoredfolders=("folder1name" "folder2name")
for i in ./**/*
do
#pattern for splitting forward slashes into empty spaces
ARRAY=(${i////" "} );
for k in "${ignoredfolders[@]}"; do
#does path (splitted in ARRAY) contain a foldername ($k) to be ignored?
if [[ " ${ARRAY[@]} " =~ "$k" ]]; then
#this skips this loop and the outer loop
continue 2
fi
done
# all ignored folders are ignored, you code sits here...
#code ...
#code ...
done