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.
Related
Find command works when executed in terminal separately.. but throwing
ksh : find : not found [no such file or directory]
when executed inside a shell script.
Eg:
find **and** -mtime -1 -type f works
But not when used inside a simple script
Find the path of the find command using
which find
export the path environment variable by appending the path of the find executable from the above command and run your script
export PATH=$PATH:/<path_to_find>
script.sh
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.
I have a personal scripts folder: ~/google_drive/code/scripts. This folder is on my $path. $path is set in ~/.zshenv and not changed anywhere else (I've disabled OS X path_helper and I don't touch $path in any other zsh startup file). In this scripts folder, there is a subdirectory called alfred_workflows. From the command line, from any location I am able to run scripts in this folder with relative paths. This is expected:
$ alfred_workflows/test.sh
#=> test successful
But in a script, this does not work. It generates an error:
$ zsh -c "alfred_workflows/test.sh"
#=> zsh:1: no such file or directory: alfred_workflows/test.sh
Once again, the scripts directory that contains alfred_workflows is on $path, which is set in ~/.zshenv, and I am able to run executables that reside in the top level of this directory from a script. The issue only seems to be when I try to go through a subdirectory. What might be the problem?
Searching of the $path is only done for names containing a slash if the
path_dirs option is set. Apparently that's set in your interactive shell,
but isn't set in the process that's executing the script.
I'm writing a script that will print the file names of every file in a subdirectory of my home directory. My code is:
foreach file (`~/.garbage`)
echo "$file"
end
When I try to run my script, I get the following error:
home/.garbage: Permission denied.
I've tried setting permissions to 755 for the .garbage directory and my script, but I can't get over this error. Is there something I'm doing incorrectly? It's a tcsh script.
Why not just use ls ~/.garbage
or if you want each file on a separate line, ls -1 ~/.garbage
backtic will try to execute whatever is inside them. You are getting this error since you are giving a directory name within backtic.
You can use ls ~/.garbage in backtics as mentioned by Mark or use ~/.garbage/* in quotes and rely on the shell to expand the glob for you. If you want to get only the filename from a full path; use the basename command or some sed/awk magic
What does it mean in Unix when you use . ./<filename>?
Thanks for the help
". ./?" would try and run a program called '?' which would reside in the current directory and it would be run in the current shell.
The first dot means 'run in current shell' (rather than spawning a new one)., the './' means 'current directory' and '?' would mean an executable file called '?' would have to exist.
Running . on a filename runs the commands in the file as though you typed them at the shell command prompt. Unlike a shell script, environment variable (and similar) changes produced by the file persist beyond running the file; the changes made by a shell script are reverted when the script finishes.
The . or source command reads the given file into the current shell. I.e. basically the given file is a shellscript which is run by typing . filename, however using . (or source, which is equivalent) differs from running the file ordinarily as a shell script in that it doesn't spawn a subshell and thus retains variables that are exported by the script. So if the script sets and exports variables, they will still be set when the script finishes.
source or . take a file as parameter. Every line of code in that file is executed. So I don't think that
. ./
would work.
$ . ./
-bash: .: ./: is a directory
$ echo "echo Hello" > out
$ . out
Hello
$ source out
Hello