Linux 如何在要排除的循环中grep?
我有一个NPM_RESERVED_作用域,它包含一个逗号分隔的禁止作用域列表 我有一个NPM_ALLOWED_DEV_DEPENDENCIES_包,其中包含一个逗号分隔的异常列表Linux 如何在要排除的循环中grep?,linux,bash,shell,sh,Linux,Bash,Shell,Sh,我有一个NPM_RESERVED_作用域,它包含一个逗号分隔的禁止作用域列表 我有一个NPM_ALLOWED_DEV_DEPENDENCIES_包,其中包含一个逗号分隔的异常列表 NPM_RESERVED_SCOPE=@privatescope,@secondary NPM_ALLOWED_DEV_DEPENDENCIES_PACKAGES="@privatescope/private-project-scripts,@privatescope/private-project-ci-build,
NPM_RESERVED_SCOPE=@privatescope,@secondary
NPM_ALLOWED_DEV_DEPENDENCIES_PACKAGES="@privatescope/private-project-scripts,@privatescope/private-project-ci-build,@privatescope/private-project-ci-test,@privatescope/private-project-ci-release,@privatescope/private-project-ci-deploy,@privatescope/private-project-ci-release-transitive"
for i in $(echo ${NPM_RESERVED_SCOPES} | sed "s/,/ /g")
do
for j in $(echo ${NPM_ALLOWED_DEV_DEPENDENCIES_PACKAGES} | sed "s/,/ /g")
do
if [[ $(cat package.json | jq .devDependencies | grep "$i/" | grep -v "$j") ]]; then
echo "[Error] GitHub release abort. Reason: devDependencies within scope $i are forbidden."
exit 1
fi
done
done
如果devdependences不包含任何NPM_RESERVED_作用域(异常列表除外),我希望此脚本通过
NPM_RESERVED_SCOPE=@privatescope,@secondary
NPM_ALLOWED_DEV_DEPENDENCIES_PACKAGES="@privatescope/private-project-scripts,@privatescope/private-project-ci-build,@privatescope/private-project-ci-test,@privatescope/private-project-ci-release,@privatescope/private-project-ci-deploy,@privatescope/private-project-ci-release-transitive"
for i in $(echo ${NPM_RESERVED_SCOPES} | sed "s/,/ /g")
do
for j in $(echo ${NPM_ALLOWED_DEV_DEPENDENCIES_PACKAGES} | sed "s/,/ /g")
do
if [[ $(cat package.json | jq .devDependencies | grep "$i/" | grep -v "$j") ]]; then
echo "[Error] GitHub release abort. Reason: devDependencies within scope $i are forbidden."
exit 1
fi
done
done
这是package.json文件
我该怎么做?将列表转换为扩展模式,方法是将列表替换为|,并用@包装 现在模式匹配可以替换嵌套的for循环。如果a匹配任何一个模式x、y或z,则a=@x | y | z
请为我刚才做的文件ok@dawg的示例添加广告。
reserved="@(${NPM_RESERVED_SCOPE//,/|})"
allowed="@(${NPM_ALLOWED_DEV_DEPENDENCIES_PACKAGES//,/|})"
while IFS= read -r dep; do
if [[ $dep = $reserved && $dep != $allowed ]]; then
echo "[Error] GitHub release abort. Reason: $dep within scope $i is forbidden." >&2
exit 1
fi
done < <( jq .devDependencies '.devDependencies' )