Everything Shell Script

  • Home
  • Home

Sunday, 6 November 2016

Powerline – Adds Powerful Statuslines and Prompts to Vim Editor and Bash Terminal

 Niraj Bhagchandani     23:15:00     tecmint     No comments   

Powerline is a great statusline plugin for Vim editor, which is developed in Python and provides statuslines and prompts for many other applications such as bash, zsh, tmux and many more.


Features

  1. It is written in Python, which makes it extensible and feature rich.
  2. Stable and testable code base, which works well with Python 2.6+ and Python 3.
  3. It also supports prompts and statuslines in several Linux utilities and tools.
  4. It has configurations and decorator colors developed using JSON.
  5. Fast and lightweight, with daemon support, which provides even more better performance.

Powerline Screenshots

 

In this article, I will show you how to install Powerline and Powerline fonts and how to use with Bash and Vim under RedHat and Debian based systems.

Step 1: Installing Generic Requirements for Powerline

Due to a naming conflict with some other unrelated projects, powerline program is available on PyPI (Python Package Index) under the package name as powerline-status.
To install packages from PyPI, we need a ‘pip‘ (package management tool for installing Python packages). So, let’s first install pip tool under our Linux systems.

Install Pip on Debian, Ubuntu and Linux Mint

# apt-get install python-pip
 
Sample Output 

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Recommended packages:
python-dev-all python-wheel
The following NEW packages will be installed:
python-pip
0 upgraded, 1 newly installed, 0 to remove and 533 not upgraded.
Need to get 97.2 kB of archives.
After this operation, 477 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ trusty-updates/universe python-pip all 1.5.4-1ubuntu3 [97.2 kB]
Fetched 97.2 kB in 1s (73.0 kB/s)     
Selecting previously unselected package python-pip.
(Reading database ... 216258 files and directories currently installed.)
Preparing to unpack .../python-pip_1.5.4-1ubuntu3_all.deb ...
Unpacking python-pip (1.5.4-1ubuntu3) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Setting up python-pip (1.5.4-1ubuntu3) ...

Install Pip on CentOS, RHEL and Fedora

Under Fedora-based systems, you need to first enable epel-repository and then install pip package as shown.

# yum install python-pip          
# dnf install python-pip                     [On Fedora 22+ versions] 
 
Sample Output 

Installing:
python-pip          noarch          7.1.0-1.el7             epel          1.5 M
Transaction Summary
=================================================================================
Install  1 Package
Total download size: 1.5 M
Installed size: 6.6 M
Is this ok [y/d/N]: y
Downloading packages:
python-pip-7.1.0-1.el7.noarch.rpm                         | 1.5 MB  00:00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : python-pip-7.1.0-1.el7.noarch                                 1/1 
Verifying  : python-pip-7.1.0-1.el7.noarch                                 1/1 
Installed:
python-pip.noarch 0:7.1.0-1.el7                                                
Complete!

Step 2: Installing Powerline Tool in Linux

Now it’s’ time to install Powerline latest development version from the Git repository. For this, your system must have git package installed in order to fetch the packages from Git.
# apt-get install git
# yum install git
# dnf install git
Next you can install Powerline with the help of pip command as shown.
# pip install git+git://github.com/Lokaltog/powerline
 
Sample Output 

Cloning git://github.com/Lokaltog/powerline to /tmp/pip-WAlznH-build
Running setup.py (path:/tmp/pip-WAlznH-build/setup.py) egg_info for package from git+git://github.com/Lokaltog/powerline
warning: no previously-included files matching '*.pyc' found under directory 'powerline/bindings'
warning: no previously-included files matching '*.pyo' found under directory 'powerline/bindings'
Installing collected packages: powerline-status
Found existing installation: powerline-status 2.2
Uninstalling powerline-status:
Successfully uninstalled powerline-status
Running setup.py install for powerline-status
warning: no previously-included files matching '*.pyc' found under directory 'powerline/bindings'
warning: no previously-included files matching '*.pyo' found under directory 'powerline/bindings'
changing mode of build/scripts-2.7/powerline-lint from 644 to 755
changing mode of build/scripts-2.7/powerline-daemon from 644 to 755
changing mode of build/scripts-2.7/powerline-render from 644 to 755
changing mode of build/scripts-2.7/powerline-config from 644 to 755
changing mode of /usr/local/bin/powerline-config to 755
changing mode of /usr/local/bin/powerline-lint to 755
changing mode of /usr/local/bin/powerline-render to 755
changing mode of /usr/local/bin/powerline-daemon to 755
Successfully installed powerline-status
Cleaning up...

