EC2 User data not formatting bash variables - bash

I have a line in my ec2 instance user data that is seen below
cat <<EOT >> $HOME/.bashrc
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export HADOOP_HOME=$HOME/hadoop
export HADOOP_CONF=$HADOOP_HOME/etc
export PATH=$PATH:$JAVA_HOME:$HADOOP_HOME/bin
EOT
It is supposed to append to the ~/.bashrc file while inferring variable itself, but what is appended to the ~/.bashrc file looks like
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export HADOOP_HOME=/home/ubuntu/hadoop
export HADOOP_CONF=/etc
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin::/bin
The HADOOP_HOME variable works fine but the HADOOP_CONF and PATH variable is not what it supposed to be. My guess is it wasn't able to infer $HADOOP_HOME in both cases.
What can I do about this?
I've tried separating it like this
cat <<EOT >> $HOME/.bashrc
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export HADOOP_HOME=$HOME/hadoop
EOT
cat <<EOT >> $HOME/.bashrc
export HADOOP_CONF=$HADOOP_HOME/etc
export PATH=$PATH:$JAVA_HOME:$HADOOP_HOME/bin
EOT
I've also tried to surround the $HADOOP_HOME in quotes too

Related

Exporting environment variables in ~/.bash_profile produces wrong values

On my MacBook with MacOS Catalina I try to export multiple environment variables by setting them in the ~/.bash_profile file. I proceed as follows:
touch ~/.bash_profile
open ~/.bash_profile
Then the file ~/.bash_profile opens in my text editor and I add multiple environment variables:
export VAR1="some_value"
export VAR2="some_value"
export VAR3="some_value"
export VAR4="some_value"
export VAR5="some_value"
Then I save the file and apply the changes as follows:
source ~/.bash_profile
Now I should be able to access the values of the newly defined variables. If I type echo ${VAR1} or echo ${VAR2} I get the correct value some_value, but if I type echo ${VAR3} I get export$lue, i.e. the first seven characters are replaced by export$.
Any advice?

Trying to install hadoop on a Linux system, getting error while editing .bashrc file

