Bash
How to catch signals in a bash script
Jul 29th
POSIX compatible UNIX systems like Linux supports standard signals as part of inter process communication. While some signals like SIGINT can be ignored, SIGKILL and SIGSTOP cannot be caught, blocked or ignored.
Bash provides a command “trap” to assign a signal handler. The following example script catches SIGINT signal that is generally received when user presses Ctrl+C key combination. A bash script will generally terminate on pressing Ctrl+C but it can chose to ignore the signal or do clean up before terminating using signal handler.
#!/bin/bash
trap mysighandler INT
mysighandler()
{
echo 'You have pressed Ctrl+C.. Ignoring...'
}
for ((i=0;i<10;i++))
do
echo More > How to loop or iterate through the output of ls command in a bash script
Jan 13th
If your original purpose is to iterate through the list of files and directories present in the current directory, you don’t even have to run ls command for that.
for i in *; do echo "$i" done
Alternately you can use ls to get the list of files and directories as shown below.
for i in `ls`; do echo "$i" done
Other way of writing the above example is:
for i in $(ls); do echo "$i"; done;
The ls command can be used to select files of certain pattern. The following example selects only files will extension “.sh”.
for i in $(ls *.sh); do echo More >
How to generate or print a sequence of numbers in bash
Jan 11th
The command “seq” can be used to generate a sequence of numbers in bash. The following is the syntax of “seq” command.
seq LAST seq FIRST LAST seq FIRST INCREMENT LAST
Look at the following to understand how it works.
Example 1:
[sara@sara-desktop ~]$ seq 5 1 2 3 4 5 [sara@sara-desktop ~]$
Example 2:
[sara@sara-desktop ~]$ seq 5 10 5 6 7 8 9 10 [sara@sara-desktop ~]$
Example 3:
[sara@sara-desktop ~]$ seq 0 10 50 0 10 20 30 40 50 [sara@sara-desktop ~]$
Generally the “seq” command prints one number per line as it assumes “\n” as the separator. However the character used as More >
How to print a bash variable along with other text
Jan 10th
A general programmer tends to use a simple dollar sign ($) before each variable. However its gets difficult if a variable hash to be echoed along with some other as shown below.
An attempt to print “redtape” fails using the following method as bash attempts print value of “COLORtape” variable.
bash# COLOR=red bash# echo $COLORtape bash#
In such cases, braces should be used to protect the bash variable. The following example does that.
bash# COLOR=red
bash# echo ${COLOR}tape
redtape
bash#
How to convert a relative path to an absolute path in bash
Jan 9th
The “readlink” command can be used to convert a relative path to an absolute path. Although readlink primarily for reading value of a symbolic link, it works transparently on files and directories.
[liz@techpulp ~]# readlink ../../var/www/wiki/index.php /usr/share/mediawiki/index.php [liz@techpulp ~]#
Let us see how it behaves for an invalid path (i.e missing file or directory).
[liz@techpulp ~]# readlink ../../var/www/wiki/index.php1 [liz@techpulp ~]#
As you can see, readlink command doesn’t print anything if the file or directory is not present. Because our intention is to just convert a relative path to absolute path and not to worry about its existence, we can use “-m” option More >
How to check if bash variable is a number or not
Dec 20th
The following example illustrates the simplest way to check if a variable is holding an number or not. Consider the following scripts which checks the first command line argument and tells us if it is a number or not.
[neo@techpulp ~]# cat numcheck.sh #!/bin/bash if [ "$1" -eq "$1" >& /dev/null ]; then echo "'$1' is an integer" else echo "'$1' is not an integer" fi [neo@techpulp ~]#
Basic logic behind this is to use the variable in an integer expression and find the result of the operation. Let run the above script with different arguments.
[neo@techpulp ~]# ./numcheck.sh 123 '123' is 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 to reset or unset a variable in bash
Sep 16th
The built in command “unset” can be used to unset a bash variable or a function. This is typically useful if you are not sure if a variable or a function is defined some where else with same name.
unset [-fv] [name ...]
The “unset” built-in function takes two option “-f” and “-v” off which “-f” is used to unset a function and “-v” is used to unset a variable. If no special option is given then bash attempts to unset bash variable.
Following is the example usage:
unset myvariable unset -v myvariable unset -f myfunction
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 > 

Recent Comments