Checking for a file with a specific type

In fish, you can check if a particular file exists with test -e hello.txt

, but I'm wondering if there is a way to use wildcard values ​​in the filename to check for the existence of some type of file.

To be more specific, I am making a quick function that checks the Xcode workspace if open, if not then check the Xcode project and if found it will open that, otherwise print an error.

Here's the current implementation:

function xcp
    open *.xcodeproj
end

function xcw
    open *.xcworkspace
end

function xc
    if test -e *.xcworkspace
        xcw
    else if test -e *.xcodeproj
        xcp
    else
        echo "No Xcode Workspace or Project in this directory."
    end
end

      

It "works" in a minute, but when it doesn't find a workspace or project file, it prints a no match message. However, this is done by default and I am wondering if I can somehow hide this.

Here is the error output when it doesn't find the workspace but finds the project and opens it:

No matches for wildcard '*.xcworkspace'.  (Tip: empty matches are allowed in 'set', 'count', 'for'.)
~/.config/fish/functions/fish_prompt.fish (line 1): if test -e     *.xcworkspace
                                                           ^
in function 'xc'
    called on standard input

      

+3


source to share


1 answer


Try to count them:



function xc
    set -l workspaces *.xcworkspace
    set -l projects   *.xcodeproj
    if test (count $workspaces) -gt 0
        xcw
    else if test (count $projects) -gt 0
        xcp
    else
        echo "No Xcode Workspace or Project in this directory."
    end
end

      

+5


source







All Articles