Tag Archives: script

10 Mar

10 useful shell script code snippets | Linux

This post is collection of 10 useful unix / linux shell script code snippets I’ve often used.

Following these code snippet you will be able to answer following questions:
1. How to calculate script run-time in shell.
2. How to parse parameters / arguments to shell script.
3. How to safely change a directory in shell script.
4. How to check if a command was successful in shell script.
5. How to add timestamps in script log output.
6. How to check if a process is running.
7. How to colour your script output.
8. How to read variables from config file.
9. How to loop over files in a folder.
10. How to perform actions based on variable value using SWITCH…CASE

And a bonus snippet
11: How to send emails from shell script.

Let’s begin.
Continue Reading…

18 Mar

batch script to add remove prefix zero pad bulk file rename

This post has two batch scripts:
1. Batch script to rename files with zero padded number series-prefix

2. Batch script to remove prefix of perticular length

1. Batch script to rename files with zero padded number series-prefix

This script will accept file-type to be searched and lenght of zero-padded prefix to be attached.

Usage: RenZeroPad.bat

Consider you have to rename / arrange a lot of mp3 files in a perticular sequence:

File names before execution:
fileX.mp3
fileY.mp3
fileZ.mp3

Command on dos prompt: RenZeroPad.bat mp3 4

File names with attached prefix:
0001 fileX.mp3
0002 fileY.mp3
0003 fileZ.mp3

[ad#ad-2-300×250]

The Batch Script to add zero pad digits prefix:

@echo off
setLocal EnableDelayedExpansion

set /a cnt=1
for %%i in (*.%1) do (
call :Set0Pad %2

set newName=!str! %%i
ren “%%i” “!newName!”
)

:Set0Pad
set padcntr=0000000000%cnt%
set str=%padcntr:~-%1%
set renstr=%str%
set /a cnt+=1

** Download available at the end of the page.

2. Batch script to remove prefix of perticular length

This script will accept file-type to be searched and lenght of prefix to be removed.

Usage: RemovePrefix.bat

Consider you have to rename / remove prefixes from a bunch of files:

0001_fileX.doc
0002_fileY.doc
0003_fileZ.doc

Command on dos prompt: RemovePrefix.bat mp3 5

Files will be renamed with removed prefixes as follows:
fileX.doc
fileY.doc
fileZ.doc

The Batch Script to remove prefix:

@echo off
setLocal EnableDelayedExpansion

set /a cnt=1
for %%i in (*.%1) do (
set str=%%i
set newstr=!str:~%2!
ren “%%i” “!newstr!”
)

Download: RemovePrefix.bat and RenZeroPad.bat

30 Jan

Upload Image to MySQL using PHP

Upload Image to MySQL using PHP

As a new-bie to php/mysql, I tried different stuffs. So here I’m with my php code for Image Upload to MySQL. Its a quite simple code with two php files one to display and one to upload.
For Image Upload code, I’ve added code download link upload-image-mysql-demo.zip at the end of the page.

As a new-bie to php/mysql, I tried different stuffs. So here I’m with my php code for Image Upload to MySQL. Its a quite simple code with two php files one to display and one to upload.

For Image Upload code, I’ve added code download link upload-image-mysql-demo.zip at the end of the page.

CREATE TABLE `pix` (

`pic_id` int(11) NOT NULL auto_increment,

`pic_name` varchar(100) NOT NULL,

`pic_data` longblob NOT NULL,

PRIMARY KEY  (`pic_id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Get 2 php files:- Image.php, showImage.php.

Set Connection parameters accordingly.

/* Image.php*/

<?php

$con = mysql_connect(“127.0.0.1:3306″,”root”,””);

if (!$con)

{

die(“Could not connect: ” . mysql_error());

}

$DB = mysql_select_db(“test”, $con);

move_uploaded_file($_FILES[“uploadedfile”][“tmp_name”],”latest.img”);

$instr = fopen(“latest.img”,”rb”);

$image = addslashes(fread(fopen(“latest.img”,”r”),filesize(“latest.img”)));

mysql_query (“insert into pix (pic_name, pic_data) values (“myImage”, “‘.$image.'”);”);

?>

<html>

<form enctype=”multipart/form-data” method=”POST”>

<img src=showImage.php?gim=1 width=500 height=150 alt=”hell”>

<br><hr>

<input type=”hidden” name=”MAX_FILE_SIZE” value=”100000″ />

Choose a file to upload(<100 KB): <input name=”uploadedfile” type=”file” /><br/>

<input type=”submit” value=”submit” name=”submit” />

</form>

<html>

/*showImage.php*/

<?php

$con = mysql_connect(“127.0.0.1:3306″,”root”,””);

if (!$con)

{

die(“Could not connect: ” . mysql_error());

}

$DB = mysql_select_db(“test”, $con);

$res = @mysql_query(“select * from pix order by pic_id desc limit 1”);

if ($row = @mysql_fetch_assoc($res))

{

$title = htmlspecialchars($row[pic_name]);

$bytes = $row[pic_data];

}

header(“Content-type: image/jpg”);

print $bytes;

mysql_close();

?>

Click here to download zip file for above demo of Uploading image to mysql using php.

-- Kedar Vaijanapurkar --