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 >
What are the useful fields of $_SERVER array in PHP
Jan 12th
The following table describes useful fields in $_SERVER array in PHP.
Name Description PHP_SELF Returns the filename of the current script SERVER_PROTOCOL Name and revision of the information protocol ‘HTTP/1.0‘ or ‘HTTP/1.1′ REQUEST_METHOD Returns the request method used to access (GET/POST/PUT) REQUEST_TIME Returns the timestamp from the beginning DOCUMENT_ROOT Returns the root directory under which the current script is executing. This directory is based on server’s configuration. HTTP_REFERER Returns the page address that referred HTTP_USER_AGENT Returns the contents of user agent extracted from HTTP header. This information is typically used for identification of user’s browser and operating system. REMOTE_ADDR Returns the 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 configure apache virtual host in Microsoft Windows for safer development
Jan 6th
Typically for web development, one likes to simulate the web environment by using actual domain name as is instead of using some local IP address. It is very useful in case if you want to host WordPress and would like to copy your version of database as is from development server to the the actual hosting server. The applications like WordPress store domain names in the database while storing your posts as part of permalinks etc.
Another use for having real simulated environment is to ensure your ad slots like Google AdSense, DoubleClick etc appear without any issues. Otherwise the ads More >
How to do search and replace operation on a portion of a string in perl
Dec 24th
I recommend reading this article to know more about usage of substr function in perl. Basically substr function and a typical search and replace mechanism (s///g) can be combined to achieve the goal in a single statement.
Look at the following script which replaces the word “goes” to “moves” in first first 10 characters of a string variable “$str”.
#!/usr/bin/perl $str="What goes around comes around"; print $str . "\n"; substr($str,0,10) =~ s/goes/moves/g; print $str . "\n";
Output of the above script is as follows:
[neo@techpulp ~]# perl replsubstr.pl What goes around comes around What moves around comes around [neo@techpulp ~]#
You can play with More >
How to get substring in perl – substr function
Dec 23rd
In perl, the substr function can be used to access individual characters or a portion of a string. The syntax of substr function is as follows:
substr ( string, offset [, count] )
Arguments:
- string – string from which you would like to extract a sub-string
- offset – this indicates start of the sub-string you want to extract
- count – length of the sub-string to extract
A negative number also can be specified for “offset” argument that make substr to count the characters in the reverse direction (i.e from the end). If “0″ is specified as offset, sub-string is extracted from beginning of the string. If a positive More >
How to delete a file whose name starts with minus character
Dec 21st
Sometimes an untended paste operation of screen dump results in unwanted files. If a file name starts with a minus (-) character, it is not possible to delete the file using traditional command as anything that starts with a minus (-) character is treated as one of the command line options.
For example, look at the following file “-myfile.txt“.
[sara@techpulp ~]# ls -myfile.txt [sara@techpulp ~]#
Traditional way fails to remove the file.
[sara@techpulp ~]# rm -myfile.txt rm: invalid option -- 'm' Try `rm --help' for more information. [sara@techpulp ~]#
You can use “–” option to stop “rm” command to stop expecting further command More >


Recent Comments