Shell Script Source: Not Found - bash

I am running the following simple script to test the block of code:
#!/bin/bash
# Load nvm and install latest production node
source $HOME/.nvm/nvm.sh
nvm install v0.10.12
nvm use v0.10.12
If the file name is test.sh with the above code, I get the following error:
test.sh: 5: test.sh: source: not found
test.sh: 6: test.sh: nvm: not found
test.sh: 7: test.sh: nvm: not found
Based on my review in SO, it appears the error with the script is originating at ==> source: not found. When I type this at CLI myself the code works fine. For some reason I am not able to execute this code through a shell script.
Any advice would be appreciated.

Your shebang line says Bash but the symptom suggests that you are running it with sh, which doesn't have a source command. Superficially, changing source to just a dot will fix that, but of course, if the sourced file contains any Bashisms, it will fail on those instead (perhaps more subtly, with incorrect results but no error message).
#!/bin/sh
# Load nvm and install latest production node
. $HOME/.nvm/nvm.sh
nvm install v0.10.12
nvm use v0.10.12
Usually, it is better to mark the script as executable and have the OS select the correct interpreter through the shebang mechanism; then you don't have to remember whether the script file contains Bash commands or sh commands or Python code or compiled code or whatever, nor notice if this changes from one version to another.
So instead of
sh script
you'd do
chmod +x ./script # the first time
./script
Once the permissions are correct, only the second line is needed. There are some minor additional caveats, which is why some busy developers sometimes give the simplest possible instructions for the immediate problem.

chmod +x $HOME/.nvm/nvm.sh
then
sh $HOME/.nvm/nvm.sh or . $HOME/.nvm/nvm.sh

Related

Bash script to set env variables does not work when using source and only works with bash -c

The oneAPI toolkit provided by Intel requires sourcing of a bash script to add several executables/libraries to $PATH and other environment variables.
For this, the documentation instructs to run a provided script as
source setvars.sh
However, on a fresh Ubuntu 20.04 system (supported by Intel OneAPI), I get the following error.
$ source setvars.sh
:: ERROR: No env scripts found: No "env/vars.sh" scripts to process.
This can be caused by a bad or incomplete "--config" file.
Can also be caused by an incomplete or missing oneAPI installation.
After pouring through forums, I came upon a workaround here for zsh-type shells.
Following those hints, when I run
bash -c 'source setvars.sh'
There are no errors reported and the script runs perfectly. As expected, the env variables are not available after execution of the bash -c command.
One workaround to this, is to do
bash -c 'source setvars.sh; exec bash'
every time I open a new terminal. This is very annoying.
I would like to source setvars.sh somewhere inside .bashrc or .profile and forget about it.
Why does source setvars.sh not work and bash -c 'source setvars.sh' run without errors here.
Answering my own question.
This turned out to be an issue with the cd command being aliased to
cd "$#" && ls. Sourcing the setvars.sh script before the alias definition solved the issue.
Refer discussion on the Intel Communities forum here.

Failing to run external Bash program — /usr/bin/bash: bad interpreter: No such file or directory

I'm trying to run a CLI tool in Linux (Mint) which allows me to edit subtitles. It is named subedit: github link. In order to run it, I've added executable permission with chmod +x and added it to the path in bash. However, when I run it, I get the following error message:
bash: /home/main/Documents/shellTools/subedit/subedit: /usr/bin/bash: bad interpreter: No such file or directory
I'm not very experienced with external bash programs and forgot to do something that would be obvious in hindsight.
When I do echo $PATH this is the output:
/home/main/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/main/Documents/shellTools/subedit/
Could somebody please help?
Assuming bash is installed, (it usually is), change the first line of subedit from:
#!/usr/bin/bash
to:
#!/bin/bash
Or if one would prefer not to edit subedit, try this one-liner covering what Al-waleed Shihadeh suggested:
ln -s "$(which bash)" /usr/bin/bash
It seems that you don't have bash installed, you can verify that by running
which bash
if the above command returns "bash not found", then you need to install it.
In case the above command returns a path, you can use the below command to add a symlink to the expected path
ln -s $(path from the above command) /usr/bin/bash
Use the command termux-chroot ONCE!
If you want to always run at the start of a session, be sure to check if it was never run before.
if [ -z $CHROOT ]; then
CHROOT=1
termux-chroot
fi

ROS installation: no such file or directory

