Linux Blog

Coppermine Photo gallery Upload Script

Filed under: Shell Script Sundays — TheLinuxBlog.com at 9:15 pm on Sunday, May 31, 2009

This week I bring you a script that I helped Kaleb (who has written posts here before) write. Well, I got him started with it, using curl and he rolled with it and finished it up. Here is the script:

#!/bin/bash
# Script to Upload to http://kporter.homelinux.com/~kaleb/cpg14x
# Written by Kaleb Porter May 23 2009
# with help of www.thelinuxblog.com
# email: porterboy55@gmail.com
# if you wish to use this code for something else please give me credit

IMAGE=”$1″
URL=”http://kporter.homelinux.com/~kaleb/cpg14x/upload.php”
DA=`date ‘+%d%b%y-%N’`
# If the user does not specify a file or url
if [ -z "$1" ]; then
echo “You did not give a file to upload”
echo “Takeing a screenshot in 3 Seconds…”
sleep 3
scrot $DA.png
IMAGE=$DA.png
fi
FI=`echo “$IMAGE” | grep ‘^[a-z]*://’`
AL=2
FIUP=`curl -s -F control=phase_1 -F blaa=continue -F file_upload_array[]=@$IMAGE $URL | grep unique_ID | awk -F\” ‘{print $6}’`
URLUP=`curl -s -F control=phase_1 -F blaa=continue -F URI_array[]=$IMAGE $URL | grep unique_ID | awk -F\” ‘{print $6}’`

#Get the title for the image from the user and change all the spaces to “%20″
echo “Enter a title for the image”
read TITLE1
TITLE=`echo $TITLE1 | sed ‘s/ /\%20/g’`

#Get the Description for the image from the user and change the spaces to “%20″
echo “Enter a discription”
read DES1
DES=`echo $DES1 | sed ‘s/ /\%20/g’`

#Get the keywords for the image from the user and change the spaces to “%20″
echo “Enter keywords (separated by spaces)”
read KEY1
KEY=`echo $KEY1 | sed ‘s/ /\%20/g’`

if [ -z "$FI" ]; then

UNIQUE_ID=$FIUP
#echo “Choose the NUMBER value for the album you want”
#curl -s -F control=phase_2 -F unique_ID=$UNIQUE_ID $URL | awk ‘/name=”album”/{disp=1} {if (disp==1) print} /<\/select>/{disp=0}’ | grep ‘value=”[0-9]“‘ | sed ‘s/<option//’ | sed ‘s/<\/option>//’ | sed ‘s/>//’
#read AL
curl -o /dev/null -d “control=phase_2&unique_ID=$UNIQUE_ID&album=$AL&title=$TITLE&caption=$DES&keywords=$KEY&blaa=continue” $URL
exit 0
else

# If the image is from a URL
UNIQUE_ID=$URLUP
#echo “Choose the NUMBER value for the album you want”
#curl -s -F control=phase_2 -F unique_ID=$UNIQUE_ID $URL | awk ‘/name=”album”/{disp=1} {if (disp==1) print} /<\/select>/{disp=0}’ | grep ‘value=”[0-9]“‘ | sed ‘s/<option//’ | sed ‘s/<\/option>//’ | sed ‘s/>//’
#read AL
curl -o /dev/null -d “control=phase_2&unique_ID=$UNIQUE_ID&album=$AL&title=$TITLE&caption=$DES&keywords=$KEY&blaa=continue” $URL
exit 0
fi

If there are any questions you can pretty much read the Shell Script Sundays column and figure out everything you need to know. Now that the upload script works, and tries to take a screenshot with scrot, the next step is a check to see if scrot exists, if it doesn’t a check for import, if not an error message.

It really does amaze me at the capabilities of the shell. Especially how mashable it is and how you can combine it with pretty much anything, this script is a great example of combining the power of the shell with the intrawebs. Well, I hoped you learned something, and as always if you have any questions, you know where the comment box is.

- Owen.

Using Bash Scripts in Web Applications

Filed under: Shell Script Sundays — TheLinuxBlog.com at 2:22 pm on Sunday, May 25, 2008

Using bash scripts for web applications is not exactly rocket science, nor is it necessarily the best idea in the world but it can be handy to do if you already have a bash script and want to use its functionality on the web. There are a couple of ways to use bash scripts on the web.

The first that I know of is as a CGI. All that you have to do for this one is create a cgi-bin or allow files with the extension .cgi to be executed this is done with apache in your httpd.conf file.

The Second is to use another scripting language to call the script. The easiest way for me is to use PHP. A system call to the script file can my made using the exec() function. Just make sure that the file has execute rights for the user that your web server runs as. Here is an example of using the exec() function in PHP:

$output = exec(‘/usr/local/bin/yourscript.sh’);

The Third method is to use Server Side Includes to include the script. I personally am not familiar with setting up SSI’s but this is how you execute a command from within a SSI:

<!–#exec cmd=”/usr/bin/date” –>

Which ever method you choose precautions have to be taken. Make sure that all inputs are sanitized so that a user cannot escape the command, pipe output to another file or manipulate the system in another way. In PHP it is easy to do this, but I can not speak for CGI’s or SSI’s. I hope this shows some insights as to how you can run bash scripts in your web application. If you have any other methods such as using mod_python or maybe tcl, please post them as a comment!