Step 3: Installing Powerline Fonts in Linux

Powerline uses special glyphs to show special arrow effect and symbols for developers. For this, you must have a symbol font or a patched font installed on your systems.

Download the most recent version of the symbol font and fontconfig configuration file using following wget command.

# wget https://github.com/powerline/powerline/raw/develop/font/PowerlineSymbols.otf
# wget https://github.com/powerline/powerline/raw/develop/font/10-powerline-symbols.conf
 
Then you need to move the font to your fonts directory, /usr/share/fonts/ or /usr/local/share/fonts as follows or you can get the valid font paths by using command xset q.

# mv PowerlineSymbols.otf /usr/share/fonts/ 
 
Next, you need to update your system’s font cache as follows.

# fc-cache -vf /usr/share/fonts/ 
 
Now install the fontconfig file.

# mv 10-powerline-symbols.conf /etc/fonts/conf.d/ 
 
Note: If custom symbols doesn’t appear, then try to close all terminal sessions and restart X window for the changes to take effect.

Step 4: Setting Powerline for Bash Shell and Vim Statuslines

In this section we shall look at configuring Powerline for bash shell and vim editor. First make your terminal to support 256color by adding the following line to ~/.bashrc file as follows.

export TERM=”screen-256color” 

Enable Powerline on Bash Shell

To enable Powerline in bash shell by default, you need to add the following snippet to your ~/.bashrc file.
First get the location of installed powerline using following command.

# pip show powerline-status 
Name: powerline-status
Version: 2.2.dev9999-git.aa33599e3fb363ab7f2744ce95b7c6465eef7f08
Location: /usr/local/lib/python2.7/dist-packages
Requires:  
 
Once you know the actual location of powerline, make sure to replace the location in the below line as per your system suggested.

powerline-daemon -q
POWERLINE_BASH_CONTINUATION=1
POWERLINE_BASH_SELECT=1
. /usr/local/lib/python2.7/dist-packages/powerline/bindings/bash/powerline.sh
 
Now try to logout and login back again, you will see powerline statuesline as shown below.

 

Try changing or switching to different directories and keep a eye on “breadcrumb” prompt changes to show your current location.
You will also be able to watch pending background jobs and if powerline is installed on a remote Linux machine, you can notice that the prompt adds the hostname when you connect via SSH.

Enable Powerline for Vim

If vim is your favorite editor, luckily there is a powerful plugin for vim, too. To enable this plugin, add these lines to ~/.vimrc file.

set  rtp+=/usr/local/lib/python2.7/dist-packages/powerline/bindings/vim/
set laststatus=2
set t_Co=256
 
Now you can launch vim and see a spiffy new status line:



Summary

Powerline helps to set colorful and beautiful statuslines and prompts in several applications, good for coding environments. I hope you find this guide helpful and remember to post a comment if you need any help or have additional ideas.

Source: http://www.tecmint.com/powerline-adds-powerful-statuslines-and-prompts-to-vim-and-bash/

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Wednesday, 26 October 2016

Until Loop in Shell Script.

 Niraj Bhagchandani     10:43:00     No comments   

To run these programs online I request you to go to following link: https://goo.gl/xKheU3
  1. The until loop is similar to while loop in shell scripting. But the major difference in two of them is
  2. Until loop executes the statement executes its code-block while its conditional expression is false,
  3. While loop statement executes its code block while its conditional expression is true.
The syntax for until expression is:

syntax
until expression
do
    command1
    command2
    command3
    .
    .
    .
    commandN
done


The above syntax indicates
until do and done are keywords
expression is any conditional expression (termed here to be false)

filename: main.sh
Let us review one example for until loop.

until [[ $var -le 6 ]]
do
    echo "Variable: $var"
    (( var++ ))
done


Thank you for watching this article.

Regards
Niraj Bhagchandani.
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

While Loop in Shell Script

 Niraj Bhagchandani     02:23:00     No comments   

To run the given example live online please visit the following link: https://goo.gl/mD69Gj

There are basic loops in shell script.
  1. For Loop
  2. While Loop
  3. Until Loop
But, In this tutorial we are going to cover While loop.

BASH WHILE LOOP

The Syntax for while loop is as follows:

Syntax
while expression
do
           command
