Programming
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 > How to avoid full compilation for a small change in header file in C
Sep 13th
Sometimes it is very annoying to wait for full compilation when a small and non-invasive change is made in a common header file that is included by many C files. This typically happens due to header file dependency rules. If you are sure that your change doesn’t really affect other C files expect a few, you change the time stamp of the modified header to a date in past. Also you should change the time stamp of affected C files to current. This ensures that no other files compile except the ones for which you changed the time stamp.
Identifying the More >
How to access file or directory names with spaces in bash command line
Sep 12th
When you have to deal with files or directories whose names contain space characters, you will have to use a escape character before each space character.
Bash is a shell which facilitates users to run commands. Bash expects a list of word out of which first word is assumed to the command or program to be run and the remaining words are passed as command line arguments to the program.
So if you try to access a file names “My File.txt” as shown below, it will not succeed because as you can see the original file name is passed as two independent More >
How to search in bash command history easily
Sep 11th
If is often needed to run a previously executed command. Generally one uses UP and DOWN arrows to navigate through the command history to get the previous command again.
Especially if you have huge list of commands in history and using UP and DOWN arrows is time consuming, then you can search for the command by typing “Ctrl+r“. The bash shows a prompt as shown below when you press Ctrl+r.
[neo@techpulp ~]# (reverse-i-search)`’:
Now you can just type some matching characters and when you are happy with the selected command, just press ENTER key to execute the command. Instead if you want to More >
Why syntax highlighting is off for files opened using cscope
Sep 9th
Those who love syntax highlighting in Linux may be a bit annoyed when they see no syntax highlighting when a file is opened in cscope. Well, this ain’t cscope’s problem. It’s just that cscope doesn’t know (or at least it doesn’t attempt to learn) that you have vim command in the system. So cscope uses standard vi command instead of vim command while opening a file. This creates trouble for user as the general features of vim listed below are not seen when the file is opened with in cscope environment.
- Code syntax highlighting
- Remembering last visited line of a file
- Remembering history More >
How to bind my mysql server to a specific IP address
Sep 6th
MySQL server contains a file my.cnf in /etc direcory of Linux. This file contains configuration of MySQL server as well as the configuration parameters of the MySQL client.
To make MySQL server listen on a specific IP address, you need to add a line similar to the following with IP address of your choice under mysqld section.
[root@techpulp ~]# cat /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Default to using old password format for compatibility with mysql 3.x # clients (those using the mysqlclient10 compatibility package). old_passwords=1 # To allow mysqld to connect to a MySQL Cluster management daemon, uncomment # these lines 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


Recent Comments