Is moving custom shell script to /usr/bin problematic? - macos

Is it dangerous, insecure or not-so-smart for any other reason to put a custom shell script to /usr/bin and add /usr/bin to $PATH in order to make a custom script executable from everywhere without ./ and the file extension?
Bonus question: can I assign custom icons to custom executable scripts?

Traditionally, /usr/bin is one of the places where operating system binaries are stored. For custom scripts, you'd use /usr/local/bin. This you have to create yourself if it does not exist and add to $PATH, as you mentioned.
Icons are a GUI thing, shell scripts are a CLI thing. They live in separate universes. Nothing prevents you from creating a bridge though. For instance, you can make a shell script and call it foo.command. Opening this from the GUI starts Terminal and runs the script. Since you see the file in the Finder, you can assign it a new icon through the Info pane.
Also, you may want to take a look at the free Platypus application. It allows you to create a full-blown application bundle around a script. The bundle will contain the script, so you won't have to put it in some obscure directory and modify $PATH. If you also need CLI access, this option is less desirable.

I wrote a command line tool for setting the custom icon of a file. You can grab it here.

Related

Creating a mac app which calls a shell script, but developing in ubuntu

I have a shell script, and a tarball. The shell script unpacks the tarball, and makes use of the files inside of for performing a task. I need to make this accessible on mac laptops, but in such a way that there is either a .app or .dmg file that when clicked, ultimately calls that shell script. I found several utilities, that can do this (create such an .app file), such as Platypus, or Appify. However, these require Mac to build the file. The thing is, I must package the .app/.dmg file, in an Ubuntu environment.
Is there any good software for creating a dmg or app file which call a shell script when clicked, but such that the software which can be run in Ubuntu (just for the purpose of creating the file)?
This is not an exact answer to your question but an workaround that might be acceptable if you can't find a better solution.
First, a zip file will automatically extract its content if you double click it in OS X so
tar -cvzf your_filename.zip ...
would create a file that can be easily extracted.
Secondly, if you create a shell script that has the extension .command, but otherwise is like any shell script, it can be run from OS X by double clicking on it (by opening a terminal and executing it there), it would mean an extra manual step for the user but like I said, this is a workaround :)
If you create a .command file, remember to make it executable.

.sh files default to read-only on OS X Yosemite

I am learning my way through shell scripts and just created one with vim.
Every file I create with .sh extension seems to be defaulting to read-only mode for every group in the ls -l command. I have tried creating files with several editors and in several locations and always get the same result.
So my question is, I know i can chmod the files so i can execute them, but is there something i can do to create them executable already and not change every single one of them?
As with any UNIX-like system, file creation is affected by the umask, which masks out file access bits when files are created.
You can change the default umask by editing the shell start-up configuration files, however I wouldn't recommend doing that.
If you want to change it so that all files you create have the executable bit set by default, then what about files that are not executable? I have always worked on shell scripts with the edit, chmod, run cycle and I don't feel it's a big problem.

Setting up ccache in Ubuntu

I am currently following the guide to setting up and compiling AOSP in Ubuntu. The problem is I do not have the best knowledge of Linux/Ubuntu.
The part that currently has me confused is setting up the ccache found at this link https://source.android.com/source/initializing.html.
What I don't really understand is the following, which .bashrc file do I need to edit/add the information to? And can I have more than 1 .bashrc file in Ubuntu? Not really sure what this file really does.
Thanks
.bashrc is a file which is called by bash before on each start of a new shell.
This file can be used to setup the environment, export variables, create aliases and functions. There are usually multiple copy of this file, One per system and one per user to allow system wide configuration but also customization by users ( users bashrc will be called at last and can overwrite things).
here you edid or add line to your won user's .bashrc file
type cd $HOME
vi .bashrc
after that change your file and then you have to compile the file using one of the commands.
..bashrc
or
source .bashrc

Where is the default terminal $PATH located on Mac?

