Score:0

If-else with non empty dir

gs flag

I would like to install something from github in a bash script but I try to verify if the folder exists or not:

BLUE "Installing LS_COLORS..."

if [ ! -d "~/opt/LS_COLORS" ]

then
        git clone https://github.com/trapd00r/LS_COLORS.git ~/opt/LS_COLORS && cd ~/opt/LS_COLORS
        cat lscolors.sh >> ~/.bashrc
        source ~/.bashrc
else

        GREEN "LS_COLORS already installed"

fi

The problem is that the LS_COLORS dir does not empty and this is not working properly. I can not get into the else part.

David avatar
cn flag
Does not empty or is not empty?
Artur Meinild avatar
vn flag
Never EVER use relative paths in scripts (never use `~`). Please rewrite your script with absolute paths, and revise your post with the complete script with absolute paths.
Natan avatar
ru flag
@ArturMeinild please add *why* a behavior is wrong - instead of just saying it is wrong - to make your comment more constructive.
Artur Meinild avatar
vn flag
Further explanation: The best strategy is to define every directory you need as a variable in the beginning of the script, and then refer to the variables as you go along. Then you'll be sure that every file and directory is referenced correctly.
Artur Meinild avatar
vn flag
With relative paths, you rely on correctly using `cd` throughout the entire script, which is not as reliable.
hr flag
The problem isn't the use of `~` per se - it's the fact that it's quoted. See for example [Why isn't tilde recognised as home folder in this case?](https://askubuntu.com/questions/1192981/why-isnt-tilde-recognised-as-home-folder-in-this-case)
Artur Meinild avatar
vn flag
Then there are 2 related issues - 1) that it doesn't expand, and 2) that use of relative paths is unreliable in general. Using absolute paths solves both issues.
hr flag
@ArturMeinild *when expanded by the shell*, `~` is as absolute as `$HOME` is - see for example [Is `~/Documents` a relative or an absolute path?](https://unix.stackexchange.com/questions/221970/is-documents-a-relative-or-an-absolute-path)
Artur Meinild avatar
vn flag
You're right, if this is a "per user" script, then `$HOME` would be the best option.
Score:0
gs flag

It looks like the path was not correct, I added the full path all the places:

BLUE "Installing LS_COLORS..."
if [ ! -d "/home/torabi12/opt/LS_COLORS/" ]
then
      git clone https://github.com/trapd00r/LS_COLORS.git /home/torabi12/opt/LS_COLORS
      cat /home/torabi12/opt/LS_COLORS/lscolors.sh >> ~/.bashrc
      source ~/.bashrc
else
      GREEN "LS_COLORS already installed"
fi

and now it is working. Thank you for all the replies.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.