done


In the above syntax I am trying to explain three major components of while loop

  1. keywords - while, do and done
  2. Expression is any expression which returns scalar values
  3. The statement will execute all the commands between do and done until the condition or expression is true.
filename: while1.sh

#!/bin/sh
a=0
while [ $a -lt 10 ]              #Spaces are very important here so keep that in mind.
do
         echo $a
         a=`expr $a + 1`
done


The another syntax for while loop can be termed in this following sequence.

while IFS= read -r line
do
     command 1;
     command 2;
     .
     .
     .
     command n;
done  < "path/to/dir/filename with or without space"


You may also write the following syntax.

while IFS= read -r field1 field2 field3 .... fieldN
do
         command1 on any one of the field
         command2 on any of the other fields.
         .
         .
         command3 on any of the fields
done < "path/to/dir/filename with or without space"


Here, IFS is used to set field separtor (by default the field seperator is white space). The -r option is to read command disables backslashes escaping such as \n,\t etc. This is failsafe while read loop for reading text files.

Here, IFS means Internal Field Separator. If time permits I will write a post for IFS too in future.

You may also use expression format in while Loop as.

filename: while2.sh
#!/bin/sh
a=0
while (( $a -lt 10 ))              #Spaces are very important here so keep that in mind.
do
         echo $a
         a=`expr $a + 1`
done


So, as we now know about the above loops let us take care of one more syntax which I have described above.
For that we need a file to read. Just look at the simplicity on how we can read the file in most efficient manner in Linux compared to C LANGUAGE.

Reading a text file using "space" as default field separator.

filename: while3.sh

#!/bin/sh
file="namefile"
while read -r f1 f2
do
      echo "Field 1 is: $f1 and Field 2 is: $f2"
done< "$file"



Reading a text file using separator field.

filename: while4.sh

#!/bin/sh
file="commafile"
while IFS="," read -r line
do
      echo $line
done < "$file"


Here, Internal Field Separator is set as "," with every comma it finds it will treat as a line and print its value.

Even though we have "," as a separator, the entire lines are printed because everytime it takes the entire line from the file and stores in in "line" variable.

Let us view one more example for IFS using ":" separator file. As we all know the information of all the users are stored in /etc/passwd file in linux.
So, Let us view that file using while loop

filename: while5.sh

#/bin/sh
file="/etc/passwd"
while IFS=: read -r user enpass uid gid desc home shell
do
    echo "User $user ($uid) it's home directory is $home with shell as $shell";
done < "$file"


With all lot of experience shared above, I can also term that different shell in linux have different while loop syntax.
Such as.

While Loop Syntax in ksh

while [[ condition ]]; do
    command-1
    command-2
    .
    .
    .
    command-n
done


While loop syntax in csh is as:

while ( condition )
    command
end;


Now, let us view the simple examples of the above syntax one by one.

KSH While Loop Example
filename: while6.sh 

#!/bin/ksh
index=1
while [[ $index -le 5 ]]; do
    echo "Welcome to e-shellscript: $index times";
    ((index++ ))
done


CSH While Loop Example

filename: while7.sh

#!/bin/csh
index=1
while ( $index <=5 )
    echo "Welcome to e-shellscript $index times";
    @ c = $c + 1
end


Let us view another example for c-shell

filename: while8.sh


#!/bin/csh
set yourname="eshellscript"
while ( $yourname != "" )
    echo -n "Enter your cute name: "
    set yourname = $<
    if ($yname != "" ) then
        echo "Hello , $yourname"
    endif
end
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Sunday, 9 October 2016

12 differnet examples of For Loop in Shell Script

 Niraj Bhagchandani     08:46:00     No comments   

The Link to programs given in this tutorial is:  https://goo.gl/akbcTw


Well, to begin with FOR LOOP, I would tell that there are 2 different types of For loop in shell script
  • First is using "in" keyword with list of values
  • C like syntax of FOR LOOP
 I would recommend you to bookmark this article for future use as, we are going to explore more about looping structure.

Procedure 1: For loop using "in" keyword and list of values.
The syntax for for loop using "in" keyword is:

for var in list
do
      command 1;
      command 2;
      command 3;
done

in the above example
  • for, in, do and done are the keywords
  • list is the collection set of values which can be used for looping. This collection of the loop is separated by space.
  • variable is any variable which will contain a single value from the list at a given point of time.
