Archive for October, 2008
How to use assert function in C
Oct 8th
Typically the assert function is used to validate input arguments passed to a function before using them. In general, input argument checking is enabled in debug builds and disabled in production builds. The assert function validates the expression if it is true. If the expression is false, it invokes abort function to terminate the process.
void assert(scalar expression);
The following example shows how typically assert is used.
void which_fruit(char *name)
{
assert(name);
strcpy(name,"apple");
}
In the above example, if a NULL pointer is passed to which_fruit() function, assert evaluates the expression to be false and process gets terminated.
In case of production builds where assert needs to be disabled More >
How to backup MySQL database
Oct 4th
The MySQL utility command mysqldump can be used to take backup of MySQL database. The output generated from this command can be used to create the same database in another host.
In the following example, we try to login to MySQL server as “root” with password and try to dump the contents of database “mydb“.
[neo@techpulp ~]# mysqldump -u root -p mydb > mydb-backup.sql Enter password: [neo@techpulp ~]# ls -l mydb-backup.sql -rw-rw-r-- 1 neo neo 23876589 2008-10-04 03:29 mydb-backup.sql [neo@techpulp ~]#
The output present in the file mydb-backup.sql will contain a series of MySQL statements and exactly same database can be rebuilt from scratch using More >
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