According to ros wiki, to set up environment,
I typed
echo "source /opt/ros/kinetic/setup.bash" >> ~/.bashrc
source ~/.bashrc
The error is
/opt/ros/kinetic/setup.bash:.:8: no such file or directory: /home/pikashun/setup.sh
In ~/.bashrc file, there is the source /opt/ros/kinetic/setup.bash line.
I use Ubuntu on WSL.
How can I improve?
Thank you!
I had the exact same issue. The problem is not due to setup.bash either ~/.bashrc but the shell that you are using. It turned out that you may be using a different shell than bash (i.e., zsh). When you are executing the setup.bash of ROS, zsh interprets the following command (whici is in /opt/ros/kinetic/setup.bash) differently:
_CATKIN_SETUP_DIR=$(builtin cd "`dirname "${BASH_SOURCE[0]}"`" > /dev/null && pwd)
It is setting the _CATKIN_SETUP_DIR to your user directory. That is why you are getting error, cause you using the wrong path:
/home/user/setup.bash instead of /opt/ros/kinetic/setup.bash
To check whether this is the issue of your problem, you can check the shell that you are using by execute the following in the terminal:
echo $0; echo $SHELL
It may return something like:
zsh
/bin/zsh
To switch from zsh to bash, use:
exec bash
Once done this, you can use source without any problem.
And to switch back to your previous shell (assuming that is zsh), just use:
exec zsh
The file /opt/ros/kinetic/setup.bash does nothing but loading /opt/ros/kinetic/setup.sh from the same directory. I might be that you are not running bash (check which terminal you run), or that WSL has some different behavoiour than expected.
However, your can just alter your append command like so:
echo "source /opt/ros/kinetic/setup.sh" >> ~/.bashrc
or in your case, since the entry exists already in your ~/.bashrc, edit the line source /opt/ros/kinetic/setup.bash to source /opt/ros/kinetic/setup.sh
The packages or files were not actually downloaded from the "http://wiki.ros.org/melodic/Installation/Ubuntu". To overcome this error first open terminal
check your directory pwd. If your directory is like /home/'Your PC Name' it won't actually work.
Change the directory : Type cd /
Continue the installation process from start which mentioned in "http://wiki.ros.org/melodic/Installation/Ubuntu"
melodic can change to kinetic or other version if you wish

How to enable shell command completion for gcloud?

I'm trying to install the Google Cloud SDK on a Mac (following https://cloud.google.com/sdk/docs/quickstart-mac-os-x), using the install.sh script:
~/Downloads$ ./google-cloud-sdk/install.sh
In the logged output, I see the following instructions:
==> Source [/Users/kurtpeek/Downloads/google-cloud-sdk/completion.bash.inc] in your profile to enable shell command completion for gcloud.
==> Source [/Users/kurtpeek/Downloads/google-cloud-sdk/path.bash.inc] in your profile to add the Google Cloud SDK command line tools to your $PATH.
Indeed, I find that using the gsutil command still leads to a -bash: gsutil: command not found error, so I still probably have to perform this step.
It is not entirely clear to me, however, what is meant by these instructions. I'm on a Mac and my bash profile is ~/.bash_profile. What lines would I have to add in order to make the command line completion work?
Update
The first time I installed I did not use sudo. Reinstalling with sudo, I get an additional prompt whether to modify my bash profile, which upon accepting leads to the following lines added to my .bash_profile:
# The next line updates PATH for the Google Cloud SDK.
if [ -f '/Users/kurtpeek/Downloads/google-cloud-sdk/path.bash.inc' ]; then source '/Users/kurtpeek/Downloads/google-cloud-sdk/path.bash.inc'; fi
# The next line enables shell command completion for gcloud.
if [ -f '/Users/kurtpeek/Downloads/google-cloud-sdk/completion.bash.inc' ]; then source '/Users/kurtpeek/Downloads/google-cloud-sdk/completion.bash.inc'; fi
However, I still get gsutil: command not found errors.
The trick was to run the install.sh using sudo as described in the update. After that I needed to restart the terminal (as described in the instructions) for the changes to take effect.
For Mac Run below command to run the install.sh :
sudo chmod +x install.sh

Installing Homebrew via shell script

This must be an easy one. I'd like to install Homebrew via a shell script on OS X.
Homebrew's recommended installation from the terminal works,
$ ruby <(curl -fsSk https://raw.github.com/mxcl/homebrew/go)
but if I put the following in a file test.sh,
#!/bin/sh
ruby <(curl -fsSk https://raw.github.com/mxcl/homebrew/go)
then execute it,
$ sh test.sh
I receive the following error:
test.sh: line 2: syntax error near unexpected token `('
test.sh: line 2: `ruby <(curl -fsSk https://raw.github.com/mxcl/homebrew/go)'
What is the correct syntax to use in a shell script to get this to work and why is it different from the command line? Thanks!
It's complaining because sh doesn't have that syntax, but bash does. Use #!/bin/bash instead.
Also, no need to use the sh command to execute shell scripts (that's the whole point of putting the hashbang!). Just chmod +x script.sh and invoke with ./script.sh
When you run bash as sh it emulates sh, which has many fewer features than bash (including one you're trying to use here). Use /bin/bash instead.

Resources