The above syntax tells that list will be stored in variable during each cycle one by one and then executed in the body of the loop between do-done clause. Say for example there are 10 items in a loop then the cycle will execute the loop 10 times and every time one item is picked and stored in var variable. 

Procedure 2: For loop with C-like Syntax.

This is the second type of the FOR LOOP, this contain three expression just like in c i.e. initialization, condition and increment.

for (( expr1; exp2; expr3 ))
do
      command 1;
      command 2;
      command 3;
done

Note here, the spaces between (( and )) because they have some significance over there.
  • Well, for starters, the initialization takes place during expr1 which will be evaluated at the beginning of the loop
  •  The second i.e. expr2 will deal with the condition checking and the loop will execute until the condition does not set itself to false.
  • The third i.e. expr3 will deal with the increment of the variable and thus fulfill the requirement of the looping structure.
Now Let us see the examples one by one.

Example 1: List of static values after "in" keyword

The code is feed with the list of items such as { Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto} (Space separated) and thus printed using for loop

Filename: for1.sh 

for planets in Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto
do
    echo $planets
done

The above example would print the name of the planets one by one.

Example 2: Listing of the values using the variable after "in" keyword:
Now let us use the values stored in a variable and then execute the for loop

Filename: for2.sh

index=1;
planets="Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto"

for planet in $planets
do
     echo "Planet $((index++)): $planet"
done

Example 3: Pass the values in the command line argument format during execution

This code is well defined by command line arguments that are to be passed during execution of the shell program and thus, make it easier.

Filename: for3.sh

index=1;
for planet
do
     echo "Planet $((index++)): $planet"
done

During Execution type:

sh-4.2 $ sh for3.sh Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto

Example 4: Use list in the form of a command as input to for loop

Here, the input for FOR LOOP  is ls command which is defined in back-tick and thus, the list of all the files that are present in current directory will be displayed one by one.

Filename: for4.sh

index=1;
for list in `ls`
do
     echo "The file/s $((index+++)) : $list"
done

Example 5: Using of wildcard as the list for getting filenames and directories.

In this example we will look after the wild character as the input to for loop in a list for that we are going to use astrick (*) sign.

Filename: for5.sh

cd ~
index=1;
for file in *
do
     echo "$((index++)) : $file"
done

Example 6: Break statement in the loop

We will have a detail discussion on if condition too but as of now please understand the code for FOR LOOP.


Filename: for6.sh

index=1;
planets="Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto"

for planet in $planets
do
     if [ $index -eq 4 ]
     then
         break;
     fi
     echo "Planet $((index++)): $planet"
done

Example 7: Continue Statement in the for loop 

We will have a detail discussion on if condition too but as of now please understand the code for FOR LOOP.

Filename: for7.sh

index=1;
planets="Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto"

for planet in $planets
do
     if [ $index -eq 4 ]
     then
         index=`expr $index + 1`;
         continue;
     fi
     echo "Planet $((index++)): $planet"
done

Example 8.: C Like Syntax for FOR LOOP

This is from Procedure 2 syntax, thus let us print the numbers from 1 to 10 in FOR LOOP.

Filename: for8.sh

for (( index=1; index <10;  index++ ))
do
     echo "The number is: $index";
done

Example 9: Infinite Loop using FOR

Here, we are going to print the infinite series of number. But to stop printingg kindly press control + c so as to come out of  the infinite loop.

Filename: for9.sh

index=1
for (( ; ; ))
do
     echo "Number: $((index++))"
done  

Please press Control+C to come out of the loop

Example 10: More than 3 expression in BASH Script

Let us review the complexity of the FOR LOOP just like in C-Language.

Filename: for10.sh
 
for (( i=100, j=0; j<=50; i--,j+=5 ))
do
     echo "The value of i: $i and The value of j: $j";
done

Example 11: Give the range in FOR LOOP.

We, can also provide the range to for loop in a short and sweet manner using "in" keyword. This will make our work a lot easier and quite impressive.

Filename: for11.sh

for number in {1..15}    # Do not keep space inside {} brackets
do
     echo "The number: " $number
done


Example 12: Give the range in FOR LOOP With some increment after "in" keyword
Alongside the range, what if we want to increment the number by more than 1, then, here is the loop for increment too.

Filename: for12.sh

for number in {1..99..3}
do
     echo "The number: " $number
done

Also, do visit my blog to capture all shorts of shell programming during your study purpose.

Regards,
Niraj Bhagchandani.
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Saturday, 8 October 2016

