unix shell列表所有Xcode xcworkspace和xcodeproj

j0pj023g  于 2022-11-04  发布在  Unix
关注(0)|答案(1)|浏览(162)

我想列出项目目录下所有的xcworkspace和xcodeproj文件。
我写了下面代码


# !/bin/bash

workspaceFile="xcworkspace"
projectFile="xcodeproj"

# listFiles

path=$(pwd)

fileList=()

walk_dir () {
    for name in "$path"/*; do
        if [[ -d "$name" && ("${name##*.}" != ${projectFile} || "${name##*.}" != ${workspaceFile}) ]]; then
            path=$name
            walk_dir "$name"
        else 
            if [ "${name##*.}"x = ${workspaceFile}x ] || [ "${name##*.}"x = ${projectFile}x ];then
                fileList+=($name)
            fi
        fi
    done
}

walk_dir

for name in ${fileList[@]}; do
    echo $name
done

它没有工作。
但如果改变条件,如下图所示

if [[ -d "$name" && ("${name##*.}" != ${workspaceFile}) ]]; then
            path=$name
            walk_dir "$name"
        else 
            if [ "${name##*.}"x = ${workspaceFile}x ] || [ "${name##*.}"x = ${projectFile}x ];then
                fileList+=($name)
            fi
        fi

if [[ -d "$name" && ("${name##*.}" != ${projectFile}) ]]; then
            path=$name
            walk_dir "$name"
        else 
            if [ "${name##*.}"x = ${workspaceFile}x ] || [ "${name##*.}"x = ${projectFile}x ];then
                fileList+=($name)
            fi
        fi

这也行得通。
我想保留筛选器xcworkspace和xcodeproj。
有人能帮我吗?

ltqd579y

ltqd579y1#

最后,我自己找到了结果。


# !/bin/bash

workspaceFile="xcworkspace"
projectFile="xcodeproj"

path=$1
fileList=()

projectList () {
    for name in "$path"/*; do
        if [ -d "$name" ] && [ "${name##*.}"x = ${workspaceFile}x ]; then
            fileList+=(${name})
        elif [ -d "$name" ] && [ "${name##*.}"x = ${projectFile}x ]; then
            fileList+=(${name})
        elif [ -d "$name" ]; then
            path=$name
            projectList
        fi
    done
}

projectList

for name in ${fileList[@]}; do
    echo "xcode project: $name"
done

相关问题