V
V
Vadim2019-11-19 20:21:19
linux
Vadim, 2019-11-19 20:21:19

How to load environmental variables for a new user?

Good to all,
I can not install node for a new user in any way. Install using bash script as root
./bash_script.sh newusername
---------------------------------
#!/ bin/bash
if [ "$#" -ne 1 ]; then
echo "Incorrect number of arguments"
exit 1
fi
if ; then
echo "This script must be run as root"
exit 1
fi
newuser=$1
useradd -m -s /bin/bash $newuser
usermod -aG sudo $newuser
sudo -H -u ${newuser} bash -c "curl -o - https://raw.githubusercontent.com/nvm-sh/nvm/v0.35... | bash"
sudo -H -u ${newuser} bash -c "
sudo -H -u ${newuser} bash -c "nvm install 10.3"
--------------------------------- ----------
Error: nvm command not found
---------------------------------- ----------
The problem is that nvm installs fine for user newusername, but its env variables (path to nvm in particular) are added to .bashrc but not loaded even though I already did source /home/$ {newuser}/.bashrc
Is it still possible to do this in one script?
best regards, Vadim

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vadim, 2019-11-19
@Viji

In short, I had to specifically pervert, but maybe it will be useful to someone (maybe someone will explain why it works at all))):
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Incorrect number of arguments"
exit 1
fi
if ; then
echo "This script must be run as root"
exit 1
fi
newuser=$1
useradd -m -s /bin/bash $newuser
usermod -aG sudo $newuser
read -r -d '' node_install << EOF
#!/bin/ bash
cd /home/${newuser}/code
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35... | bash
source /home/${newuser}/.profile
source /home/${newuser}/.bashrc
nvm install 10.3
source /home/${newuser}/.profile
source /home/${newuser}/.bashrc
exit
EOF
echo -e "Node Install Script: \n""${node_install}"
echo "switching to the new user =""$newuser"
sudo -H -u ${newuser} bash -c "echo \"${node_install}\" > /home/$newuser/install_node.sh"
chmod +x /home/$newuser/install_node .sh
chown "${newuser}":"${newuser}" /home/$newuser/install_node.sh
sudo -i -H -u "${newuser}" exec $SHELL -i "/home/"${ newuser}"/./install_node.sh"

V
Vitaly Karasik, 2019-11-19
@vitaly_il1

sudo -H -u ${newuser} bash -c "source /home/${newuser}/.bashrc"
sudo -H -u ${newuser} bash -c "nvm install 10.3"

should be
sudo -H -u ${newuser} bash -c "source /home/${newuser}/.bashrc ; nvm install 10.3"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question