Alternative to dirname command that supports spaces - macos

Consider the following output, when run the command:
$ /usr/libexec/java_home -v 1.8
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
The command dirname are not getting right the directory name:
$ echo $(dirname $(/usr/libexec/java_home -v 1.8))
/Library Plug-Ins/JavaAppletPlugin.plugin/Contents
I think dirname nor supports spaces. I'm behind macOS with Big Sur.

After the tip of #Shawn the solution is simple: just use quotes...
$ echo "$(dirname "$(/usr/libexec/java_home -v 1.8)")"
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents
Thanks #Shawn!

Related

bash scripting - what does "ls .." (ls with double dot) do?

My homework asked me to find what ls .. does.
I tried to search the internet but I couldn't find any answer to the question.
Does someone know what this command does?
.. is an entry in the current directory that refers to the parent of the current directory. It's not a special convention used just by ls. For example:
$ cd /usr/bin
$ pwd
/usr/bin
$ cd ..
$ pwd
/usr
$ [ -d .. ] && echo "It's a directory"
It's a directory
$ stat ..
<output specific to your installed version of stat>

Can't remove an item in $PATH (Xamarin Workbooks)

The following directory no longer exists on my MacBook Pro after I uninstalled Visual Studio Community Edition for Mac. I can copy and paste it from the $PATH though here:
/Applications/Xamarin Workbooks.app/Contents/SharedSupport/path-bin
It's still in the $PATH but I can't tell where it is being set
~ $ grep -n SharedSupport ${HOME}/.bash_profile
~ $ grep -n SharedSupport ${HOME}/.bashrc
~ $ grep -n SharedSupport ${HOME}/.profile
~ $ grep -n SharedSupport ${HOME}/.zshrc
~ $ grep -n SharedSupport ${HOME}/.zprofile
~ $ grep -n SharedSupport ${HOME}/.zlogin
Any ideas how to remove it from the $PATH? I uninstalled it so naturally the terminal can't find the folder. Now I can't find where it's being exported to PATH so I can remove it.
Just figured it out! Turns out it stores its paths in a file in /etc/paths.d called Xamarian, like Jonathan Leffler commented. So in order to delete it, we'll need to:
Open a terminal, this can be iTerm, Terminal, etc.
CD Into the Directory
cd /etc/paths.d
Remove the file called xamarian with rm,
rm -rf xamarian
That's it!
In my case, there is a file called workbooks in /etc/paths.d.
Open the file by vim and edit it

Uninstalled Anaconda still shows up in PATH (Mac OS X)

I have installed Anaconda a few months ago but then uninstalled it and removed all anaconda files by using
rm -rf ~/anaconda
but when I run
echo $PATH
it still outputs a path that point to an Anaconda folder but when I search for it, it doesn't even exist, why is that happening?
What makes you think that non-existent directory are automatically
removed from $PATH? They are not. As an example I can make a new dir
and go there:
$ mkdir /tmp/new-path-dir && cd /tmp/new-path-dir
Add it to the $PATH:
$ PATH=/tmp/new-path-dir:$PATH
$ echo $PATH
/tmp/new-path-dir:<REST_OF_PATH>
Make a new olleh.so (hello spelled backwards) executable inside
it:
$ echo 'echo hi' > olleh.so && chmod +x olleh.so
Then go back to ~:
$ cd ~
And start a olleh.so:
$ olleh.so
hi
Now I can safely remove /tmp/new-path-dir:
$ rm -r /tmp/new-path-dir/
And it still will be shown in my $PATH:
$ echo $PATH
/tmp/new-path-dir:<REST_OF_PATH>
But I won't be able to run olleh.so any more:
$ olleh.so
bash: /tmp/new-path-dir/olleh.so: No such file or directory
And as paths to executables are cached by bash I can get rid of
olleh.so permanently like this:
$ hash -r
$ olleh.so
bash: olleh.so: command not found

How can I simply retrieve the absolute path of the current script (multi OS solution)?

I am looking for a simple solution to retrieve the absolute path of the current script. It needs to be platform independent (I want it to work on linux, freebsd, macos and without bash).
"readlink -f $0" works on linux but not on freebsd and macos: readlink
doesn't have the "-f" option.
"realpath $0" works on freebsd and linux but not on macos: I don't have this command.
EDIT :
Solution for retrieve the path of the repository of the script :
DIR="$( cd "$( dirname "$0" )" && pwd )" (source : Getting the source directory of a Bash script from within )
#!/bin/sh
self=$(
self=${0}
while [ -L "${self}" ]
do
cd "${self%/*}"
self=$(readlink "${self}")
done
cd "${self%/*}"
echo "$(pwd -P)/${self##*/}"
)
echo "${self}"
It's «mostly portable». Pattern substitution and pwd -P is POSIX, and the latter is usually a shell built-in. readlink is pretty common but it's not in POSIX.
And I don't think there is a simpler mostly-portable way. If you really need something like that, I'd suggest you rather try to get realpath installed on all your systems.
For zsh scripts, FWIW:
#! /bin/zsh -
fullpath=$0:A

Bash: in directory `directory_name.git`, how to cd to `../directory_name`?

I'm pretty new to Bash scripting and am looking to do the following:
The script's pwd is "/a/b/c/directory_name.git/" and I'd like to cd to "../directory_name" where directory_name could be anything. Is there any easy way to do this?
I'm guessing I'd have to put the result of pwd in a variable and erase the last 4 characters.
tmpd=${PWD##*/}
cd ../${tmpd%.*}
or perhaps more simply
cd ${PWD%.*}
Test
$ myPWD="/a/b/c/directory_name.git"
$ tmpd=${myPWD##*/}
$ echo "cd ../${tmpd%.*}"
cd ../directory_name
*Note: $PWD does not include a trailing slash so the ${param##word} expansion will work just fine.
Try:
cd `pwd | sed -e s/\.git$//`
The backticks execute the command inside, and use the output of that command as a command line argument to cd.
To debug pipelines like this, it's useful to use echo:
echo `pwd | sed -e s/\.git$//`
This should work:
cd "${PWD%.*}"
Didn't expect to get so many answers so fast so I had time to come up with my own inelegant solution:
#!/bin/bash
PWD=`pwd`
LEN=${#PWD}
END_POSITION=LEN-4
WORKING_COPY=${PWD:0:END_POSITION}
echo $WORKING_COPY
cd $WORKING_COPY
There's probably one above that's much more elegant :)
That's what basename is for:
cd ../$(basename "$(pwd)" .git)

Resources