Bash shell script to run more commands which is stored in a file

Bash shell script to run more commands which is stored in a file

You might be tired by finding the right Bash shell script to run more commands which are stored in a file. Here is the script which you are searching…

In this example going to use below commands in the file to execute using the script.

df -h

uname -a

ls -l

netstat

As next step, we are going to create a file with above commands assigned to a variable. If we are using this commands directly without assigning to a variable, the script will not execute the commands properly. Because of space(df -h) which we used in between commands will be taken as a separator by the script. So that only we are saving the commands in a variable.

[[email protected] ~]# cat > cmd.lst
DF=`df -h`
UNAME=`uname -a`
CAT=`cat /etc/resolv.conf`
NET=`netstat` 
^c
[[email protected] ~]# 

Then create the script to run the commands which are stored in cmd.lst file

[[email protected] ~]# cat > cmdscritp.sh
#!/bin/bash
sh cmd.lst
for comm in `cat cmd.lst`
do
 for cmd in `cat cmd.lst | awk -F "=" {'print $1'}`
 do
 echo "Going to execute:" $cmd
 eval $cmd
 if [[ $? -eq "0" ]]
 then
 echo $cmd "executed successfully..."
 else
 echo "last command didn't executed properly. So, Exiting from the script..."
 exit
 fi
 done
done

and now provide the execute permission using chmod command for both files.

[[email protected] ~]# chmod +x cmd.lst
[[email protected] ~]# chmod +x cmdscritp.sh

[[email protected] ~]# ll | grep cmd
-rwxr-xr-x. 1 root root 69 Dec 28 23:58 cmd.lst
-rwxr-xr-x. 1 root root 326 Dec 28 23:58 cmdscritp.sh

Now run the script

[[email protected] ~]# ./cmdscript.sh 
cmd.txt: line 4: netstat: command not found
cmdscript.sh cmd.txt diff2.sh diff.sh lvcheck.txt post pre sh
Goind to execute: df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 10474496 2097740 8376756 21% /
devtmpfs 1887604 0 1887604 0% /dev
tmpfs 1894500 0 1894500 0% /dev/shm
tmpfs 1894500 8444 1886056 1% /run
tmpfs 1894500 0 1894500 0% /sys/fs/cgroup
tmpfs 378900 0 378900 0% /run/user/0
tmpfs 378900 0 378900 0% /run/user/1000
df executed successfully...
Goind to execute: uname
Linux
uname executed successfully...
Goind to execute: ls
cmdscript.sh cmd.txt diff2.sh diff.sh lvcheck.txt post pre sh
ls executed successfully...
Goind to execute: net
./cmdscript.sh: line 8: net: command not found
last command didnt executed properly. So, Exiting from the script...

 

Script working successfully. Thanks for reading the post and support.

Help command and Data Types in Python

In this post we are going to see Help command and Data Types in Python.

Use help along with command which you want need to know more that command.

Here is the example:

Below command will shows the help about print command.

[[email protected] ~]# python
Python 2.7.5 (default, Aug 2 2016, 04:20:16)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> help ('print')

 

Data types:

  1. Numbers
  2. Strings
  3. Lists
  4. Tuple
  5. Dictionary

Numbers:

Three types of numbers data type available in Python.

Integers: 10

Floating point: 2.1, 3.45

Complex numbers: (4+1J), (3.6 – 5.4a).

If..else.. statement in Bash shell scripting

If..else.. statement in Bash shell scripting

We are going to see if statement in bash shell scripting in Linux/UNIX environment.

If statement is playing the most important role in bash shell scripting and other programming languages. Its working based on condition and we can write another if statement inside of an if statement.

Syntax of If statement:

if [ condition..]
then
  < commands...>
else
  < commands... >
fi

Basic if statement says that, if a condition if valid then, it will execute the single/ set of commands which is comes under then will execute and if a condition not valid then, it will execute the single/ set of commands which comes under else. Finally, it will be ended.

We have some operator which is useful in if statement conditions for validation. Below they are.

Operator Description
 =, ==, -eq  This is to check string/numeric value is equal to another string/ numeric value.
 !=,-ne  This is to check string/numeric value is not equal to another string/ numeric value.
 <=, -le  This is to check string/numeric value is less than or equal to another string/ numeric value.
 >=, -ge  This is to check string/numeric value is greater than or equal to another string/ numeric value.
 <, -lt  This is to check string/numeric value is less than to another string/ numeric value.
 >, -gt  This is to check string/numeric value is greater than to another string/ numeric value.

Example:

[[email protected] ~]# vi newscript.sh

#!/bin/bash
if [[ $1 -gt "100" ]]
then
 echo "Given values is greater than 100.."
else
 echo "Given value is equal to or less than 100..."
fi

Then provide the execute permission to the script using chmod command.

[[email protected] ~]# chmod +x newscript.sh

Now will run the script by passing a parameter

[[email protected] ~]# ./newscript.sh 101
Given values is greater than 100..

 

Basic’s of Python scripting

In this post we are going to see Basic’s of Python scripting.

Use Python command to move to python interpreter like below and using quit() to exit from the interpreter.

and here we used simle print command to print any string.