I have been looking throughout the system but I cannot find it. When I do echo $PATH I get the stuff I added, plus the default path. I do not have a .profile, and I do have a .bashrc, but the default path is not in there. I am looking for it just to know where it is located because all the tutorials explain that its in .profile... but what if you don't have one? Where is it located then? Anybody have any ideas?
If you do sudo man path_helper, it talks a bit about how it puts the path together. You might look in /etc/paths and /etc/paths.d. I did, and found what I was looking for.
Many system-wide settings including PATH are set in /etc/profile which is read in by bash at startup. On Mac OS X this file usually uses path_helper to set PATH. This utility in turn reads the information from other system configuration files under /etc (see path_helper manpage).
Note that even if you disable the reading of the initialization files by bash (e.g. with command-line options like --noprofile) it will still inherit the environment of the parent process.
If you start at /etc/profile, it should look something like this:
if [ -x /usr/libexec/path_helper ]; then
eval `/usr/libexec/path_helper -s`
fi
It's testing to see if the file exists and if so, executes it. If you execute it by hand, you'll get something like this:
PATH="/usr/bin:/bin:/usr/sbin:/usr/local/bin:/usr/X11/bin"; export PATH;
I believe that's what you're looking for. So it comes from /etc/profile, which in turn calls an executable that sets the path.
As mentioned in the accepted answer, the $PATH is built by first looking into the content of the file /etc/paths, and then looking into every file in the folder /etc/paths.d. So, the $PATH in the pre-installed bash system installation contains every entry in these files, as well as in other shell types.
However, because in the latest Mac OS versions the default shell is zsh, I followed a couple of tutorials in which the writer avoided to change the $PATH for the bash shell, and simply added new entries to the $PATH by editing ~/.zshrc the following way:
export PATH=/path/available/only/for/zsh/shells:$PATH
The above command adds /path/available/only/for/zsh/shells to the $PATH, and the added path will only be available in zsh shells.
I hope this helps someone who, like me, had too many entries in the $PATH in zsh shells, and couldn't figure out where they were coming from!
The .profile file on Mac is located in your user folder: ~/Users/youruser/
However, the .profile file is hidden. You can press Command+shift+. (command, shift, dot) while on Finder to see them.
There's one important fact I only realized today while debugging a problem: the profile settings (.bash_profile etc.) are only read by login shells. They are not read by the processes that are used to launch your applications.
You launch your applications in diverse ways: click the icon in /Applications, or type the name in Spotlight search, or click an icon in the Dock ... In all those cases, the application itself (i.e the binary or shell script inside the application) is launched by launchd without any parent shell. Meaning that your profile is not run and that your custom settings (PATH, environment variables ...) will be ignored.
That can cause all sorts of trouble, for example if you setup you environment to use a specific version of Java: your application will not see that and use the "default" java, which will be the one with the highest version number.
In my case, the problem is that my application was crashing when run via the application launcher, but runs fine when run from a terminal window ... The reason was that I had a custom path that included some libraries required by the application, but that was not available when the application was run by the launcher.
The solution I used was to symlink the libraries needed into /usr/local/lib

Where do I put mxmlc so that I could just type 'mxmlc' in the terminal to compile a swf file?

I'm on a Mac and I'm trying to make a Vim plugin for compiling/running actionscript files.
First, I need to run mxmlc on the command line, but to do that I have to keep on typing the path to it. Where do I place it so that I don't have to retype the path?
You need to modify your "$PATH" environment variable, so that the tool is in that directory. However, if you want to make this very easy... you can download my macosx-environment-setup.tar.bz2 program. If you execute the "install.sh" script using "sudo ./install.sh", it will setup your environment in such a way that if you use "/Library/Flex4SDK" as the location for the Flex4SDK, it will automatically find it, define FLEX_HOME to point to that location, and it will also ensure that the binaries from the Flex4SDK are in your PATH.
Side Note: This is up on the web, because I use it in my Development Environment Setup How-To Guides. If you aren't too keen about running "sudo ./install.sh", you need to choose a location (I am going to assume "/Library/Flex4SDK", so that the tools are located in "/Library/Flex4SDK/bin"), and then you would simply need to edit your "~/.profile" file (using "nano ~/.profile"), adding the following to the very end:
export FLEX_HOME=/Library/Flex4SDK
export PATH="$PATH":"$FLEX_HOME/bin"
Note that these changes occur in your shell... they will not affect programs that are launched by double-clicking them in Finder. In order to affect those programs, you will need to place the environment variables in a file named ~/.MacOSX/environment.plist. See Automatically build ~/.MacOSX/environment.plist for a script that will automatically generate such a file using the current environment variables defined in your shell.
There are a few ways to answer this:
In one of your directories searched
by PATH (see the list with echo
$PATH)
Add a new directory to PATH
(e.g. in your ~/.bashrc
export PATH=$PATH:/path/to/bindir)
Add an
alias to your program (e.g. in your
~/.bashrc alias
mxmic=/path/to/mxmic)
(I'm assuming you're using bash shell, which is usually the case you can check with echo $SHELL)

Resources