GNU ls from Coreutils missing OS X ACL implementation

别来无恙 提交于 2019-12-23 02:41:48

问题


I'm using brew to retrieve and install common GNU versions of terminal commands and utils with brew install coreutils.
Then in my .bash_profile I'm including their PATH with

if [  -d $(brew --prefix coreutils)/libexec/gnubin  ]; then
    PATH="$(brew --prefix coreutils)/libexec/gnubin:$PATH"
fi

so far so good, I can use use the GNU version of coreutils.
The problem comes from ls. Apple implement ACL that is not implemented on GNU ls. I discovered this by banging my head many times and not understanding why (for example) ls -le@ would give me error ls: invalid option -- 'e'.

So now I understood that GNU ls is the problem.

QUESTION:
how can I source all the coreutils BUT ls? I want to use the Apple version of ls but keep on using the rest of the coreutils. How can I achieve this modifying my .bash_profile?

EDIT:

If I create a flag to understand if I am currently using coretuils or not and as a consequence I'll create an alias:

ls_flag=false
if [[  $(brew) &&  -d  $(brew --prefix coreutils)/libexec/gnubin  ]]; then
    PATH="$(brew --prefix coreutils)/libexec/gnubin:$PATH"
    ls_flag=true
fi

export PATH

if [[ ls_flag -eq true ]]; then
   alias ls=/bin/ls
fi

This will work if I stop my .bash_profile here. But another problem arise from the following conditions. I use them to understand if I'm using the GNU ls or the Apple ls and chose the correct option to colorise the ls command:

# Detect which `ls` flavour is in use
if ls --color > /dev/null 2>&1; then # GNU `ls`
    alias ls='ls --color=always'
    # load my color scheme (it only works with GNU ls)
    # dircolors only work with coreutils
    eval `dircolors  ~/.dotfiles/data/dircolors`
else # OS X `ls`
    alias ls='ls -G'
fi

So, at this point ls should be:
1) alias ls=/bin/ls # from the 1st condition ls_flag == true
2) alias ls='ls -G' # from the 2nd condition "if ls --color" (false)

BUT if I prompt ls -@ will still throw an error telling me that I'm still using the GNU ls...wondering why the last alias will override the previous ones...


回答1:


This is wrong

if [[ ls_flag -eq true ]]; then
   alias ls=/bin/ls
fi

You're missing the $ for $ls_flag and -eq is used for numeric comparison within [[ ... ]]

Since "true" and "false" are commands, you want to write

if $ls_flag; then
   alias ls=/bin/ls
fi

or, more tersely

$ls_flag && alias ls=/bin/ls



回答2:


You could create an alias:

alias ls=/bin/ls


来源:https://stackoverflow.com/questions/26585868/gnu-ls-from-coreutils-missing-os-x-acl-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!