osx - How can I install my script into the user's $HOME/.bin (or $HOME/bin) directory and make sure that is in their PATH?

07
2014-07
  • Jason Livesay

    Edit: places where I said $HOME/.bin I should have written $HOME/bin or any equivalent is fine.

    So I have a bash script which I am distributing as a client for my API. Current version installs like this curl -s http://api.blah.com/install | sudo sh. I may try to deal with six different package management systems so that they can just apt-get or brew install at some point, but for now I am going with the one-liner since I want this solution to work for multiple systems. However, apparently there are quite a few users on systems like cygwin or even Macs who don't have sudo at all or don't have it set up.

    My install script just downloads the API client script and puts it in /usr/bin and makes it executable. But I am thinking based on the issues with sudo and the fact that this doesn't really need to be installed globally, I would like to just install it into the user's $HOME/.bin folder (create that if it doesn't exist) and then make sure that $HOME/bin or $HOME/.bin is in their PATH.

    This is my current install script:

    #!/bin/bash
    BASE="https://api.blah.com"
    
    if ! hash curl 2>/dev/null; then
      echo "Please install curl. On Ubuntu/Mint/Debian, command is sudo apt-get install curl"
      echo "Trying to install curl.."
      sudo apt-get install curl
      if ! hash curl 2>/dev/null; then
        echo "Yeah couldn't install curl.  Please install it."
        exit 1 
      else
        echo "curl installed. Continuing."
      fi
    fi
    
    sudo bash -c "curl -s $BASE/mycmd > /usr/bin/mycmd"
    sudo chmod uga+x /usr/bin/mycmd
    

    Three reasons I am asking this question rather than just starting to hack on a solution right away: 1) I think that this is a common requirement or desire (installing something non-globally without requiring privileges) and so the answer to this question will probably be useful to someone else. 2) since this is supposed to work on the majority of systems, I believe there are going to be a few wrinkles, perhaps ones that I am not aware of since I don't use all types of systems regularly, just Ubuntu. 3) I believe that at least a few users out there may have already solved this problem.

    Regarding 2), first wrinkle that occurs to me is that many users are now in zsh. So if I add or modify a line in ~/.bashrc that updates the PATH to include $HOME/.bin, that won't work for those systems.

    So to restate the question, what code should I use to download and install my script into the user's $HOME/.bin or ($HOME/bin) directory and make sure that is in their PATH, with the requirement (or at least strong desire) that this will work on almost everyone's system (if they have something like a Unix prompt)?

    Also, one more meta statement about this question.. I am shortening it.. if you object completely to the approach, please just don't answer, rather than giving me a lecture. For example I am aware that some people don't like one-line installs. If you have minor changes to the approach however, please do answer.

    Thanks very much!

  • Answers
  • Nevin Williams

    If you can access the user's $PATH variable, try installing your script in a (the?) directory owned and executable by the $USER.


  • Related Question

    linux - Unable to install bin files.(No such file or directory error)
  • rogerstone

    I wanted to install adobe reader on my ubuntu 10.10(Maverick Meerkat).I have downloaded the file and copied it on my desktop.I had then browsed to the desktop directory through command line terminal.

    I had tried all the possible combinations of the commands but still i get a "file or directory does not exist error"

    roger@ubuntu:~/Desktop$ chmod a+x AdbeRdr9.4-1_i486linux_enu.bin
    roger@ubuntu:~/Desktop$ sudo ./AdbeRdr9.4-1_i486linux_enu.bin
    sudo: unable to execute ./AdbeRdr9.4-1_i486linux_enu.bin: No such file or directory

    I tried without the sudo and this is what i get

    roger@ubuntu:~/Desktop$ chmod a+x AdbeRdr9.4-1_i486linux_enu.bin
    roger@ubuntu:~/Desktop$ ./AdbeRdr9.4-1_i486linux_enu.bin
    bash: ./AdbeRdr9.4-1_i486linux_enu.bin: No such file or directory

    The file is present in the desktop.Where am i going wrong?

    P.S:This is not a duplicate of the question Cannot install .bin package on Ubuntu


  • Related Answers
  • Mikel

    What does

    file AdbeRdr9.4-1_i486linux_enu.bin
    

    say?

    Try

    sudo bash AdbeRdr9.4-1_i486linux_enu.bin
    

    Or are you on a 64-bit system?

    Try installing the 32-bit compatibility libraries.