[[email protected] ~]# python
Python 2.7.5 (default, Aug 2 2016, 04:20:16)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print ('Hello World')
Hello World
>>> quit()
[[email protected] ~]#

As you all aware will write bash script in linux/unix. In Python also will create shell script. For that, we need to do simple change in first line like below and even we can create with extension with .sh.

#!/usr/bin/env python

Now we will create our first simple python script to display set of strings. To create this script will use any editor and here i’m going to use vi editor.

[[email protected] ~]# vi first.py
#!/usr/bin/env python
print "Hello, This is my first script"

use :wq to save and exit from this file and now should provide execute permission using chmod command to run this script.

[[email protected] ~]# chmod +x first.py

Finally will run the script using ./ like shell scripting.

[[email protected] ~]# ./first.py
Hello, This is my first script

We successfully created our  first python script.

Python installation in Linux/Unix

In RedHat by default python installed. We are going to see the Python installation in Linux/Unix.

First we have to prepare our system to install Python.

Preparing system:

login as root user, as its a administrator and having full privileges.

Use subscription-manager to know whether you have access to RedHat software repository or not.

[[email protected] ~]# subscription-manager repos --list-enabled

if you don’t see any repository enabled, than your machine not registered or not having subscription.

Now update using yum command.

[[email protected] ~]# yum update

Once you executed the above command with valid subscription, all the packages will get updated in your OS.

Now setup your environment:

As you aware already, we are going to use yum to install and check Python package.

Check whether python installed or not using yum.

[[email protected] ~]# yum list installed | grep python

If the package not installed, use below command to install python.

[[email protected] ~]# yum install python

above command will ask your confirmation to proceed install. Simply type  and press enter.

Once executed above command, again check whether its installed or not.

[[email protected] ~]# yum list installed | grep python

Use python  command to run python in interactive mode.

[[email protected] ~]# python
Python 2.7.5 (default, Aug 2 2016, 04:20:16)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
[[email protected] ~]#

Finally we installed python in linux.

About Python

About Python:

Python language created by Guido van Rossum.

Features of Python

Built on c  routines

Easy to use and debug

Portable

Object oriented

Interface with other languages

compiled and Interpreted language

Open and free

 

Download link:        www.python.org

To know python version use below command

python -v

Note: Any line starts with # will commented and not used by script while executing.

Will create a script file with .py extension and should use python command to run the python script.

Example:     

python first.py

Bash Shell Script – 2

Bash Shell Script - 2

Today we are going to see how to use if statement in bash shell scripting.

before going to work with if statement, we should know the options which will be used in if statement to compare conditions.

-gt :    greater than
-lt  :    less than
-eq :   equal too
-ge :   greater than or equal
-le  :   less than or equal

We are going to see a script with a simple if statement.

From the below script we are going to check given number is equal to 100 or not and using read command to get keyboard interaction while running the script.
It will read the number from our keyboard interaction and store it in a variable called number. Then will check for the condition, which we mentioned in if statement and display the string.

create a file called ifstat.sh using vi

#vi ifstat.sh

#!/bin/bash
echo “Enter a numeric value”
read number
if [ $number -eq 100 ]
then
echo “Numeric value is 100”
else
echo “Numeric values is not equal to 100”
fi

We can use elif, if we need to execute more than one statement for if condition.

For the same numeric value validation we are going to change use this elif  option.

#vi ifstat1.sh

#!/bin/bash
echo “Enter a numeric value”
read number
if [ $number -eq 100 ]
then
echo “Numeric value is 100”
elif [ $number -gt 100 ]
then
echo “Numeric values is greater then 100”
elif [ $number -lt 100 ]
then
echo “Numeric values is less then 100”
fi

and we have nested if statement to validate the sme numeric values.
nested if is nothing but we are using if statement inside of if statement.

#vi ifstat2.sh

#!/bin/bash
echo “Enter a numeric value”
read number
if [ $number -eq 100 ]
then
echo “Numeric value is 100”
else
if[ $number -gt 100 ]
then
echo “Numeric values is greater then 100”
else
echo “Numeric values is less then 100”
fi
fi

Bash Shell Script – 1

Bash Shell Script - 1

Bash shell scripting is collections of Linux commands in a single file and we should know where and when to use the command. Its mandatory to know all the basic commands for bash shell scripting.

Execute permission is must to run the script.

Using below command will add the execute permission to a file.

#chmod +x testscript.sh

Here am using testscript.sh file for simple script.

Now check using “ll” command whether the permission has been added or not.

#ll | grep -i testscript.sh

We can use three methods to execute the bash shell script.

1. ./testscript.sh
2. sh testscript.sh
3. bash testscript.sh

Here am writing script to display a string.
Echo command will do simple printing whatever we are giving with double quotes.

Already i have created testscript file using vi editor.

#vi testscript.sh

Now will write a script to display Current logged in user, date and Count of users logged in.
Use the vi editor to create script file.

#vi testscript1.sh



type the below set off commands to create this script.

Save and exit from the file.

change the permission using chmod command.

#chmod +x testscript1.sh

Now run the script and check for the output.



like above will create bash shell scripts for our requirements.
Will see some other examples in next post.