I am installing hadoop in my system while trying to make permanent .bashrc changes using --source ~/.bashrc getting the error below:
/home/tcs/hadoop>source ~/.bashrc
ksh: .[5]: .[35]: shopt: not found [No such file or directory]
ksh: .[5]: .[46]: shopt: not found [No such file or directory]
ksh: .[5]: .[65]: [: argument expected
the .bashrc file content is
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific aliases and functions
export JAVA_HOME=/usr/lib/jvm/jre-1.6.0-openjdk.x86_64
export HADOOP_INSTALL=/home/tcs/hadoop
export PATH=$PATH:$HADOOP_INSTALL/bin
export PATH=$PATH:$HADOOP_INSTALL/sbin
export HADOOP_MAPRED_HOME=$HADOOP_INSTALL
export HADOOP_COMMON_HOME=$HADOOP_INSTALL
export HADOOP_HDFS_HOME=$HADOOP_INSTALL
export YARN_HOME=$HADOOP_INSTALL
export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_INSTALL/lib/native
export HADOOP_OPTS="-Djava.library.path=$HADOOP_INSTALL/lib"
There should be a respective rc file for ksh that you need to edit instead.
And if you find it, note that bash and ksh have slightly different syntaxes, but that shouldn't be an issue if you are only exporting environment variables
Also, latest versions of Hadoop do not support Java 6, so you'll need to update the Java home variable
It seems you run ksh (Korn-shell) environment. According to the .kshrc:
The $HOME/.kshrc file is a shell script that customizes the Korn-shell
environment. This .kshrc script often contains a list of environment
variables, command aliases, and function definitions that customize
the Korn-shell environment.
You can add your EXPORT statements to the .kshrc file as they are. The issue you see is with the script located in the /etc/bashrc file.
Or as an alternative, use bash instead. Simply run /bin/bash for a one time use. Or set it up as a default shell via chsh -s /bin/bash. You must log out and log back in to see this change.

SH File not setting ENV variables (Mac OS X)

I have a file set_env.sh, that I run in the terminal using:
sh set_env.sh
The code runs, but the variables are not set. I am checking with
echo $EMAIL_SERVER
set_env.sh
#!/bin/sh
echo email Address
read y
echo email password?
read x
export EMAIL_SERVER='smtp.gmail.com'
export EMAIL_PORT=587
export EMAIL_DOMAIN='domain.com'
export EMAIL_AUTHENTICATION='plain'
export EMAIL_ENABLE_STARTTLS_AUTO=true
export EMAIL_USERNAME=$y
export EMAIL_PASSWORD=$x
Use source command instead:
source set_env.sh
The command executes the script in the current shell context as opposed to invocation via separate shell process: sh set_env.sh (the environment variables are set within the new sh process which is isolated from the current process).
By the way, you can use dot (.) instead of source, if you prefer.
You may modify your ~/.bash_profile file in your home dir with this
export EMAIL_SERVER='smtp.gmail.com'
export EMAIL_PORT=587
export EMAIL_DOMAIN='domain.com'
export EMAIL_AUTHENTICATION='plain'
export EMAIL_ENABLE_STARTTLS_AUTO=true
export EMAIL_USERNAME=$y
export EMAIL_PASSWORD=$x

Where is Shell Path in mac. .bash_profile Dont works correctly

When i use:
nano .bash_profile
the terminal show me:
export PATH=/usr/bin:/usr/sbin:/bin:/usr/local/bin:/sbin:/opt/x11/bin:$PATH
# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
export PATH
export PATH="/Applications/MAMP/Library/lib"
export PATH="/Applications/MAMP/Library/bin"
#XAMPP
#export PATH="/Applications/XAMPP/xamppfiles/bin:$PATH"
#export PATH="/Applications/XAMPP/xamppfiles/lib:$PATH"
But when i open a new terminal only shows. echo $PATH prints.
/Applications/MAMP/Library/bin
The problem is that when i execute a command the terminal returns :
command not found
I need to execute this command for the terminal to operate properly
export PATH=/usr/bin:/usr/sbin:/bin:/usr/local/bin:/sbin:/opt/x11/bin:$PATH
and echo $PATH prints.
/usr/bin:/usr/sbin:/bin:/usr/local/bin:/sbin:/opt/x11/bin:/Applications/MAMP/Library/bin
What can i do to open and edit the correct shell PATH ?
You are overiding your PATH variable every time you export. your .bash_profile should be
export PATH=/usr/bin:/usr/sbin:/bin:/usr/local/bin:/sbin:/opt/x11/bin
export PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
export PATH="/Applications/MAMP/Library/lib:$PATH"
export PATH="/Applications/MAMP/Library/bin:$PATH"
this way your current path is added to the end of the new path variable

How can I define my text editor in bashrc?

I'm trying to set a variable containing my editor in ~/.bashrc. Unfortunately it doesn't seem to be coming through
# ~/.bashrc
export EDITOR=sublime
in terminal:
source ~/.bashrc
echo $EDITOR
=> nothing
How can I set and persist this variable?
Edit
This is my current .bashrc file:
source ~/.profile # Get the paths
source ~/.bashrc # get aliases
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
### Added by the Heroku Toolbelt
export PATH="/usr/local/heroku/bin:$PATH"
export EDITOR=sublime
Depending on your OS, check your .bash_profile or /etc/bash.bashrc
If your .bashrc isn't sourced, your .bashrc isn't read. All you need to do is source your file or drop your export command in something that is.
The .bashrc is only read in when a new shell is created and not when you log in. If you want your .bashrc read in during logins, you have to add this line to your $HOME/.bash_profile or $HOME/.profile:
[ -x $HOME/.bashrc ] && . $HOME/.bashrc
Note that $HOME/.bashrc must be both readable and executable by the user. (i.e., the file permission must be 5.. or 7..) for it to work. Make sure that your file permissions are set correctly.

Resources