我似乎无法让我的 bash 脚本中的正则表达式工作:
#!/bin/bash
cd /var/lib/gitolite/repositories
for D in *; do
if [ -d "${D}" ]; then
if [["${D}" = [^[0-9A-Za-z\-_].git$] ]]; then
echo "${D}"
fi
fi
done
可能的目录名称:
test.git
test-admin12.git
test_admin.git
test_admin.git.bkp (these are the folders I DON'T want)
我不想启动辅助进程,例如:sed 或 grep 或 ls
最佳答案
代替:
if [["${D}" = [^[0-9A-Za-z\-_].git$] ]]; then
echo "${D}"
fi
以正确的语法和正确的正则表达式使用此条件:
[[ "${D}" =~ ^[0-9A-Za-z_-]+\.git$ ]] && echo "${D}"
关于regex - 用于处理目录的 Bash 脚本 (REGEX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20357365/