Passing filename arguments to PHP from shell and space issue…

Today, while working with a project to import thousands of articles from text files to wordpress,  I had faced a trouble while I am executing a shell script to iterate through all text files and call a PHP script with sending the filename as argument. The PHP script would then process the file as our requirement. The shell script looked like:

for i in `find . -type f -name '*.txt'`;

do

php import_posts.php $i

done

Here, $i would be the filename and it is passed to PHP script for processing. But the problem is that, when the filename (including directory name) consists any whitespace in its name, the script would broken. For example, some files path looked like
/home/user/articles/Internet Business/Web Hosting/how_to_start_your_company.txt

Here, notice the space after Internet and Web. The shell script was breaking just after Internet. It was sending /home/user/articles/Internet as argument to PHP script and obviously PHP script was failed to find this file.

After searching a lot, finally I found a solution.

It was really simple. I am quoting from his blog:

If none of your files or folders have spaces in their names, then this isn’t a problem, but that is increasingly unlikely nowadays.  If your songs do have spaces in their filenames, then the solution is to change how the shell splits that output.  This is controlled by the variable $IFS, which is set to spaces, tabs, and newlines by default.  We’d like it set to just newlines, so we do:

So, here is the solution. I just put the following line at the top of my shell script.

export IFS=$'\n'

After that everything is fine. The script is running exactly how I intended it to run.

Thanks for Anthony for his solution.

Easy Control Nginx!

Nginx

Nginx

Recently I am trying to learn managing nginx web server. Most probably it is going to take the place of Apache or at least seat beside Apache. The main benefit Nginx is claimed to be low memory footprint.

So far I have configured three VPS with nginx (also mysql & php). But every time I felt uneasy to start and stop the service as it can’t be started or stopped as ordinary service call.

For example:

To start apache/httpd we write:

service httpd start

But to start Nginx we have to write:

/usr/local/nginx/sbin/./nginx

[I assume that Nginx is installed in default directory]

Again to stop apache, we write:

service httpd stop

But in case of Nginx:

kill -9 `cat /usr/local/nginx/logs/nginx.pid`

or

killall -9 nginx

Today I thought to make it a bit easier. So after mining the internet, I have made a small script to take care of start, stop, restart and check the status of Nginx. I am new to shell scripting.

How to implement it?

  1. I expect your Nginx installation is completed successfully and you can start/stop the server using the earlier methods.
  2. Download this file
  3. Store the file in /sbin/ as nginx. So that path of this file will be /sbin/nginx
  4. Chmod it to 744 by calling:
    chmod 744 /sbin/nginx
  5. You are done.

Now to start the server:

nginx start

Stopping the server:

nginx stop

Restarting the server:

nginx restart

Checking Status:

nginx status

Hope you like it :).

So, any suggestion is