Lost posix utilities on Mac OSX, any ideas on debugging? - bash

Suddenly I lost almost everything on the command line. I can do cd, exit and pwd...nothing else that I tried...no ls, no vi...nothing....
I do have an open terminal that's still working, but no new ones...as soon as I exit out of the current session, I'm screwed...
I was about to say I'm baffled...but I just noticed that now /etc is symlinked to /etc/private. Don't remember that being the case before...
I recently upgraded to Snow Leopard...but I've rebooted since with no problems... No clue when this came about...
Any ideas on debugging it?

It sounds like your PATH environment variable may be messed up. You may have messed it up in a .profile, .bashrc, .tcshrc, .login or similar shell startup file. Check using:
echo $PATH
Then (assuming you are using a bash shell) try setting it with:
export PATH=/usr/bin:/bin
You could also try typing commands using the full path, e.g. /bin/ls. If you want to make sure that your commands are still there you should be able to use echo /bin/* /usr/bin/*.
/etc has always been a symbolic link to private/etc on Mac OS X.

Start with echo $PATH to see what's there. Mine contains:
/Users/jleffler/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:.
On my machine, /etc is a symlink to /private/etc, and /tmp is a symlink to /private/tmp (and isn't accessible to other user IDs).
If that isn't the problem, check whether /usr/bin and /bin are available (they are not symlinked on my machine).

Related

How can I debug $PATH?

There is a directory in my $PATH that I want removed.
But I cannot figure out how it got there.
I've looked at all the places I can think of where $PATH gets set: .profile, .bashrc, .bash_profile, et cetra
but I cannot find who is putting this particular directory into $PATH.
Clearly, I do not know all the things that change $PATH when my system starts.
Is there a way to debug the startup sequence?
Ideally I just want to set a trap on anything that touches $PATH.
Or may there is a log file (or ordered set of log files) I can scan.
How can I find how each directory gets set in $PATH among all the things that run when my system starts?
There are two ways to do this:
Try bash --login -x in a terminal window. This will print every line of the setup scripts as they are executed (kudos go to kojiro).
Alternatively, you could add set -x near the top of /etc/profile or $HOME/.profile. The danger here is that if you make a mistake, your system may become unusable (if you can't start a new shell anymore) plus you need to be root. If you want to try this, I suggest to create two terminal windows, keep them open at all cost, make the changes and then start new terminal windows to see the effects.
Use grep -r PATTERN / (replace PATTERN with the path that you look for). That should search your whole hard disk for this pattern and can take a long time. Maybe start with your home directory. If you want to search just the . files, use this trick: grep -r PATTERN .??*
If you are using BASH, the way the login loading works is this order:
/etc/profile is first read in.
$HOME/.bash_profile is read in.
If $HOME/.bash_profile doesn't exist, then $HOME/.bash_login is read in.
If $HOME/.bash_login doesn't exist, then $HOME/.profile is read in.
NOTE: That $HOME/.bashrc is not read in by default. However, many people do source in their .bashrc file at login.
When a shell is invoked, and it's not a login shell, then the $HOME/.bashrc file is loaded.
By default, the Mac's PATH is /bin:/usr/bin:/usr/sbin:/sbin. This is set via /etc/profile which executes /usr/libexec/path_helper. This takes the initial paths from /etc/paths.d/... and from /etc/paths. The /etc/paths.d only adds in the /opt/X11/bin directory if X11 is installed. Otherwise, /etc/paths is used and it contains the default paths.
Mine is set to:
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
I use /usr/local/bin for third party tools. or newer versions of already installed tools. For example, /usr/bin/svn is version 1.7.17 while /usr/local/bin/svn is set to version 1.8.10. Me invoking svn will invoke the version of Subversion I installed, and not the native version.
In this case, the 1.8 version of Subversion is actually installed in /opt/subversion/bin/svn, but I create a symbolic link to it in /usr/local/svn. I do this for any tool that I install: The tool gets installed in /opt and I link the binaries and scripts to /usr/local/bin, so they're in my $PATH.
Place these 2 lines at top of your ~/.bash_profile (create it if it doesn't exist):
set -o functrace
trap '[[ "$BASH_COMMAND" == *" PATH="* ]] && echo "$BASH_COMMAND"' DEBUG
Then exit the current shell and start a new shell.
It will print every time PATH variable is set in your environment.

Too many bash initialization files?

I just installed GMT4 (Generic Mapping Tools) from MacPorts and have run into some serious issues with terminal...
When using the ls command in terminal I get the following:
-bash: ls: command not found
To fix this I've tried many things that I read many different places online. I've changed the following files to include /usr and /bin:
~/.bashrc
~/.bash_profile
~/.profile
Doing so gave me back the sudo command which I had previously lost.
However, I still am not able to use ls in a new terminal window without manually exporting the path("new session"). This has been causing problems when I'm running shell scripts that use system functions. So I'm starting to wonder what exactly those three files do and if maybe having three files doing the same thing is causing them to interfere with one another. If anyone can tell me where I need to add these PATHS that would be great; I dunno, maybe I can delete one of these files or something ELSE is conflicting. Right now I just run " export PATH=$PATH:/usr" in each new terminal window and it works for that session.
I'm running Mac OS X 10.6 on a early 2011 MacBook Pro with 4GB of RAM.
Background for those who are interested:
My end goal is to use Relax to model viscoelastic relaxation in a mid-oceanic ridge system to come up with some sort of constitutive law or power-relation between slip magnitude and the location of greatest tensional stress in the lithosphere due to normal faulting.
The Bash manual actually explains these files fairly thoroughly. However, the manual does not describe how these mechanisms are usually used in practice. What follows is a brief, abridged version of what is common best practice.
.profile is read at login by both sh and bash.
.bashrc is read by non-login Bash shells. By convention, .bash_profile sources .bashrc in turn.
.bash_profile is read at login by Bash. If this file exists, .profile is not read. By convention, .bash_profile should source .profile if it exists.

STTY may have been deleted?

I was installing ruby/rails/gem and homebrew and got frustrated. I think I deleted the stty folder. ANYWAY, what can I do? It doesn't seem to have ever happened on the internet before.
Running stty comes back with command not found...
The stty command can't be easily deleted; you'd have to be running with administrator privileges. It is supposed to be located in /bin/stty (try ls /bin/stty to make sure it's still there).
My guess is that you altered your shell's $PATH variable (which is a colon-separated list of directories that the shell searches for commands when you run them only by name).
Try this to see your current path: echo $PATH.
Then make sure that :/bin appears somewhere in that value, e.g.
export PATH=${PATH}:/bin
To fix this permanently, make sure that PATH is set in a shell startup file, e.g. .bashrc in your home directory.
I copied the stty from a friends mac. It seems to be working fine. I have run a few commands and there have been no issues yet.

Fullpath in OS X

I am a few steps before starting my first real programming project and I am preparing my machine. I noticed that my fullpath (what I get when I "echo $PATH" in the terminal) does not look like a "normal" one (as I see "usr/bin" quite a lot of times). What does it mean? How does it affect my use of the terminal? And how, if, can I change it back to the default one?
EDIT: I can change it just by typing "PATH=the_name_of_the_path" but it's not permanent, if I quit the session I am running and start terminal again, what I get is "/Applications/lejos_nxj/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin" (and that's because I changed it some months ago so I could usel lejos_nxj, for a course at university). Is it better I change it back to the "normal" again or should I stop worrying about it? How can I change it anyway, in case I had to?
$PATH is a variable specifying a set of directories where executable programs are located. It's normal to see /usr/bin there.
Basically, if you type a command on the terminal, like cat for example, it's going to look for cat in those directories. This way, you don't have to specify the full path to all your frequently used commands.
Here is an example of a "normal" path on a new OS X 10.6 install:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/X11/bin
If you see the same entries repeated again and again, then you probably have a mixup with your .bashrc vs. .bash_profile. You should set the PATH in the .bash_profile, not .bashrc, to avoid this.
$PATH under all *nixes is actually a list of colon-separated directories. Unless you see several times the /usr/bin entry, nothing's wrong (and even if you see it several times, it doesn't mean it's broken either).
At any rate, you should post what you get.
My path looks like this:
frak:~ seth$ echo $PATH
/usr/local/bin:/bin:/sbin:/usr/bin:/usr/sbin
Yours should look pretty much the same. Each directory is separated by ':'. However, even if you do have /usr/bin more than once, it won't make any difference.
Observe:
frak:~ seth$ whereis units
/usr/bin/units
frak:~ seth$ units attoparsecs/s m/s
* 0.030856776
/ 32.407793
Add a /usr/bin again:
frak:~ seth$ PATH=$PATH:/usr/bin
frak:~ seth$ echo $PATH
/usr/local/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/bin
And everything still works fine:
frak:~ seth$ whereis units
/usr/bin/units
frak:~ seth$ units attoparsecs/s m/s
* 0.030856776
/ 32.407793
PATH is a shell variable specifying where to find executables. For example, if you do a ftp (file transfer), the shell will look for the command ftp in those directories in your PATH variable before executing it. There's nothing wrong with that. If /usr/bin is not specified in your PATH, then everytime you need to use ftp, you need to give full path name , eg /usr/bin/ftp
Note that for a normal user, /usr/sbin should not be in PATH because those in /usr/sbin are mostly administrative commands.

In Mac OSX 10.5, it can't find my Terminal commands sudo, find, etc

I don't know what has happened, but in my Terminal on Mac OSX 10.5 it can no longer find my sudo command, or find command, etc. They are there because if I put /usr/bin/sudo or /usr/bin/find it works fine...
My .bash_login file looks like this:
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/usr/bin$PATH"
My .bash_profile file looks like this:
export PATH="/Library/Frameworks/Python.framework/Versions/Current/bin:/Library/Python/2.5/site-packages/django_trunk/django/bin:/usr/local/mysql/bin:/usr/bin/sudo$PATH"
I'll say now, I don't really know what I'm doing with the Terminal. I'm just a beginner to it all, and I must of done something for the environment variables (is that what they're called?) to be lost. I presumed I'd just have to make sure the /usr/bin/ path is in my bash files, but they are, and it doesn't seem to work. Please help!
Also, when I do use the /usr/bin/find command, it says "Permission denied" to me, even though I am logged into Mac OSX as the System Administrator account. I don't understand.
Any help would be grand. Thank you - James
It looks like both of your PATH exports are malformed:
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/usr/bin$PATH"
The end bit there won't work. It should be:
export PATH=/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/usr/bin:$PATH
Notice the colon before '$PATH'? It's important ;)
Also, the double quotes are not necessary.
If this doesn't work, we will need more information. It is possible that something else is modifying your path even after your shell configurations are loaded.
Can you post the results of:
$ echo $PATH
Configuration files are not always a good indication of the current environment variables, since they are modified by many programs and files, all across your system. To see all of your environment variables, you can run:
$ env
This should fix the problem completely and permanently.
first, export environment paths by using below command in the terminal.
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/b‌​in
now you have the commands you want. (eg. try ls. You'll see the command is working). But this is only for the current session. If you close the terminal and open a new one, you will have the previous issue. To make this change permanent, use below command,
go to home directory
cd ~
open .bash_profile file in nano / vim (I'm using nano here)
nano .bash_profile
This will open up nano editor. In a new line, paste the following;
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:$PATH
press 'control'+'o' to save (WriteOut) and 'control'+'x' to exit nano.
All done ! Now try the commands.
Check out --- http://www.sweeting.org/mark/blog/2008/05/26/mac-os-x-tip-setting-path-environment-variables
I went trough the same issue and here is how I solved it.
First of all I reverted the file to its original doing this way
/usr/bin/nano ~/.bash_profile
In my case I was not able to make work any command alias. Even vi or vim didnt work without specifying the full path of that command.
If nano is not installed just replace nano in the command by the editor installed
After that just restart the computer. In my case as I said bellow I could not use any command. When trying to do /usr/bin/source ~/.bash_profile
that command failed. So I had to restart the OS and it worked

Resources