Shell Script to get user input

Filed under: Shell Script Sundays — TheLinuxBlog.com at 2:06 am on Sunday, January 27, 2008

Creating a shell script to get input is rather easy. Shell scripts prompting input are generally more user friendly too. In this article I’ll show you how to read input from the bash shell. Take the example below:

#!/bin/bash

echo "Shell Script To Get User Input";

while read inputline
do
what="$inputline"
echo $what;

if [ -z "${what}" ];
then
exit
fi

done

All it does is echo’s the introduction “Shell Script To Get User Input” and then goes right to the bash read input loop. The next line makes a variable so that we can echo it out and also check if its empty with the if [ -z part. If the script is empty we exit, if not we loop around another time.

This is a very basic example but it can easily be modified so that you can use bash to grab user input. If you have any trouble with this script drop me a comment and I’ll be happy to help you out.

For, While and Until Loops in Bash

Filed under: Shell Script Sundays — TheLinuxBlog.com at 3:45 pm on Sunday, August 12, 2007

Normally in a shell script it is desirable to do something repetitive.
I have touched on some loops in other posts but now would like to go over them in a bit more detail. Each of the examples in this post are intended to give an introduction to looping in bash.

For Loops
For loops allow you to repeat a section of code a number of times. Its very similar to other languages syntax but works a little differently. The for loop in bash only allows you to give a fixed list of values to loop over. A good way to remember how a for loop works is “For each of the dishes: clean and dry.”
For Syntax:

for i [in list]
do
statements [use $i]
done

For Example:

for x in 1 2 3
do
echo “Number: $x”
done

echo “Finished!”

This is a very simple script that just counts to 3 and then prints “Finished!”

While and until Loops
In essence while and until are the same in bash. The titles are pretty much self explanatory. A while loop would be explained in real life as “While the sink is still full: wash dishes” and a until loop would be “Until the sink is empty: Wash dishes.”
While and Until Syntax:

until/while [condition] do
statements
done

Example of a While loop:

count=1
while [ $count -lt 10 ]; do
echo $count
let count=$count+1
done
echo “Finished!”

Basically this loop will loop over the code while the count variable is less than 10. If we didn’t put the let statement in the script it would get stuck in the loop causing the user to press CTRL+C to end the script.

Doing the same thing can be done in a until loop except the condition has to be modified to get the same result.
Until example:

count=1
until [ $count -gt 9 ]; do
echo $count
let count=$count+1
done
echo “Finished!”

Now that you’ve figured out how to loop over something its probably a good idea to know how to stop the loop.
All that needs to be done to stop a loop is:

break

Break Example:

for x in 1 2 3 4 5
do
if [ $x = 3 ]; then
echo “Number is 3. Quitting”
break;
fi
echo “Number: $x”
done

This is a very easy to follow example. Its the same as the basic for loop except that if x is 3 it will stop the loop. This example has no real practical purpose. Since its a for loop the number 3 could just have been omitted.

Real World For Loop Example
Looping over all files in /etc and printing all of those that match “grep conf” and putting them in quotes.
The code to do this in a loop is:

for x in $(ls /etc -1|grep conf);
do
echo “$x”
done

The situation for many bash scripts is that there is normally a shorter way to do something. Take the Real World For Loop Example in this tutorial the same results can be achieved with:

x=$(ls /etc |grep conf); echo “$x”\n

This will get the job done but a loop may be better for esthetic purposes or for additional logic.

When Photoshop Fails

Filed under: General Linux,Shell Script Sundays — TheLinuxBlog.com at 9:50 pm on Monday, January 15, 2007

I was recently assigned the task of reducing the quality of over 1000 images. My first instinct was to use photoshop’s batch functions to complete the task. After playing around with it for a little while it became apparent that you cannot save files for web with spaces in. Photoshop had failed me. My next idea was to use a bash script to loop over every file and process the image. This was sure to work. Since I have prior experience with image processing I decided to use ImageMagick to complete the task. The command to adjust the quality is:

convert (FILENAME) -quality 50 (FILENAME)

This command is useless on its own so using a for loop I came up with:

#!/bin/bash
for i in $(ls); do
convert $i -quality 50 $i
done

This script would have worked for me if there were no spaces in the filenames. Because there were it took each filename that had spaces and ran the command on each part of the file name. Unfortunately this was not going to work.

After googling for a while it became apparent that I was not the only person to have the problem of spaces in filenames with for loops. The solution I found was to use the find command and a while loop. Below is working script that successfully completed the task in no time:

#!/bin/bash
find * -iname “*” | \
while read I; do
convert “$I” -quality 50 “$I”
done

This script could be easily modified to take advantage of ImageMagick’s many other functions. For example it could be used to batch resize a folder of images to make thumbnails by changing the command to

convert “$I” -resize 200 “$I”

Or it could be used to overlay text onto an image with the following command:

convert “$I” -gravity southeast -annotate +5+10 “thelinuxblog.com” -fill black “$I”

As shown above ImageMagick is really powerful image editing software which can easily be used with the bash shell to process thousands of images with no trouble.