Here is my code:
#!/usr/bin/node
console.log('HELLO')
This file has been named test. No extension. If I try running it in my terminal like so
$ test
There is no output. However, if I run
/usr/bin/node test
I get the desired output:
HELLO
Why isn't my script giving the same results?
When you type a command, and it's not an absolute or relative path (like ../test, or /bin/test, or ~/test), then the shell has to search for the executable. It does this by looking in the directories specified in $PATH. You can print it:
> echo $PATH
/usr/local/bin /usr/bin /bin /usr/sbin /sbin
Notice that the current directory . is not in PATH. This is deliberate: if PATH contained ., then it's possible that a command may be accidentally or maliciously overridden by a file in the current directory.
You can ask which command you'd get:
> which test
/bin/test
That's what's being run, and why there's no output.
To run a command that's not in PATH, use an absolute or relative path:
./test
That should fix your problem.
Related
I am using an ssh account that connects to an external server, i have downloaded through guix some software like samtools and bedtools but when i try to use them in my directory it gives me this error:
-bash: samtools: command not found
In my direcory, however, there is the directry guix.profile and if I go into the bin folder of this, I have everything I downloaded.
What am I doing wrong?
Thank you
enter image description here
To run a file from the shell you need two things:
The shell must find the file
Being in the same directory does not enable the shell to find the file. You have to either supply an absolute or relative path the file, or have the directory in your PATH environment variable
In simplest terms this means instead of
$ samtools
try
$ ./samtools
The relative path tells the shell it wants that file
To run it from another directory, either use the whole absolute path, e.g. /home/yourname/samtools , or move the file into a directory that is on your $PATH
The file needs to be executable
If the file is not executable you will need
$ chmod +x ./samtools
I have a bash script located in /home/http/mywebsite/bin/download.sh
I have a config file in /home/http/mywebsite/config/config.yaml
Now I want to read the yaml file no matter where I execute my script.
The problem:
When I cd into /home/http/mywebsite/bin/ and run ./download.sh everything works.
When I cd into /home/ and run http/mywebsite/bin/download.sh it can not find the config file because of the relative path.
How do I make sure I can always read the config file no matter where I execute the script. It is always located 4 directories up from the script in config/config.yaml
The script looks like this:
#!/bin/bash
# This will give me the root directory of my project which is /home/http/mywebsite/
fullpath="$( cd ../"$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cat ${fullpath}config/config.yaml
This works if I execute it inside the directory where the script is.
If I execute the script from another directory such as /home/ I get the following error:
cd: ../http/mywebsite/bin: No such file or directory
cat: config/config.yaml: No such file or directory
Solution?
If it is possible it would be great with a code snippet that can traverse up a path N amount of times, this would solve my problem. But it is too advanced for me.
For example you can set a variable "cd_up=1" how many times to go up. The run the loop/sed or whatever magic.
And it would turn the absolute string from:
/home/http/mywebsite/bin/
into:
/home/http/mywebsite/
And changing it to 2 it would change the string to:
/home/http/
Managed to solve it finally by using:
#!/bin/bash
cd "$(dirname "$0")"
BASE_DIR=$PWD
# Root directory to the project
ROOT_DIR=${BASE_DIR}/../
cat ${ROOT_DIR}config/config.yaml
This allows me to execute the script no matter where I am.
You can use which command to determine absolute path of the executing script no matter where you are running
BASE_DIR=$(which $0 | xargs dirname)
ROOT_DIR=${BASE_DIR}/../..
cat ${ROOT_DIR}/config/config.yaml
Lets try printing the path from different locations.
-bash-4.1$ /tmp/dir.sh
$0 - /tmp/dir.sh. Absolute path - /tmp
-bash-4.1$ cd /tmp
-bash-4.1$ ./dir.sh
$0 - ./dir.sh. Absolute path - /tmp
-bash-4.1$
-bash-4.1$ cd /usr/bin
-bash-4.1$ ../../tmp/dir.sh
$0 - ../../tmp/dir.sh. Absolute path - /tmp
I have used export before but I don't know why when I set the variable PATH to any directory this time, ls, awk commands are not found but no problem with pwd, cd
export PATH="/Users/carolW/Desktop"
ls
-sh: ls: command not found
Use:
export PATH=/Users/carolW/Desktop:$PATH
You're removing all the normal directories from your path, so it only looks in your Desktop folder for everything. You just want to add your directory, not replace the entire path with it.
most probably because pwd and cd are built shell command
(you can test:
which pwd
which ls
)
However, ls are such are tools you can find in /bin directory or such, and those paths are defined in your variable PATH.
If you clear the variable PATH, most likely you won't find your tool anymore.
You may use export PATH=$PATH:"/Users/carolW/Desktop"
So that you concatenate your path to the already existing paths
I created a simple bash script. The script works just fine.
When I run echo $PATH this prints my paths, I have:
/usr/local/sbin:/usr/local/bin/:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
So i moved my script to /usr/local/bin and ran chmod +x mybash.sh. I've even chmod 0777 mybash.sh
Now, when I run ./mybash.sh I just get the "No such file or directory"
Why is this happening and where's the best place to put my scripts
Once the script is in your path, you can run it just with the filename: mybash.sh rather than the path to the file: ./mybash.sh
./mybash.sh means run mybash.sh from the current folder. If you've moved mybash.sh to /usr/local/bin, then it's no longer in ./ (your current folder), so it can't find it.
Either move to /usr/local/bin to run it using ./mybash.sh or just use mybash.sh from any folder once you've moved it into a path folder.
I have installed Google's Go language on FreeBSD 8.1, added the path to the compiler/interpreter to my PATH in .profile and now am trying to reload it.
$ . ~/.profile
And here is what i get:
/usr/local/bin/.: Permission denied.
I also tried to use source instead of ., but that gives an error per line of the file, like HOME=/root: Command not found.
What can I do about it? And is this the right place to ask such questions?
I believe that the right way to extend the user's path is to edit user's shell configuration file. The default shell in FreeBSD for regular users is tcsh, for which you can use the ~/.cshrc configuration file.
To modify the PATH environment variable, create or edit .cshrc file in your home directory and put there the following example line:
set path = (/sbin /bin /usr/sbin /usr/bin /usr/games /usr/local/sbin /usr/local/bin $HOME/bin)
Then run:
$ source ~/.cshrc
verify your setting with:
$ echo $path
/sbin /bin /usr/sbin /usr/bin /usr/games /usr/local/sbin /usr/local/bin /home/danger/bin
The default interactive shell on FreeBSD is tcsh. Its syntax is different from that of sh. It is only the latter that uses ~/.profile.
The right way to extend the path is to modify it in the default profile in /etc/login.conf. Then run cap_mkdb /etc/login.conf as root, log out and log in again.