Relative path to executable is not resolved in zsh script - unix

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.

Related

How to change the working directory when running R scripts with Rscript.exe in Windows

I want to execute an R script myscript.R by using Rscript.exe on a Windows machine. However, my default home folder is configured to be \\\\<domain>/home/m/myname/Documents. I've made a wrapper DOS batch script (run.bat) on the same folder where myscript.Rresides (actually a folder in a pendrive) in order to make it the current working directory:
#ECHO OFF
REM This batch file is an interface to run R scripts from the command line
SET RSCRIPT="c:\Program Files\R\R-3.4.3\bin\Rscript.exe"
echo "Changing drive and directory to %~dp0"
pushd "%~dp0"
REM we change "\" to "/"
setlocal enabledelayedexpansion
set f=%*
set "f=!f:\=/!"
%RSCRIPT% %f%
popd
I call the script by run.bat "myscript.R" --dir ../AnotherFolder
So far, so good. However, the problem is that the script needs to change the working folder to a folder one level up (../AnotherFolder) but the setwd() R function fails. By debugging, I can see that the problem is that Rscript.exe loads the script myscript.R while still having the default directory setup to the home folder.
The question is: how the heck I can make Rscript.exe to ignore the default home directory and take the script directory as the current working directory.
Ok, I've found my own answer after scratching my head the whole day. I've modified the wrapper batch file and add an extra command line argument --drive to be processed by my R script:
#ECHO OFF
REM This batch file is an interface to run R scripts from the command line
SET RSCRIPT="c:\Program Files\R\R-3.4.3\bin\Rscript.exe"
echo "Changing drive and directory to %~dp0"
pushd "%~dp0"
REM we change "\" to "/"
setlocal enabledelayedexpansion
set f=%*
set "f=!f:\=/!"
set P=%~dpnx1
%RSCRIPT% %f% --drive "%P:~0,2%"
popd
Then, my R script must execute setwd(drive) where drive contains the value passed through the --drive option (e.g. "F:") before executing a setwd() on a relative path to the current folder.

Change Location of zcompdump Files

Is it possible to change the location of .zcompdump and .zhistory files generated by ZSH?
They are polluting my Home Directory.
One possible solution I found was changing the value of $ZDOTDIR to $HOME/.config/zsh. I did the following, but it does not seem to work (temp files are still created in the home directory).
$ cat ~/.zshenv
ZDOTDIR=$HOME/.zsh
. $ZDOTDIR/.zshenv
.zcompdump files are produced to speed up the running of compinit, Which initializes the shell completion in zsh. You could specify an explicit dump file by compinit -d <dumpfile> in your .zshrc to change the location where compdump are stored.
Change the value for HISTFILE to set different location to store you zsh command history.
$ man zshall
/Use of compinit
To speed up the running of compinit, it can be made to produce a dumped
configuration that will be read in on future invocations; this is the
default, but can be turned off by calling compinit with the option -D. The
dumped file is .zcompdump in the same directory as the startup files (i.e.
$ZDOTDIR or $HOME); alternatively, an explicit file name can be given by
`compinit -d dumpfile'. The next invocation of compinit will read the
dumped file instead of performing a full initialization.

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.

zsh doesn't complete local dir name when trying to execute local executable

I have a local directory with executable in it (e.g. foo_dir/bin/bar). I typed "foo", but foo_dir wasn't suggested. What cames out is other program in $PATH (such as bootlogd).
How should I config my zshrc? Thanks
I think adding "." to your $PATH variable will fix this issue. By default $PATH contains standard bin directory i.e /usr/bin, /bin, /usr/local/bin.
I found a workable solution, by "setopt autocd", but the side effect is when executing some directory, you'll cd into it.

Unix: Getting Export PATH to "Stick"

When setting the export path in Unix, example:
export PATH=$PATH: $EC2_HOME/bin
If I quit terminal and open it back up to continue working, I have to go through all the steps again, setting up the paths each time.
I'm wondering how I can set the path and have it "stick" so my system knows where to find everything the next time I open terminal without having to do it all over again.
Thanks!
Open ~/.bashrc. This file is loaded every time you start up a new shell (if you're using Bash, which most people are). If you're using a different shell, the file may have a different name, like ~/.shrc.
Add the line you need to the bottom of the file:
export PATH=$PATH:$EC2_HOME/bi
Other info rolled up from elsewhere in the thread:
There are multiple places to put this, depending on your shell and your needs. All of these files are in your home directory:
For Bash:
.bashrc (executed when you shart a shell)
OR
.bash_profile (executed when you log in)
For csh and tcsh:
.cshrc
For sh and ksh:
.profile
Add it to your .cshrc file (for csh and tcsh), .profile file (for sh and ksh), or .bash_profile file (for bash)
You need to find your profile file and put that line in there. Suppose you use bash, the profile files are .bashrc and .bash_profile, found in ~. These files will vary depending on which shell you use.
You have to put those commands into one of the "autostart" files of your shell.
For bash this would be .bashrc in your homedirectory (create it if necessary)
add it to your .bashrc or another .bash startup file.
... and for ksh edit .profile.

Resources