0

Context: I'm trying to clean my Xcode workspace and scheme via the command line. I've tried both trimming the quotes and not trimming them.

The current result is: -bash: ~/my/workspace/location: No such file or directory. But, if I run the command directly, $ xcodebuild clean -workspace ~/my/workspace/location -scheme SchemeName, it works just fine.

Here's what I've got so far:

cxc () {
    local path_to_workspace="~/my/workspace/location"
    local scheme="SchemeName"

    if [ ! -z "$1" ]; then
        path_to_workspace="$1"
    fi

    if [ ! -z "$2" ]; then
        scheme="$2"
    fi

    if [ -z "$1" ]; then
        echo "No arguments supplied. Using default."
    fi

    # Trim quotes from URL
    path_to_workspace=$("$path_to_workspace" | tr -d \")

    xcodebuild clean -workspace "$path_to_workspace" -scheme "$scheme"
}
h.and.h
  • 690
  • 1
  • 8
  • 26
  • 3
    `~` is not expanded when expanding a variable. Don't quote it in the variable assignment. – Barmar Mar 29 '23 at 19:59
  • 2
    Use `${HOME}` instead of `~`. – Bib Mar 29 '23 at 19:59
  • 1
    The quoted assignment to `path_to_workspace` does not undergo tilde expansion like the unquoted argument to the command does. `~` is not the real name of a directory; it's a shell feature. – chepner Mar 29 '23 at 19:59
  • @F.Hauri-GiveUpGitHub - the error message mentioned in this question (`No such file or directory`) was not due to the use of `~` but because of this subshell call: `$("$path_to_workspace" | tr -d \")` - trying to 'execute' a directory name; while I have no problem with the closing of this question, the reason given for closing the question (`[duplicate]` re: `~ expansion`) is incorrect in this instance – markp-fuso Mar 29 '23 at 21:38
  • @markp-fuso Duplicate list edited! this is anyway a duplicate. – F. Hauri - Give Up GitHub Mar 30 '23 at 07:01
  • @F.Hauri-GiveUpGitHub - still missing the point; this particular error has nothing to do with `~` or `'removing quotes from data'`; error was due to trying to *execute* a path => `path_to_workspace=$(~/my/workspace/location | tr-d '"`) – markp-fuso Mar 30 '23 at 13:30

3 Answers3

2

The line of interest:

path_to_workspace=$("$path_to_workspace" | tr -d \")

This spawns a subshell that tries to execute $path_to_workspace, ie, it tries to execute ~/my/workspace/location:

path_to_workspace=$(~/my/workspace/location | tr -d \")
                    ^^^^^^^^^^^^^^^^^^^^^^^

And since this is not a valid command ... error message.

What you probably want is:

path_to_workspace=$(echo "$path_to_workspace" | tr -d \")
                    ^^^^

I'm assuming the tr -d \" is looking to remove double quotes from the value stored in $patch_to_workspace but, there are no double quotes stored in the variable so ... ??

At this point it seems (to me) that all you need to do is remove this line:

path_to_workspace=$("$path_to_workspace" | tr -d \")

This should get you past the current error. I'm not familiar with xcodebuild so I can't speak to whether or not you'll receive a new error ...

markp-fuso
  • 28,790
  • 4
  • 16
  • 36
  • Avoid useless pipe: `$(tr -d \" <<<"$path_to_workspace")` or better, avoid useless forks: `path_to_workspace=${path_to_workspace//\"}` – F. Hauri - Give Up GitHub Mar 30 '23 at 06:11
  • @F.Hauri-GiveUpGitHub sure, agree with your comments but I wasn't trying to address how to feed a value to `tr`, since a) the `tr` isn't needed and b) this has nothing to do with removing (non-existing) qutoes from the value stored in the variable – markp-fuso Mar 30 '23 at 13:33
0

The issue was the ~. It needed to be updated to ${HOME}. This worked:

# Clean Xcode
cxc () {
    local path_to_workspace="${HOME}/my/workspace/location"
    local scheme="SchemeName"

    if [ ! -z "$1" ]; then
        path_to_workspace="$1"
    fi

    if [ ! -z "$2" ]; then
        scheme="$2"
    fi

    if [ -z "$1" ]; then
        echo "No arguments supplied. Using default."
    fi

    xcodebuild clean -workspace "$path_to_workspace" -scheme "$scheme"   
}
h.and.h
  • 690
  • 1
  • 8
  • 26
  • replacing `~` with `${HOME}` did *not* solve the problem/error mentioned in the question; removing this line of code - `path_to_workspace=$("$path_to_workspace" | tr -d \")` - from your script is what solved the problem/error (see my answer for details); in this answer you've removed that line of code but had you left that line of code in this answer you would've still generated the same error message as in the original question: `-bash: /home/h_and_h/my/workspace/location: No such file or directory` – markp-fuso Mar 29 '23 at 21:24
0

The tilde part until and including the first slash (examples: ~/ or user/) must not be quoted.

Example:

#!/bin/bash

function test_it()
{
    local a

    a="~user/xyz"; echo "$a"
    a=~"user/xyz"; echo "$a"
    a=~user/"xyz"; echo "$a"

    a="~/xyz"; echo "$a"
    a=~"/xyz"; echo "$a"
    a=~/"xyz"; echo "$a"
}

test_it

Will for example print:

~user/xyz
~user/xyz
/home/user/xyz
~/xyz
~/xyz
/root/xyz

${HOME}/ will only help to replace ~/, but not to replace ~user/.

Wiimm
  • 2,971
  • 1
  • 15
  • 25