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.

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

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

[root@server ~]# 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.

[root@server ~]# chmod +x cmd.lst
[root@server ~]# chmod +x cmdscritp.sh

[root@server ~]# 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

[root@server ~]# ./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.

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:

[root@server ~]# 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.

[root@server ~]# chmod +x newscript.sh

Now will run the script by passing a parameter

[root@server ~]# ./newscript.sh 101
Given values is greater than 100..

 

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.