Escape character in Shell Script

 Niraj Bhagchandani     20:28:00     No comments   

Okay!! uptil now we have seen a lot of stuffs regarding Shell Script. But how about escape characters

Example: What if you want to display: Hi "Niraj" ?

So, if you type

sh-4.2 $ echo "Hi "Niraj"" 

The first and last " characters wrap the whole lot into one parameter passed to echo so that the spacing between the two word is kept as is.

But the above code would interpret as
  • "Hello "
  • World
  • ""
Thus, the output would be

Hello World

Please note here that we loose the double quotes entirely as they are not printed during this process.

Thus, if you want to display the quotes you are in great need of escape characters.

sh-4.2 $ echo "Hi \"Niraj\" "

Here the first Argument treated individual is
  •  " -> start of the string
  • Hi
  • \" is treated as "
  • Niraj
  • \" is again treated as "
  • " -> end of the string
 Now, Let us look for one more wildcards and understand.(Astrick *)

sh-4.2 $ echo *
first.sh  second.sh etc.... will list files in the current directory.
sh-4.2 $ echo *txt 
Will list all txt files in current directory
sh-4.2 $ echo "*" 
Will print * as a literal / string
 sh-4.2 $ echo "*txt"
*txt 

However, " i.e. double quotes, $ i.e. dollar, ` backtick and \ backslash are still interpreted by the shell, even when they are in double quotes.

Thus for displaying the above characters we need a backslash (\) as a special character. Thus, they wont be interpreted by the shell, but passed as a string and not as a command or wild character.

Let us see if we want an output as below:

A sentence with ", backslash \, backtick ` and few spaces are      alongwith dollar sign $

we would have to write a echo command as:

echo "
A sentence with \", backslash \\, backtick \` and few spaces are      alongwith dollar sign \$"

Thus, backslash itself must be escaped to show that it is to be taken literally.

Regards,
Niraj Bhagchandani
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Wildcards

 Niraj Bhagchandani     08:53:00     No comments   



 A wildcard is a character that can be used as a substitute for any of a class of characters in a search, thereby greatly increasing the flexibility and efficiency of searches.

Wildcards are commonly used in shell commands in Linux and other Unix-like operating systems. A shell is a program that provides a text-only user interface and whose main function is to execute commands typed in by users and display their results.

Wildcards are also used in regular expressions and programming languages. Regular expressions are a pattern matching system that uses strings (i.e., sequences of characters) constructed according to pre-defined syntax rules to find desired strings in text.

The term wildcard or wild card was originally used in card games to describe a card that can be assigned any value that its holder desires. However, its usage has spread so that it is now used to describe an unknown or unpredictable factor in a variety of fields.

Star Wildcard
 
Three types of wildcards are used with Linux commands. The most frequently employed and usually the most useful is the star wildcard, which is the same as an asterisk (*). The star wildcard has the broadest meaning of any of the wildcards, as it can represent zero characters, all single characters or any string.

As an example, the file command provides information about any filesystem object (i.e., file, directory or link) that is provided to it as an argument (i.e., input). Because the star wildcard represents every string, it can be used as the argument for file to return information about every object in the specified directory. Thus, the following would display information about every object in the current directory (i.e., the directory in which the user is currently working):
file *
If there are no matches, an error message is returned, such as *: can't stat `*' (No such file or directory).. In the case of this example, the only way that there would be no matches is if the directory were empty.
Wildcards can be combined with other characters to represent parts of strings. For example, to represent any filesystem object that has a .jpg filename extension, *.jpg would be used. Likewise, a* would represent all objects that begin with a lower case (i.e., small) letter a.
As another example, the following would tell the ls command (which is used to list files) to provide the names of all files in the current directory that have an .html or a .txt extension:
ls *.html *.txt
Likewise, the following would tell the rm command (which is used to remove files and directories) to delete all files in the current directory that have the string xxx in their name:
rm *xxx*
Question Mark Wildcard
 
The question mark (?) is used as a wildcard character in shell commands to represent exactly one character, which can be any single character. Thus, two question marks in succession would represent any two characters in succession, and three question marks in succession would represent any string consisting of three characters.
Thus, for example, the following would return data on all objects in the current directory whose names, inclusive of any extensions, are exactly three characters in length:
file ???
And the following would provide data on all objects whose names are one, two or three characters in length:
file ? ?? ???
As is the case with the star wildcard, the question mark wildcard can be used in combination with other characters. For example, the following would provide information about all objects in the current directory that begin with the letter a and are five characters in length:
file a????
The question mark wildcard can also be used in combination with other wildcards when separated by some other character. For example, the following would return a list of all files in the current directory that have a three-character filename extension:
ls *.???
Square Brackets Wildcard
 
