Posts tagged Bash Programming
How to find IP address of an interface using bash scripting
Oct 1st
In Linux, ifconfig command is used to view the information about network interfaces. Let us examine the output of the command to determine how we can parse the information for IP address.
[neo@techpulp ~]# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 03:A6:2E:52:3D:BA inet addr:66.27.16.64 Bcast:66.27.16.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:14257 errors:0 dropped:0 overruns:0 frame:0 TX packets:4364 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:4831053 (4.6 MiB) TX bytes:663388 (647.8 KiB) Interrupt:21 Base address:0x2000 [neo@techpulp ~]#
The IP address information is present in second line containing keyword “inet addr”. The following script accepts an interface name as command line argument More >
How to extract extension from a file name in bash
Sep 29th
The following trick can be used to extract file extension from a name.
[neo@techpulp ~]# MYFILE=filename1.txt
[neo@techpulp ~]# echo $MYFILE
filename1.txt
[neo@techpulp ~]# echo ${MYFILE##*.}
txt
[neo@techpulp ~]#
Let us try the same on a file name with full path.
[neo@techpulp ~]# MYFILE=/home/neo/filename1.txt
[neo@techpulp ~]# echo ${MYFILE##*.}
txt
[neo@techpulp ~]#
How define local variables in a bash function
Sep 15th
The keyword “local” should be used to define a local variable with in a bash function. It is good practice to define local variables with in a bash function to avoid unwanted modification of global variables. The following example script shows how to define a local variable.
[neo@techpulp ~]# cat local.sh
#!/bin/bash
function fun1()
{
local var1="hello"
echo Inside function: $var1
}
fun1
echo Outside function: $var1
[neo@techpulp ~]#
Let us run the above script:
[neo@techpulp ~]# sh local.sh Inside function: hello Outside function: [neo@techpulp ~]#
Let us use exactly same name for a global and a local variable and see the scope of More >
How to implement substring() like function in bash
Sep 14th
Look at the following snippet to understand how to extract specified number of characters from a given offset in a string.
[neo@techpulp ~]# cat substr.sh
#!/bin/bash
str="this is a mysubstring test"
subs=${str:10:11}
echo Length of string is ${#str}
echo Sub string starting at offset 10 and len 11 is \"$subs\"
[neo@techpulp ~]#
Here is the output of the above script.
[neo@techpulp ~]# sh substr.sh Length of string is 26 Sub string starting at offset 10 and len 11 is "mysubstring" [neo@techpulp ~]#
Now let us implement a generic function substring:
[neo@techpulp ~]# cat sstr.sh
#!/bin/bash
# Usage: substring <string> <offset> <length>
function substring()
{
local More > Count number of characters (strlen) in a bash variable
Aug 30th
The following example shows the functionality of strlen in a bash script.
[neo@techpulp ~]# echo $HOME
/home/neo
[neo@techpulp ~]# echo ${#HOME}
9
bash#
The following defines an utility function that can be used in a bash script.
#!/bin/bash
function strlen() { echo -n ${#1}; }
len=`strlen $HOME`
echo $len
How to use logical OR and logical AND in a bash script
Aug 29th
The operators used in bash scripting for logical OR and logical AND are “||” and “&&” respectively.
The following snippet shows example usage of logical OR operator.
if [ "$a" -eq 30 ] || [ "$b" -eq 40 ]; then
echo The logical OR condition met
else
echo The logical OR condition not met
fi
The following snippet shows example usage of logical AND operator.
if [ "$a" -eq 24 ] && [ "$b" -eq 47 ]; then
echo The logical AND condition met
else
echo The logical AND condition not met
fi
How to write infinite loop in Bash using for or while statement
Oct 17th
The while-do-done statement can be used to program an infinite loop in bash. The following sample shows how it can be done.
#!/bin/bash while [ 1 ]; do echo I am in infinite loop. Press Ctrl+C to stop me done
The break statement can be used to exit the while loop. The following sample code breaks away from while loop after iterating 10 times.
#!/bin/bash
i=1
while [ 1 ];
do
i=`expr $i + 1`
echo loop $i
if [ "$i" == "10" ]; then
echo Exiting while loop after iterating $i times
break
fi
done
echo end of shell script
The following will be the output More >
How to generate temporary file name in bash using RANDOM environment variable
Oct 17th
Whenever the $RANDOM environment variable is accessed, it gives a random number. This can be used to generate a random file name. In the following example randfile function takes prefix and suffix of the temporary file name and returns a temporary file name after checking for its existence.
#!/bin/bash
function randfile
{
while [ 1 ];
do
TNAME="$1""$RANDOM""$2"
if [ ! -f $TNAME ]; then
echo $TNAME
exit 0
fi
done
}
NAME=`randfile /tmp/test- .tmp`
echo $NAME
Export a bash function as command
Oct 4th
You can use a function written in bash script to use like any other Unix/Linux command. This function can take command line arguments and return a value as exit code similar to standard commands. In the following example, we try to identify if a directory exists with the given name. The function returns 1 if a directory exists. Otherwise returns 0. You can copy the following piece of code and keep in a script or paste it on the console.
function isdir()
{
echo REQ isdir $1
if [ -d "$1" ]; then
return 1
else
return 0
fi
}
export More > 

Recent Comments