How to loop or iterate through the output of ls command in a bash script

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

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 print a bash variable along with other text

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 configure apache virtual host in Microsoft Windows for safer development

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 delete a file whose name starts with minus character

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 >