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

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

==> 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.

Related

how to echo $(command) into bash profile

I am trying to append eval "$(rbenv init -)" to my bash profile
(I am trying to follow this instruction)
# Load rbenv automatically by appending
# the following to ~/.bash_profile:
eval "$(rbenv init -)"
This is part of an automated process so I don't have access to a GUI or can't use an editor. but when I echo the command $(rbenv init -) gets executed and append bunch of stuff. How can I echo it as plain text?
this is what I have now
ec2-user#ip-172-31-46-129 ~ % cat /tmp/install-rbenv.sh
+-zsh:41> cat /tmp/install-rbenv.sh
#!/bin/bash
sudo -i -u buildkite-agent bash << EOF
echo "export RUBY_CONFIGURE_OPTS=\"--with-openssl-dir=$(brew --prefix openssl#1.1)\"" >> /Users/buildkite-agent/.bash_profile
echo 'eval "$(rbenv init -)"' >> /Users/buildkite-agent/.bash_profile
EOF
echo "Done"
running it
ec2-user#ip-172-31-46-129 ~ % . /tmp/install-rbenv.sh
+-zsh:42> . /tmp/install-rbenv.sh
+/tmp/install-rbenv.sh:6> brew --prefix openssl#1.1
+/tmp/install-rbenv.sh:6> rbenv init -
+/tmp/install-rbenv.sh:6> sudo -i -u buildkite-agent bash
+/tmp/install-rbenv.sh:10> echo Done
Done
checking the bash profile
ec2-user#ip-172-31-46-129 ~ % sudo su - buildkite-agent
+-zsh:43> sudo su - buildkite-agent
rbenv: no such command `sh-'
-bash: eval: line 31: syntax error near unexpected token `rehash'
-bash: eval: line 31: ` rehash|shell)'
The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
ip-172-31-46-129:~ buildkite-agent$ cat ~/.bash_profile
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=/usr/local/opt/openssl#1.1"
eval "export PATH="/Users/ec2-user/.rbenv/shims:${PATH}"
export RBENV_SHELL=zsh
source /usr/local/Cellar/rbenv/1.1.2/libexec/../completions/rbenv.zsh
command rbenv rehash 2>/dev/null
rbenv() {
local command
command="${1:-}"
if [ "$#" -gt 0 ]; then
shift
fi
case "$command" in
rehash|shell)
eval "$(rbenv "sh-$command" "$#")";;
*)
command rbenv "$command" "$#";;
esac
}"
Apparently you don't want to use an editor to edit your .bash_profile file or you are afraid you will got trapped inside vi :-D
Put the text you want to append to the file in apostrophes and it will go verbatim to the file. The shell does not do any expansion in the arguments that are wrapped in apostrophes.
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
That's all.
Use apostrophes around the first EOF, i.e. 'EOF' instead of EOF. bash distinguishes between the two and won't evaluate stuff if you use the apostrophe version.
> bash << EOF > test.sh
echo '$(date)'
EOF
> cat test.sh
Mon 12 Apr 2021 08:08:53 PM CEST
> bash << 'EOF' > test.sh
echo '$(date)'
EOF
> cat test.sh
$(date)

CMD does not run if used after ENTRYPOINT

I have the following docker file
FROM confluentinc/cp-kafka-connect:5.3.1
RUN apt-get update && apt-get -y install cron
ENV CONNECT_PLUGIN_PATH=/usr/share/java
# JDBC-MariaDB
RUN wget -nv -P /usr/share/java/kafka-connect-jdbc/ https://downloads.mariadb.com/Connectors/java/connector-java-2.4.4/mariadb-java-client-2.4.4.jar
# SNMP Source
RUN wget -nv -P /tmp/ https://github.com/name/kafka-connect-snmp/releases/download/0.0.1.11/kafka-connect-snmp-0.0.1.11.tar.gz
RUN mkdir /tmp/kafka-connect-snmp && tar -xf /tmp/kafka-connect-snmp-0.0.1.11.tar.gz -C /tmp/kafka-connect-snmp/
RUN mv /tmp/kafka-connect-snmp/usr/share/kafka-connect/kafka-connect-snmp /usr/share/java/
COPY plugins-config.sh /usr/share/kafka-connect-script/plugins-config.sh
RUN chmod +x /usr/share/kafka-connect-script/plugins-config.sh
ENTRYPOINT [ "./etc/confluent/docker/run" ]
CMD ["/usr/share/kafka-connect-script/plugins-config.sh"]
And the bash file as this
#!/bin/bash
#script to configure kafka connect with plugins
# export CONNECT_REST_ADVERTISED_HOST_NAME=localhost
# export CONNECT_REST_PORT=8083
url=http://$CONNECT_REST_ADVERTISED_HOST_NAME:$CONNECT_REST_PORT/connectors
curl_command="curl -s -o /dev/null -w %{http_code} $url"
sleep_second=5
sleep_second_counter=0
max_seconds_to_wait=60
echo "Waiting for Kafka Connect to start listening on localhost" >> log.log
echo "HOST: $CONNECT_REST_ADVERTISED_HOST_NAME , PORT: $CONNECT_REST_PORT" >> log.log
while [[ $(eval $curl_command) -eq 000 && $sleep_second_counter -lt $max_seconds_to_wait ]]
do
echo "In" >> log.log
echo -e $date " Kafka Connect listener HTTP state: " $(eval $curl_command) " (waiting for 200) $sleep_second_counter" >> log.log
echo "Going to sleep for $sleep_second seconds" >> log.log
sleep $sleep_second
echo "Finished sleeping" >> log.log
((sleep_second_counter+=$sleep_second))
echo "Finished counter" >> log.log
done
echo "Out" >> log.log
nc -vz $CONNECT_REST_ADVERTISED_HOST_NAME $CONNECT_REST_PORT
/bin/bash
Entry point gets called correctly but CMD does not get invoked.
I also try to understand the solution given here CMD doesn't run after ENTRYPOINT in Dockerfile
but I did not understand the solution.
If someone could explain a bit more what is wrong here.
What I am trying to accomplish
I am trying to have a single docker container image which will start the kafka-connect server (ENTRYPOINT) and then via bash file (CMD) I will configure the plugins. Requirement is that the same sequence of steps gets executed everytime the containers restarts.
CMD is run after ENTRYPOINT, like parameters after a function invokation, in the same command line.
In your case you want two different commands running sequentially. Then, you may add them to a startup_script.sh whose content is:
#!/bin/bash
./etc/confluent/docker/run & # run in background not to get stuck in here
/usr/share/kafka-connect-script/plugins-config.sh # apply configuration
sleep 100000000 # to avoid the startup script to exit since that would kill the container

Making bash script continue after exec $SHELL

I'm making a bash script that would install rbenv and ruby.
cd
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL
rbenv install $rubyVersion
rbenv global $rubyVersion
But when the exec $SHELL is called the bash process is replaced by new bash process and the script stops (of course).
How can I make the script to continue?
It appears that you're trying to achieve multiple objectives by modifying the .bashrc file then calling exec $SHELL. Neither of those actions will modify the shell-in-which-this-script-is-running. To modify the current shell, you want to "source" the .bashrc file. Use the "dot notation" instead of calling exec $SHELL:
. ~/.bashrc
Good luck with this one!
replace exec $SHELL lines with "$SHELL" lines or completely remove those lines

How to write in /etc/profile using bash | Permission Denied

I'm creating a bash script to set up an Ubuntu 16.04 lts OS to download, install and other stuff without introduce each command separately and I have to write in the /etc/profile file to add a PATH environment variable. When my code get into that line it appears the Permission Denied message, this is what I have:
sudo echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile
bash: /etc/profile: Permission denied
Do you know how could I solve this?
Shell i/o redirection happens before the shell executes your command...in other words, when you write:
sudo somecommand >> /etc/profile
The >> /etc/profile part is performed as the current user, not as root. That's why you're getting the "permission denied" message. There are various ways of solving this. You can run:
sudo sh -c "echo export PATH=$PATH:/usr/local/go/bin >> /etc/profile"
Or you can take advantage of the append (-a) flag to the tee command:
echo "export PATH=$PATH:/usr/local/go/bin" | sudo tee -a /etc/profile
sudo sh -c "echo MY_GLOBAL_ENV_TO_MY_CURRENT_DIR=$(pwd)" >> /etc/environment"

not found error when insert #!/bin/sh

I started learning Linux shell scripting, when I was writing this script I got an error,
./my_script: 4: read: Illegal option -n
./my_script: 5: ./my_script: [[: not found
I found out its because #!/bin/sh line, i can still run the script without that line but it won't execute codes such as /n
#!/bin/sh
# Shell installer for gurb customizer. by Naveen Gamage.
OS=$(lsb_release -si)
ARCH=$(uname -m | sed 's/x86_//;s/i[3-6]86/32/')
VER=$(lsb_release -sr)
grabninstall() {
sudo add-apt-repository ppa:danielrichter2007/grub-customizer
sudo apt-get update
sudo apt-get install grub-customizer
}
echo "Installer for GRUB CUSTOMIZER\n"
echo "GURB CUSTOMIZER"
echo "A tool for editing and configuring boot menu (GRUB2/BURG).\n"
read -p "Do you want to install Grub Customizer for $OS ${VER} [$ARCH] ? (Y/n) " -n 1
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "The installer is downloading and installing GRUB Customizer!";
echo "This action may require your password.\n";
grabninstall
else
echo "user quit"
echo "Installation was unsuccessful."
fi
I'm doing this on Ubuntu 12.10.
and which sh gives this output
/bin/sh
any idea where i did wrong?
The problem is that you are using /bin/sh to run the script and on your system /bin/sh -> dash. This means that dash is executing your script. The dash shell does not support [[, but bash does. So you should change the first line in your script (called the Shebang) from #!/bin/sh to #!/bin/bash.
Alternatively, don't use [[ in your script. Only use features support by dash.
Also see this Ubuntu page on what constructs are not supported in dash.

Resources