The third type of wildcard in shell commands is a pair of square brackets, which can represent any of the characters enclosed in the brackets. Thus, for example, the following would provide information about all objects in the current directory that have an x, y and/or z in them:
file *[xyz]*
And the following would list all files that had an extension that begins with x, y or z:
ls *.[xyz]*
The same results can be achieved by merely using the star and question mark wildcards. However, it is clearly more efficient to use the bracket wildcard.
When a hyphen is used between two characters in the square brackets wildcard, it indicates a range inclusive of those two characters. For example, the following would provide information about all of the objects in the current directory that begin with any letter from a through f:
file [a-f]*
And the following would provide information about every object in the current directory whose name includes at least one numeral:
file *[0-9]*
The use of the square brackets to indicate a range can be combined with its use to indicate a list. Thus, for example, the following would provide information about all filesystem objects whose names begin with any letter from a through c or begin with s or t:
file [a-cst]*
Likewise, multiple sets of ranges can be specified. Thus, for instance, the following would return information about all objects whose names begin with the first three or the final three lower case letters of the alphabet:
file [a-cx-z]*
Sometimes it can be useful to have a succession of square bracket wildcards. For example, the following would display all filenames in the current directory that consist of jones followed by a three-digit number:
ls jones[0-9][0-9][0-9]
Now Let us view something more interesting:

