Error while making symbolic links for nvim - unix

So I was making a symbolic links for nvim so that if i use vim command it opens nvim.
So I used this code
ln -s (which nvim) /usr/local/bin/vim
but when i ran the command it showed this error
zsh: unknown file attribute: h
so plz tell me my mistake and solution for that

You need to put the path to nvim as the first parameter, i.e.
ln -s =nvim /usr/local/bin/vim
In general, =foo expands to the absolute path of the command foo (by using PATH search).

Related

zsh command not found issue

I installed sublime text 2 and created a symlink to it and placed it in ~/bin. I added ~/bin to PATH variable in ~/.zshrc.
If I try to execute subl (sublime's symlink), I get:
zsh: command not found: subl
But if I execute ~/bin/subl, it works correctly.
Echoing the PATH shows that ~/bin is in the PATH variable.:
~/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Any idea what can cause the command not found issue?
bash interprets ~ in PATH, but most shells do not.
Use $HOME instead.

Trying to set up sublime text 2 to open files in unix terminal but getting an error

I'm trying to set up sublime text 2 to open a file in my cygwin terminal by using the command 'subl [file]'. I run the below command in my terminal as instructed in a number of sites
ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" /bin/subl
However, I get this error:
ln: failed to create symbolic link `/bin/subl': File exists
How do I work around that?
Perhaps you could try using 'alias', instead?
Add a line to your .bashrc file similar to:
alias subl='/cygdrive/c/Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl'
Then do a 'source .bashrc' command to load the new alias.
Try this on a Mac:
sudo ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /bin/subl
You're on Windows, right? That directory you're trying to point to is the directory for the Mac OS install of Sublime Text 2. You'll probably want something more like this, though adjust the path to sublime_text.exe to match where yours is:
ln -s /cygdrive/c/Program\ Files/Sublime\ Text\ 2/sublime_text.exe /usr/local/bin/subl
Make sure /usr/local/bin is some directory in your PATH (echo $PATH to find out) so that your subl command will work from anywhere. Then you will be able to do subl . from any project directory to open that project in Sublime.

shebang line not working in R script

I have the following script
#!/usr/bin/Rscript
print ("shebang works")
in a file called shebang.r. When I run it from command line using Rscript it works
$ Rscript shebang.r
but when I run it from the command line alone
$ shebang.r
It doesn't work. shebang.r command not found.
If I type (based on other examples I've seen)
$ ./shebang.r
I get permission denied.
Yes, Rscript is located in /usr/bin directory
Make the file executable.
chmod 755 shebang.r
In addition to Sjoerd's answer... Only the directories listed in the environment variable PATH are inspected for commands to run. You need to type ./shebang.r (as opposed to just shebang.r) if the current directory, known as ., is not in your PATH.
To inspect PATH, type
echo $PATH
To add . to PATH, type
export PATH="$PATH:."
You can add this line to your ~/.bashrc to make it happen automatically if you open a new shell.

How to find path from where current process/executable is running?

I am running some executables while connected to a local unix server box.
Say, I'm trying to run an executable 'abc'. Now the server might have provided an alias for 'abc'.. How do I get to know of this path? As in, if I invoke 'abc', it might actually run it from, say, /opt/corp/xyz/abc .. How do I get to know from what path I'm invoking the executable?
By the way I'm running on HP-UX :D
"which abc" to show which abc you would be calling
or "alias" to list aliases
perhaps "echo $0" from inside a script, or retrieving argv[0] some other way.
If you are running using the PATH environment variable, you could try:
$ which abc
or
$ whereis abc
If there is a symbolic link for the command and you want to know the "real" target, you can use something like:
$ readlink /opt/corp/xyz/abc
I do not have access to an HPUX system in front of me right now, but this should work:
$ ls -l /opt/local/bin/wish
lrwxr-xr-x 1 root admin 22 Feb 3 21:56 /opt/local/bin/wish# -> /opt/local/bin/wish8.5
$ readlink /opt/local/bin/wish
/opt/local/bin/wish8.5
If the command is based on an alias, the following will reveal the alias definition.
$ alias abc
depending on how your system is configured, the above commands should provide answers to multiple variations of your question.
in Perl:
$running_script = $0;
from Python, see SO How to get filename of the __main__ module in Python?
Does HP-UX have the "which" command? Run:
which abc
If you have it, the which command will tell you which abc program will run from your $PATH.
Thanks all!
'which' was the commmand I was after! I'm facepalming myself now as I had already known the command (in Ubuntu)..
And it does work like a charm in HP-UX!
EDIT : 'whereis' suggested by popcnt is even more appropriate!
Thanx a lot man!
From a command line terminal:
$ which abc
/opt/corp/xyz/abc
The correct way to get the path of a script on Unix is:
dir=$(cd $(dirname "$0"); pwd)
Background: $0 is the filename+path of the script relative to the current directory. It can be absolute (/...) or relative (../, dir/...). So the $(dirname "$0") returns the path (without the filename). Mind the quotes; "$0" can contain spaces and other weird stuff.
We then cd into that directory and pwd will return the absolute path where we end up.
Works for ksh and bash.
In a C program, you should check argv[0]. I'm not sure whether the shell will put the complete path in there. If you have trouble, I suggest to wrap your executable in a small script which prepares the environment and then invoke your executable with:
exec "$dir/"exe "$#"

`(cd X; pwd)` sometimes returns two-line

I have shell script which starts with:
sdir=`dirname $0`
sdir=`(cd "$sdir/"; pwd)`
And this usually gets expanded (with 'sh -h') into
++ dirname /opt/foo/bin/bar
+ sdir=/opt/foo/bin
++ cd /opt/foo/bin/
++ pwd
+ sdir=/opt/foo/bin
but for single user for single combination of parameters in expands into (note two lines at the result sbin value)
++ dirname bin/foo
+ sdir=bin
++ cd bin/
++ pwd
+ sdir='/opt/foo/bin
/opt/foo/bin'
I tried different combinations but was not able to reproduce this behavior. With different input parameters for that user it started producing correct single line result. I am new to shell scripting, so please advice when such (cd X; pwd) can return two line.
it was observed on CentOS, but not sure it that matters. Please advice.
The culprit is cd, try this instead
sdir=`dirname $0`
sdir=`(cd "$sdir/" >/dev/null; pwd)`
This happens because when you specify a non absolute path and the directory is found in the environment variable CDPATH, cd prints to stdout the value of the absolute path to the directory it changed to.
Relevant man bash sections:
CDPATH The search path for the cd command. This is a
colon-separated list of directories in which the
shell looks for destination directories specified
by the cd command. A sample value is ``.:~:/usr''.
cd [-L|-P] [directory]
Change the current working directory to directory. If
directory is not given, the value of the HOME shell
variable is used. If the shell variable CDPATH exists,
it is used as a search path. If directory begins with a slash,
CDPATH is not used.
The -P option means to not follow symbolic links; symbolic
links are followed by default or with the -L option. If
directory is ‘-’, it is equivalent to $OLDPWD.
If a non-empty directory name from CDPATH is used, or if ‘-’
RELEVANT -\ is the first argument, and the directory change is successful,
PARAGRAPH -/ the absolute pathname of the new working directory is written
to the standard output.
The return status is zero if the directory is successfully
changed, non-zero otherwise.
OLDPWD The previous working directory as set by the cd
command.
CDPATH is a common gotcha. You can also use "unset CDPATH; export CDPATH" to avoid the problem in your script.
It's possible that user has some funky alias for "cd". Perhaps you could try making it do "/usr/bin/cd" (or whatever "cd" actually runs by default) instead.
Some people alias pwd to "echo $PWD". Also, the pwd command itself can either be a shell built-in or a program in /usr/bin. Do an "alias pwd" and "which pwd" on both that user and any user that works normally.
Try this:
sdir=$( cd $(dirname "$0") > /dev/null && pwd )
It's just a single line and will keep all special characters in the directory name intact. Remember that on Unix, only two characters are illegal in a file/dir name: 0-byte and / (forward slash). Especially, newlines are valid in a file name!

Resources