Environment Variables or Exporting Variables Permanently

Environment Variables or Exporting Variables Permanently

To export variables permanently, you can add the export command in any of the following start-up files :
~/.profile
~/.bashrc
/etc/profile
.profile and .bashrc lives in the users home directory so they are accessible only for that user./etc/profile is for global access for all the variables.
For example

admin@DevOps:~$ cat .bashrc | grep export
#export
GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
export tesla='Alternate current'
export EDITOR=vim
export name=Cassini

Summary:

$1, $2, ...
The first, second, etc command line arguments to the script.
Quotes "
Double will do variable substitution, single will not.
variable=value
To set a value for a variable. Remember, no spaces on either side of =
variable=$(command )
Save the output of a command into a variable
export var1
Make the variable var1 available to child processes
User Input
Interactive Scripts
Ask the User for Input
Taking input from the user while executing the script, storing it into a variable and then using that variable in our script. We would be taking inputs from user like IP addresses, usernames, passwords or confirmation Y/N to do this we use command called read. This command takes the input and will save it into a variable.
read var1
Let's look at a simple example:
input.sh

1.#!/bin/bash
2. # Ask the username
3. echo “Please enter the username”
4. read varname
5. echo “Welcome back $varname”

admin@DevOps:.../bashscripts$ ./input.sh
Please enter the username
DevTestOps
Welcome back DevTestOps

Explanation:

Line 3 - Print a message asking the user for input.
Line 4 - Run the command read and save the users response into the variable varname
Line 5 – echo another message just to verify the read command worked. Note: I had to put a backslash ( \ ) in front of the ' so that it was escaped.
You are able to alter the behaviour of read with a variety of command line options. (See the man page for read to see all of them.) Two commonly used options however are -p which allows you to specify a prompt and -s which makes the input silent. This can make it easy to ask for a username and password combination like the example below:
Login.sh
#!/bin/bash
# Ask the user for login details
read -p 'Username: ' uservar
read -sp 'Password: ' passvar
echo
echo Thankyou $uservar we now have your login details

So far we have looked at a single word as input. We can do more than that however.

multiinput.sh

#!/bin/bash
# Demonstrate how read actually works
echo Enter your Name, Profession &Interests in same order seprated by a space?
read name profession interest
echo Your entered name is: $name
echo Your profession is: $profession
echo Your are interested in: $interest





admin@DevOps:.../bashscripts$ ./multiinput.sh
Enter your Name, Profession &Interests in same order seprated by a space?
Imran DevOps Hiking
Your entered name is: Imran
Your profession is: DevOps
Your are interested in: Hiking


If Statements
Scripts making decisions.
Basic If Statements
If you use bash for scripting you will undoubtedly have to use conditions a lot. Based on a condition you decide if you should execute some commands on the system or not. A basic if statement effectively says, if a particular test is true, then perform a given set of actions. If it is not true then don't perform those actions. If follows the format below:

if [ <some test>]
then
<commands>
fi

Anything between then and fi (if backwards) will be executed only if the test (between the square brackets) is true.
Let's look at a simple example:

if_example.sh 

#!/bin/bash
# Basic if statement
if [ $1 -gt 100 ]
then
echo Hey that’s a large number.
pwd
fi
date  

Explanation:

Line 3 - Let's see if the first command line argument is greater than 100 
Line 5 and 6 - Will only get run if the test on line 4 returns true. You can have as many commands here as you like. 
Line 7 - fi signals the end of the if statement. All commands after this will be run as normal. 
Line 8 - Because this command is outside the if statement it will be run regardless of the outcome of the if statement. 
admin@DevOps:.../bash$ ./if_example.sh 150
Hey that’s a large number.
/tmp/bash
Sun Oct 30 16:19:28 IST 2016  

Sample if/else condition script
If condition is true we execute a block of code but if its false we can execute some other block of code by using else command

#!/bin/bash
intA=20
intB=30
if [ intA==intB ];
then
echo "intA is equal to intB"
else
echo "Not Equal"
fi
if [ -f hosts ];
then
echo "File exists!"
else
echo "Does not exist"
fi  

Test
The square brackets ( [ ] ) in the if statement above are actually a reference to the command test. This means that all of the operators that test allows may be used here as well. Look up the man page for test to see all of the possible operators (there are quite a few) but some of the more common ones are listed below.

Operator Description    
! EXPRESSION The EXPRESSION is false.    
-n STRING The length of STRING is greater than zero.    
-z STRING The length of STRING is zero (ie it is empty).    
STRING1 = STRING2 STRING1 is equal to STRING2    
STRING1 != STRING2 STRING1 is not equal to STRING2    
INTEGER1 -eq INTEGER2 INTEGER1 is numerically equal to INTEGER2    
INTEGER1 -gt INTEGER2 INTEGER1 is numerically greater than INTEGER2    
INTEGER1 -lt INTEGER2 INTEGER1 is numerically less than INTEGER2    
-d FILE FILE exists and is a directory.    
-e FILE FILE exists.    
-r FILE FILE exists and the read permission is granted.    
s FILE FILE exists and it's size is greater than zero (ie. it is notempty).    
-w FILE FILE exists and the write permission is granted.    
-x FILE FILE exists and the execute permission is granted.  


A few points to note:
= is slightly different to -eq. [ 001 = 1 ] will return false as = does a string comparison (ie. character for character the same) whereas -eq does a numerical comparison meaning [ 001 -eq 1 ] will return true.li> 
When we refer to FILE above we are actually meaning a path. Remember that a path may be absolute or relative and may refer to a file or a directory. 
Because [ 5 is just a reference to the command test we may experiment and trouble shoot with test on the command line to make sure our understanding of its behaviour is correct. 

Comments