nodejs active on boot with nvm $HOME/nvm/nvm.sh - node.js

I installed node with nvm it works fine, but every new session i need to do source $HOME/nvm/nvm.sh to get node active
How can I do to be active on boot so there is no need to type source $HOME/nvm/nvm.sh in every sssh session
I try to add this command source $HOME/nvm/nvm.sh to /etc/rc.local but I get the following error
> /etc/init.d/node_start.sh: 13: root/nvm/nvm.sh: Bad substitution
> /etc/init.d/node_start.sh: 78: root/nvm/nvm.sh: [[: not found
> /etc/init.d/node_start.sh: 78: root/nvm/nvm.sh: [[: not found
> /etc/init.d/node_start.sh: 129: root/nvm/nvm.sh: [[: not found
> /etc/rc.local: 14: /etc/rc.local: source: not found
But if i done that command on shell works normally
Any help is welcome
Regards

You should add the line below to the end of ~/.bashrc file:
[[ -s /home/$USER/.nvm/nvm.sh ]] && . /home/$USER/.nvm/nvm.sh

The init script is running as root, not your user. If you want to source your own nvm.sh, you shouldn't use $HOME, but rather just use the absolute path like /home/vitor/nvm/nvm.sh. Also the init script is probably running under dash so you should not use the [[ construct. Just use [ instead as described in this dash as /bin/sh article
So I went and read the nvm.sh source code from https://github.com/creationix/nvm/blob/master/nvm.sh
I don't think that is compatible with dash. So I suggest you leave nvm.sh elsewhere in the filesystem and in your init script just do
exec /bin/bash /root/nvm/nvm.sh
Starting node_start.sh with #!/bin/bash may also be sufficient.
You may also want to subscribe to issue 168 on github for nvm where others hit this error.

I expect your init script starts with #!/bin/sh when it needs to start with #!/bin/bash. Of course, there may be many good reasons not to write init scripts in Bash...

Related

How to reload /etc/environment from shell script

So I have this shell script that checks and then concats an environmental variable to /etc/environment, then reloads the file without having to logout/login:
#!/bin/sh
portvar="PORT=5000"
echo $portvar
grep -q $portvar /etc/environment && echo "EV already in" || echo $portvar >> /etc/environment
set -a; source /etc/environment; set +a;
When I run it, I get the error ./test.sh: 5: ./test.sh: source: not found. However, if I run set -a; source /etc/environment; set +a; directly in the terminal it updates the environmental variable just fine. I have no idea what the set command does, I just found it in another stack overflow question.
Any idea why it runs in the terminal directly but not in the .sh file?
Thanks
/bin/sh on your system is likely some shell that isn't bash and doesn't implement the source command. On my Ubuntu 20.04 system /bin/sh is actually dash.
The source command is not defined by POSIX as part of the shell command language nor is it one of the required special built-in utilities. It's a non-standard feature provided by bash. However, the . command, which does the same thing, is specified by POSIX.
So you can use . instead, e.g. . /etc/environment. Or if you want to keep using source, then you need to have your script run by bash or some other shell that supports it, by changing the shebang line to #!/bin/bash.
There is a tool called checkbashisms that can help you find unintentional uses of bash-specific features in your scripts. When run on your script, it flags this:
possible bashism in foo.sh line 5 (should be '.', not 'source'):

(oh-my-)zsh behaviour: `$ command_name` at home directory results in `cd command_name` effect

I'm currently using zsh with oh-my-zsh, and I've run into an annoying shell behaviour.
I must have done a subtle yet breaking change in the $PATH export while editing my .zshrc, because the following thing happens:
At the shell prompt while in ~/, issuing, for example, the flutter command...
$ flutter
...results in:
$ ~/flutter/
(as if calling $ flutter had been interpreted as $ cd flutter)
However, issuing $ flutter in any other directory, including $ ~/flutter results in the correct execution of the flutter command. Same thing for go and others.
Line 2 on my .zshrc exports $PATH in the following fashion:
export PATH=$HOME/bin:$HOME/.emacs.d:$HOME/flutter/bin/cache/dart-sdk:$HOME/flutter/bin/cache/dart-sdk/bin:$HOME/.pub-cache/bin:$HOME/.composer/vendor/bin:$HOME/.cargo/env:$HOME/.platformio/penv/bin:$HOME/flutter/bin:$PATH
I've been comparing .zshrc versions with other backups, and I may be missing something, but no differences were found.
What seems off, in your opinion?
If you've encountered this annoyance before, how did you correct it?
Explanation
It'a feature called AUTO_CD.
AUTO_CD (-J)
If a command is issued that can’t be executed as a normal command, and the command is the name of a directory, perform the cd command to that directory.
http://zsh.sourceforge.net/Doc/Release/Options.html#Changing-Directories
AUTO_CD is enabled by oh-my-zsh in file oh-my-zsh/lib/theme-and-appearance.zsh.
...
setopt auto_cd
setopt multios
setopt prompt_subst
[[ -n "$WINDOW" ]] && SCREEN_NO="%B$WINDOW%b " || SCREEN_NO=""
...
Solution
Append the following command in ~/.zshrc after oh-my-zsh is loaded to disable this feature.
unsetopt AUTO_CD

How to solve bash error "syntax error at line 3: 'CYBER_UNAME=$' unexpected"?

This error happens when I run a software containing bash script with beggining like this:
#! /bin/sh
CYBER_UNAME=$(uname)
CYBER_UNAME_M=$(uname -m)
I tried to execute these two commands in terminal and it works fine. This error only happens when I run the shell script. What should I do?
The result of 'uname' is SunOS. This shell script cannot be modified since it's protected on our server.
The line
#! /bin/sh
should read:
#!/bin/bash
So, that script will probably never really work.
If you cannot modify the script in situ, you might want to copy it to your local directory and correct it.
Otherwise,
tail +2 scriptname|/bin/bash
might work.

Program runs fine from the terminal, but does not work from a shell script

I am on Linux Mint.
I have program called samtools, and it is stored in a folder on my desktop. I've added path to the executable into $PATH variable. In other words my local ~/.bashrc file has a line:
export PATH="~/Desktop/samtools/samtools-1.1:$PATH"
Executable file named samtools is in this folder.
So when I try to launch it from a command line like a simple command, by just typing "samtools" it works. It also works when I type direct path to the executable in the command line.
However when I try to launch it from a shell script, it does not launch and says either No such file or directory or not found.
Actually, I am trying to use another software which uses some shell scripts to preprocess some data. The error I am getting looks like this:
Indexing...
./RD_capture//process_one_capture.sh: 17: ./RD_capture//process_one_capture.sh: samtools: not found
Sorting...
./RD_capture//process_one_capture.sh: 20: ./RD_capture//process_one_capture.sh: samtools: not found
Piling up...
./RD_capture//process_one_capture.sh: 23: ./RD_capture//process_one_capture.sh: samtools: not found
Shell code looks like this:
echo "Indexing..."
samtools index $INPUTDIR/$sample.bam
#Then we sort them
echo "Sorting..."
samtools sort $INPUTDIR/$sample.bam $TMPDIR/$sample.sorted
#Finally we pile them up
echo "Piling up..."
samtools mpileup $TMPDIR/$sample.sorted.bam | cut -f 1-4 > $OUTPUT/$(basename $sample .bam).pile
Can anyone help me to solve this problem?
It looks like Linux is not able to find samtools.
To solve this use:
Declare this in the beginning (change pwd with the path that leads to executable from pwd)
export set CURRENT_DIR=`pwd`
and while calling samtools use
$CURRENT_DIR/samtools
Note 1: you are responsible to tell the exact path to shell script as shell scripts execute from /usr/bin/sh. try echoing the path on the console with the command, if this does not help, as follows:
echo `pwd`
Note 2: the use of back quote above that is located on left hand side top corner of your keyboard.
Note 3: the export set is used to store global variables in a shell script so that commands that appear after this command can use this variable and update it when ever required.

Lost in Nodester Installation

I am trying to install my own version of Nodester. I have tried on Ubuntu 12.04 LTS and now with CentOS. I am not the most skilled Linux user (~2 months use) so I am at a loss at this point.
The instructions are located at https://github.com/nodester/nodester/wiki/Install-nodester#wiki-a. They ask you to "export paths (to make npm work)" with the lines necessary to accomplish this.
cd ~
echo -e "root = ~/.node_libraries\nmanroot = ~/local/share/man\nbinroot = ~/bin" > ~/.npmrc
echo -e "export PATH=3d9c7cfd35d3628e0aa233dec9ce9a44d2231afcquot;\${PATH}:~/bin3d9c7cfd35d3628e0aa233dec9ce9a44d2231afcquot;;" >> ~/.bashrc
source ~/.bashrc
I can accomplish all of this until I get to the source ~/.bashrc line. When I run that, I get the following:
[root#MYSERVER ~]# source ~/.bashrc
-bash: /root/.bashrc: line 13: syntax error near unexpected token ';;'
-bash: /root/.bashrc: line 13: 'export PATH=3d9c7cfd35d3628e0aa233dec9ce9a44d2231afcquot;${PATH}:~/bin3d9c7cfd35d3628e0aa233dec9ce9a44d2231afcquot;;
I have tried changing the quot; to " and that didn't help. I tried changing quot; to colons and that didn't help. I also removed that and it didn't help (I am sure many of you at this point are probably wondering why I would even try those things). Does anyone have any insight as to what I need to do to get this to run properly?
This looks more like a documentation error than your failing to understand anything. Created issue 409 for it on Github.
In the meantime, going back through the wiki page's history, I found the following which may help. It certainly looks more reasonable though I won't be able to confirm for some time.
Export paths (to make npm work):
> cd ~
> echo -e "root = ~/.node_libraries\nmanroot = ~/local/share/man\nbinroot = ~/bin" > ~/.npmrc
> echo -e "export PATH=\"\${PATH}:~/bin\";" >> ~/.bashrc
> source ~/.bashrc
From commit d71e66b4c96f04c13467ede5f6469d6d4dd17059

Resources