sh-4.2 $ cp /tmp/a/* /tmp/b
sh-4.2 $ cp /tmp/a/.txt /tmp/b/
sh-4.2 $ cp /tmp/a/*.html /tmp/b

So, If you want to list the files from /tmp/a without using ls command then ls /tmp/a/? or you may also use echo /tmp/a*?

Also, if you want to rename a file using wildcard then

sh-4.2 $ mv *.txt *.bak

This will convert all txt files to bak file.

Regards,
Niraj Bhagchandani 





Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Variables in Shell Script - Part 1

 Niraj Bhagchandani     07:25:00     No comments   

 
Every Programming language in existence has the concepts of variable – It can be termed as a chunk of memory which can be assigned values, read and manipute its contents. Thus, Bourne Shell aslo introduces the idea of variables. The explanation of variables is described in two different parts.
Let us look back to our First Shell Program for example which can be done easily using variables.

Please Note:

Pre-requisite: There should not be spaces around the “=” sign i.e. VARIABLE=value works but in shell scritp VARIABLE = value does not work efficiently. ( and gives command not found error ). This is because in the first case the, when the shell sees “=” symbol it treats as a variable assignment, while in the later case it assumes that VARIABLE must be the name of the command.



Enter the following code into variable1.sh:

variable1.sh
#!/bin/sh
MESSAGE="Hello World"
echo $MESSAGE
 

The above program assigns “Hello World” as a string to store into
the variable MESSAGE then echoes out the value in the
second line of the code. 


Please Note:
We need to the quotes around the string Hello World.Whereas, we could get away with ehco Hello World because echo will take any number of parameters while variable can hold only one value, also a string with spaces must be quoted as an indicatoin to treat all as one. Otherwise, the shell will try to execute World as a command after assigning MESSAGE=Hello
Also, do note that shell does not care about data types such as strings, integers, real numbers etc.
The reality is, they are stored as strings, but routines may treat strings as a number if these numbers are stored as described. For example if you assign a string to a variable then try to add 1 to it, you shall not get away with correct output as described.


sh-4.2$ x="string"
sh-4.2$ expr $x + 1
expr: non-numeric argument
sh-4.2$
 
The above output is as exptected because there is no much difference in their syntax.
 
sh-4.2$ MESSAGE="My Message" 
sh-4.2$ SHORT_MESSAGE=hi
sh-4.2$ NUMBER=3
sh-4.2$ PI=3.142
sh-4.2$ OTHER_PI="3.142"
sh-4.2$ MIXED=456xyz


To get the values from user just like printf() function in C, we can interactively set variable names using read command; for Example:

variable2.sh
 
#!/bin/sh
echo What is your age?
read age
echo "Hello friends! My age is $age."
echo ‘Hello friends! My age is $age.’

The above code will work differently while echoing with double quotes and singe quotes. Here, in Double quotes string the value of $age is printed, while in the later case (i.e. string with single quotes) the $age is printed as a string, thus the value of $age is not echoed out during execution of shell script.

Scope of Variables

The variables in C language are declared in much different way then in Bourne Shell. But if you try to read an undeclared variable, the result will always stay empty string, thus you will not get any warnings or errors. This may cause some bugs in your assignments.
Example:

sh-4.2$ HOME=/opt/lampp
and then if you type
sh-4.2$ echo $HME;

it will output empty string rather than error or warning.
Okay, friends now let us understand variables in more detail in order to really know what’s going on with variables. You will need to understand something about how this is used.
Let us create a simple script.

variable3.sh
 
#!/bin/sh
echo "VARIABLE is: $VARIABLE"
VARIABLE="hi there"
echo "VARIABLE is: VARIABLE"

Let us run the script:
sh-4.2$ ./VARIABLE.sh
VARIABLE is:
VARIABLE is: hi there

VARIABLE does not contain any value intially so, it prints empty string. Then we assign it a value, and it has the expected result.
Okay, if you understood the above program easily then let us see the next part of variable. Let us see the sequence given below.
Now run:
 
sh-4.2$ VARIABLE=hello
sh-4.2$ ./variable3.sh
VARIABLE is:
VARIABLE is: hi there
 

Ops! Something went wrong, though we have assigned the variable
(VARIABLE) in the shell it prints an empty string.  So, now let us
understand the core concept behind this.
When you call variable4.sh from shell interactively, a new shell gets started to run the script. This is partly because #!/bin/sh line is at the start of the script which we have discussed earlier.
So, to overcome the above issue there is a command callled export which has the fundamental effect on the scope of variables. Now, Let us see how export would be useful in generating output.

Example:
 
sh-4.2$ export VARIABLE
sh-4.2$ ./variable3.sh
VARIABLE is: hello
VARIABLE is: hi there

Okay, there it is you can view the line 3 of the script: the value of VARIABLE has not been changed. But, it might not pass the value interactively back to the shell.
Let us try to read the value of VARIABLE.

sh-4.2$ echo $VARIABLE
hello
sh-4.2$

Keep in mind that once the shell script exists, the shell environment is destroyed. But VARIABLE keeps its value as hello within its interactive shell.
In order to receive environment changes back from the script, we must source the script - this effectively runs the script within our own interactive shell, instead of spawning another shell to run it.

We can source a script via the "." (dot) command:

variable3.sh
 
sh-4.2$ VARIABLE=hello
sh-4.2$ echo $VARIABLE
hello
sh-4.2$ . ./variable3.sh
VARIABLE is: hello
VARIABLE is: hi there
$ echo $VARIABLE
hi there

In realiuty, it has now made the change into our shell! The best example for this is .profile and .bash_profile file. So, if you define variable in these files, then there is no need to export a variable.
Now, Let us see the common mistake that we make during the shell script. Let us monitor the shell script variable and point out its values as:

variable4.sh
 
#!/bin/sh
echo "Please provide Category name to create file: "
read CATEGORY
echo "This is the category: $CATEGORY"
echo "Let us create a file named: $CATEGORY_file"
touch $CATEGORY_file
 
Let us analyze the code here for a moment and we enter category as “shoes” as CATEGORY, then the last line of the code should actually create a file named shoes_file (here touch command is used to create a file).

Thus, the answer is a big NO. It will not create a file but instead it will give you an error as it understand $CATEGORY_file as an entire variable rather than $CATEGORY as variable and _file as a string. So, How do we define the above mentioned thing in here?

The answer to this question is as follows: we declare the variable itself in curly brackets:

category.sh
 
#!/bin/sh
echo "Please provide Category name to create file: "
read CATEGORY
echo "This is the category: $CATEGORY"
echo "Let us create a file named: ${CATEGORY}_file"
touch ${CATEGORY}_file

Thus, the above code will refer to the variable CATEGORY and that suffix which we want as “_file”.
Note:
the quotes around “${CATEGORY}_file” -- If the user enters “Tooth Brush” (with the space within) then without the quotes, the argument passed to touch command would be Tooth and Brush_file – i.e. the command would be
touch Tooth Brush_file
thus it will create 2 files instead of one.

Program Link : https://goo.gl/kQGhnM

In my next tutorial I would cover more about variables in shell script

Regards, 
Niraj Bhagchandani 
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Friday, 7 October 2016

My First Shell Script.

 Niraj Bhagchandani     08:47:00     No comments   

My First Shell script


We are about to write a script for Hello World. After that we will have some flavors of Hello World Program. First let us create a shell script file named. HelloWorld.sh

#!/bin/sh
#This is a comment line!
echo Hello World    #This is also Comment line. 

In this the first line says Unix that the file is to be executed using /bin/sh. This is where Bourne Shell is located.

The Second line is an comment which begins with a special symbol #. This marks the line as a comment, and it is ignored completely by the shell.

The only exception is when the very first line of the file starts with #! - as ours does and this is special derivative. So, if you are using csh, ksh or any other shell it would be interpreted by the Bourne Shell (/bin/sh).

The third line runs a command named echo, with two parameters, or argument i.e. "Hello" and the second is "World".

Now let us give permission to first.sh and then run by following way
Giving Permission: chmod 777 first.sh
Running Shell Script: ./first.sh
or you may also run as: sh first.sh

sh-4.3$ chmod 777 first.sh                                                                                                                                        
sh-4.3$ sh first.sh                                                                                                                                               
Hello World! 
 
You may probably expect the output from terminal itself as...
sh-4.3$ echo Hello World
Hello World
sh-4.3$
 
Now Let us View some more examples on echo ...

#!/bin/sh
#This is a comment!

echo "Hello World"  #This is a comment, Too! 
echo "Too many                  Space"
echo Too many                       Space 
echo "Star * Between"
echo Star * Between
echo "quotes" noquotes
echo Spaces  "                  " Between
echo "Quotes"*"Star"
echo `hello` world
echo 'hello' world
 
You May run the above program on the following link given below. 

Program Link : https://goo.gl/qgNoMp

This is Just the beginning... If you want know more about it do comment me.. Also, we might use more powerful command than echo in future.

Regards,
Niraj Bhagchandani 
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Posts Home

Amazon Ads

Popular Posts

  • 12 differnet examples of For Loop in Shell Script
    The Link to programs given in this tutorial is:  https://goo.gl/akbcTw Well, to begin with FOR LOOP, I would tell that there are 2 diff...
  • Powerline – Adds Powerful Statuslines and Prompts to Vim Editor and Bash Terminal
    Powerline is a great statusline plugin for Vim editor , which is developed in Python and provides statuslines and prompts for many other...
  • How to install Docker and run Docker containers on Linux Mint 18/18.1
         How to install Docker and run Docker containers on Linux Mint 18/18.1 So, now before going through all the steps update and upg...
  • While Loop in Shell Script
    To run the given example live online please visit the following link: https://goo.gl/mD69Gj There are basic loops in shell script. For ...
  • Until Loop in Shell Script.
    To run these programs online I request you to go to following link: https://goo.gl/xKheU3 The until loop is similar to while loop in shel...
  • Variables in Shell Script - Part 1
      Every Programming language in existence has the concepts of variable – It can be termed as a chunk of memory which can be assigned valu...
  • Escape character in Shell Script
    Okay!! uptil now we have seen a lot of stuffs regarding Shell Script. But how about escape characters Example: What if you want to displa...
  • Wildcards
     A wildcard is a character that can be used as a substitute for any of a class of characters in a search, thereby greatly increasin...
  • My First Shell Script.
    My First Shell script We are about to write a script for Hello World. After that we will have some flavors of Hello World Program. Fir...

Recent Posts

Categories

  • Docker in Mint 18.1
  • Docker Linux Mint
  • Install Docker
  • Install Docker in Mint 18
  • Install Docker Linux Mint
  • tecmint

Pages

  • Home

Text Widget

Blog Archive

  • ►  2017 (1)
    • ►  June (1)
  • ▼  2016 (8)
    • ▼  November (1)
      • Powerline – Adds Powerful Statuslines and Prompts ...
    • ►  October (7)
      • Until Loop in Shell Script.
      • While Loop in Shell Script
      • 12 differnet examples of For Loop in Shell Script
      • Escape character in Shell Script
      • Wildcards
      • Variables in Shell Script - Part 1
      • My First Shell Script.

Page Views:

About Me

Niraj Bhagchandani
View my complete profile

Search Here:

Powered by Blogger.

Sample Text

Copyright © Everything Shell Script | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates