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.

Reattach Screen Script

Filed under: Shell Script Sundays — TheLinuxBlog.com at 2:02 pm on Sunday, April 12, 2009

A friend of mine who happens to be an avid screen user sent me this snippet below:

### Reattach to a screen if one exists ###
if [[ $TERM != 'screen' ]] ; then
if [[ `screen -list | grep -v "No" | awk '$2 { print }' | wc -l` == 0 ]] ; then
screen
else
screen -dr
fi
fi

What this handy snippet does is looks for a screen session, if it finds one it detaches the running screen, and reattaches it(-dr) if it isn’t lucky enough to find one, then it just starts a session up for you. Its rather handy to put in your .bashrc file to auto launch a screen session. The only thing I have modified for my use is replacing -dr for -x to enable me to reattach the screen without detaching the session I may have had open on another terminal. It works pretty well, although when you open a new “screen” CTRL-a + c, the tab doesn’t show up on the other sessions until you change to it, or cycle through them. It isn’t a big deal and could even be a local configuration issue. Anyway, enjoy this snippet and as always let me know if you found it useful.

APC Access Temperature Query and Conversion. (2 of 2)

Filed under: Shell Script Sundays — TheLinuxBlog.com at 6:55 pm on Sunday, March 29, 2009

This second part of the script APC Access Temperature Query Script and its been a long time coming. Basically this script is the part that runs as a cron and will e-mail me if the temperature goes over a certain threshold. Once it returns to normal it e-mails me again. It has the option to send a text message to me via my SMS gateway, but it is commented out.

#!/bin/bash

temp=$(/home/linux/bin/temp f)
threshold=76

if [ "`echo \"$temp > $threshold\" | bc`" == 1 ]; then
echo $(date +%s) $temp >> /home/linux/thermal-over.log
echo “High Temp”;

if [ "$(cat temp.txt)" == "norm" ]; then
echo “Sending E-Mail, High Temp”;
echo “Current Temperature Is: $(/home/linux/bin/temp f)” | mail -s “Thermal Overload” owen@linuxblog                #echo “Current Temp Is: $(/home/linux/bin/temp f)” | mail -s “Thermal Overload” mynumber@cingularme.com
echo “high” > temp.txt
fi
elif [ "`echo \"$temp < $threshold\" | bc`" == 1 ]; then
echo “Low Temp”;

if [ "$(cat temp.txt)" == "high" ]; then
echo “Temp Resumed, Sending E-Mail”;
echo $(date +%s) Resumed at: $temp | mail -s “Thermal Normal” owen@linuxblog
echo “norm” > temp.txt
fi

fi

echo $(date +%s) $temp >> /home/linux/thermal.log

When I first wrote the script, I did not do any temperature checking. I found out that I needed to when I came back one morning with a bunch of emails that I needed to delete. Its pretty simple to figure out, temp.txt holds a value that is either norm or high. It gets switched when the temperature changes, this will in turn stop it from e-mailing me repeatedly. Once the temperature drops it flips it back. It will still e-mail if your temperature fluctuates between 75 and 77 which can be annoying, but you can adjust the threshold with the variable and set it to what you need. Thankfully our chiller has been fixed and I no longer have to worry about the temperature, but it still runs on a cron just in case.