Bash shell script: how do I exit and restart the script?

qinking126

I use a shell script to provision my server. After I modify the .bashrc file, i need to exit then log back in to restart the shell.

su vagrant <<'EOF'
echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
echo "export PROJECT_HOME=/var/www" >> ~/.bashrc
echo "alias mkvirtualenv='mkvirtualenv --no-site-packages'" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc

source ~/.bashrc

// this is where I need help, i need to exit the shell and relogin. then run mkvirutalenv command.
mkvirtualenv test1

EOF

Update:

this is the shell script file vagrant will run.

#!/usr/bin/env bash

if [ -f "/var/vagrant_provision" ]; then
    exit 0
fi

echo "Installing Flask environment and setting it up.."
echo "------------------------------------------------"
apt-get update >/dev/null 2>&1
echo "1. update is done"

#apt-get upgrade -y >/dev/null 2>&1
echo "2. upgrade is done -- skipped for dev"

rm -rf /var/www
ln -fs /vagrant /var/www
echo "3. Symbolic link is created"

apt-get install -y build-essential python-dev >/dev/null 2>&1
apt-get install -y curl >/dev/null 2>&1
echo "4. curl is installed"

apt-get install -y python-pip >/dev/null 2>&1
echo "5. pip is installed"

pip install virtualenv virtualenvwrapper >/dev/null 2>&1
echo "6. virtualenv and virtualenvwrapper are installed"

su vagrant <<'EOF'
echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
echo "export PROJECT_HOME=/var/www" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
echo "alias mkv='mkvirtualenv --no-site-packages'" >> ~/.bashrc
echo "alias mycmd='ls'" >> ~/.bashrc
source ~/.bashrc

mycmd
mkv test1
EOF

echo "7. add environment variables to .bashrc"
echo "8. source .bashrc"
echo "9. test1 environment is created"

touch /var/vagrant_provision
echo "------------------------------------------------"
echo "Installation is done"

this is the output I got. still getting command not found.

Installing Flask environment and setting it up..
------------------------------------------------
1. update is done
2. upgrade is done -- skipped for dev
3. Symbolic link is created
4. curl is installed
5. pip is installed
6. virtualenv and virtualenvwrapper are installed
bash: line 8: mycmd: command not found
bash: line 9: mkv: command not found
7. add environment variables to .bashrc
8. source .bashrc
9. test1 environment is created
------------------------------------------------
Installation is done
Gaurav Pant

==> After I modify the .bashrc file, i need to exit then log back in to restart the shell.

No need to restart the shell. If you want changes to get reflected in current session immediately then you can do this by using below commands.

source ~/.bashrc

or

. ~/.bashrc

By doing this you will load current new settings into your session. So you need not to re-login.

Please find one sample code which work properly.

#!/usr/bin/sh
echo "alias mycmd='ls'" >> ~/.bashrc
source ~/.bashrc
mycmd

To fix your problem -->

Please create passwordless ssh for user 'vagrant'. Please check the documantation to create passwordless ssh here.

Then put your run document commands like below.

ssh vagrant@localhost "alias mycmd='echo $HOME';/mycmd"

here using '/' before mycmd is mandatory otherwise 'mycmd' will be executed by current shell only and you will get command not found error.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

In bash how do I exit a script from a function that is piped by tee?

From Dev

How do I write shell script for Redhat Linux bash shell

From Dev

How do I search a word in Shell Script (Bash)?

From Dev

How do I exit a script in a conditional statement?

From Dev

restart bash script on exit(1) from php script?

From Dev

How do i run a shell script, in a shell script with the menu dialog?

From Dev

Why do I have to type enter to exit my bash script?

From Dev

How do you set the exit status of a shell script?

From Java

Automatic exit from bash shell script on error

From Dev

How can I exit from vi using a shell script?

From Dev

How do I script a filecount in android shell?

From Dev

How do I use a regex in a shell script?

From Dev

How do I close stdin in a shell script?

From Dev

How do I script a filecount in android shell?

From Dev

How do I wait for a file in the shell script?

From Dev

How do I handle switches in a shell script?

From Dev

How do I add a line to the shell script?

From Dev

How can a bash script restart a process on non-0 exit while sending signals to child

From Dev

How do I restart a python script on a remote server if it stops?

From Dev

How do I run a Python script in the background and restart it after a crash?

From Dev

How to restart gnome shell but daemonise it from a script?

From Dev

How to restart a docker service using Shell Script

From Dev

How do I stop execution of bash script?

From Dev

How do I iterate the arguments in a bash script?

From Dev

How do I maintain sudo in a bash script?

From Dev

How do I run this bash script in ubuntu?

From Dev

How do I concatenate strings in a bash script?

From Dev

How do I iterate the arguments in a bash script?

From Java

How do I know the script file name in a Bash script?

Related Related

  1. 1

    In bash how do I exit a script from a function that is piped by tee?

  2. 2

    How do I write shell script for Redhat Linux bash shell

  3. 3

    How do I search a word in Shell Script (Bash)?

  4. 4

    How do I exit a script in a conditional statement?

  5. 5

    restart bash script on exit(1) from php script?

  6. 6

    How do i run a shell script, in a shell script with the menu dialog?

  7. 7

    Why do I have to type enter to exit my bash script?

  8. 8

    How do you set the exit status of a shell script?

  9. 9

    Automatic exit from bash shell script on error

  10. 10

    How can I exit from vi using a shell script?

  11. 11

    How do I script a filecount in android shell?

  12. 12

    How do I use a regex in a shell script?

  13. 13

    How do I close stdin in a shell script?

  14. 14

    How do I script a filecount in android shell?

  15. 15

    How do I wait for a file in the shell script?

  16. 16

    How do I handle switches in a shell script?

  17. 17

    How do I add a line to the shell script?

  18. 18

    How can a bash script restart a process on non-0 exit while sending signals to child

  19. 19

    How do I restart a python script on a remote server if it stops?

  20. 20

    How do I run a Python script in the background and restart it after a crash?

  21. 21

    How to restart gnome shell but daemonise it from a script?

  22. 22

    How to restart a docker service using Shell Script

  23. 23

    How do I stop execution of bash script?

  24. 24

    How do I iterate the arguments in a bash script?

  25. 25

    How do I maintain sudo in a bash script?

  26. 26

    How do I run this bash script in ubuntu?

  27. 27

    How do I concatenate strings in a bash script?

  28. 28

    How do I iterate the arguments in a bash script?

  29. 29

    How do I know the script file name in a Bash script?

HotTag

Archive