You are not logged in.

#1 2012-05-12 19:41:09

debdj
Member
Registered: 2012-01-19
Posts: 163

[solved] whats going wrong with this script

I'm tweaking the image upload script from imgur.com
here's the moded script:

 1	#!/bin/bash
     2	
     3	usage () {
     4	   cat <<- EOF
     5	   Usage:
     6	   $0 <filename> to upload a file to imgur
     7	   without any arguments the script takes a shot and uploads it.
     8	   -t : for creating a thumbnail and uploading it as well.
     9	   -h : for this help text.
    10	EOF
    11	
    12	if [ $# -gt 1 ]; then usage; exit 1 #
    13	
    14	thumb=""
    15	
    16	if [ -z "$1" ]; then
    17	 take_shot
    18	else
    19	case "$1" in
    20	 -t)
    21	   thumb=' -t 20'
    22	   take_shot
    23	   ;;
    24	 -h)
    25	   usage
    26	   ;;
    27	 *)
    28	  if [ -e "$1" ]; then
    29	  filename="$1" && shotdir=`echo "${1%/*}"` && upload_shot
    30	  else
    31	  echo -e "File not found / unxpected argument!" && usage && exit 10
    32	  fi
    33	  ;;
    34	esac
    35	fi
    36	
    37	
    38	take_shot () {
    39	
    40	  shotdir="."
    41	  filename=`date "+%F--%R"-${RANDOM}`
    42	  [ ! -d ~/shots ] && { mkdir -p ~/shots && shotdir='~/shots'; }
    43	  echo "Taking shot..."
    44	  scrot -cd 5${thumb} ${shotdir}/${fiename}.png
    45	  echo -e "Shot saved as ${shotdir}/${fiename}.png"
    46	  upload_shot
    47	}
    48	
    49	
    50	# upload the image
    51	
    52	upload_shot () {
    53	
    54	apikey="b3625162d3418ac51a9ee805b1840452"
    55	
    56	cd "$shotdir"
    57	
    58	for j in `ls $filename}.png ${filename}-thumb.png 2>/dev/null`; do
    59	response=$(curl -F "key=$apikey" -H "Expect: " -F "image=@$j" 'http://imgur.com/api/upload.xml' 2>/dev/null)
    60	# the "Expect: " header is to get around a problem when using this through the
    61	# Squid proxy. Not sure if it's a Squid bug or what.
    62	if [ $? -ne 0 ]; then
    63	   echo "Upload failed" >&2
    64	      exit 2
    65	elif [ $(echo $response | grep -c "<error_msg>") -gt 0 ]; then
    66	   echo "Error message from imgur:" >&2
    67	   echo $response | sed -r 's/.*<error_msg>(.*)<\/error_msg>.*/\1/' >&2
    68	     exit 3
    69	fi
    70	
    71	  # parse the response and output our stuff
    72	url=$(echo $response | sed -r 's/.*<original_image>(.*)<\/original_image>.*/\1/')
    73	deleteurl=$(echo $response | sed -r 's/.*<delete_page>(.*)<\/delete_page>.*/\1/')
    74	echo $url
    75	echo "Delete page: $deleteurl" >&2
    76	
    77	done
    78	
    79	exit 0
    80	}

the error bash's giving is " imgur_me.sh: line 81: syntax error: unexpected end of file"

I guess somethings wrong with the upload_image func but cant really figure out.

Last edited by debdj (2012-05-13 15:29:54)

Offline

#2 2012-05-12 19:54:30

Glennzo
Member
From: Salem, Mass USA
Registered: 2009-09-24
Posts: 13

Re: [solved] whats going wrong with this script

Line 3 has the opening brace {  but there is no closing brace ???


Glenn
Powered by Arch Linux

Offline

#3 2012-05-12 22:23:20

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [solved] whats going wrong with this script

Line 12:

if [ $# -gt 1 ]; then usage; exit 1 #

you forgot the closing 'fi'.

Line 58:

for j in `ls $filename}.png ${filename}-thumb.png 2>/dev/null`; do

you missed the opening curly brace in the first '${filename}'.


You have to define functions first and then call them, otherwise

line 18: take_shot: command not found

happens.

Offline

#4 2012-05-13 07:49:20

debdj
Member
Registered: 2012-01-19
Posts: 163

Re: [solved] whats going wrong with this script

EDIT: Its working. finally.

Thanks you all.
There are a lot of mistakes you pointed out and more are there.

#!/bin/bash

usage () {
   cat <<- EOF
   Usage:
   $0 <filename> to upload a file to imgur
   without any arguments the script takes a shot and uploads it.
   -t : for creating a thumbnail and uploading it as well.
   -h : for this help text.
EOF
}

if [ $# -gt 1 ]; then usage; exit 1; fi #


# upload the image

upload_shot () {

apikey="b3625162d3418ac51a9ee805b1840452"

cd "$shotdir"

for j in `ls ${filename}.png ${filename}-thumb.png 2>/dev/null`; do
response=$(curl -F "key=$apikey" -H "Expect: " -F "image=@$j" http://imgur.com/api/upload.xml 2>/dev/null)
# the "Expect: " header is to get around a problem when using this through the
# Squid proxy. Not sure if it's a Squid bug or what.
if [ $? -ne 0 ]; then
        echo "Upload failed" >&2
        exit 2
elif [ $(echo $response | grep -c "<error_msg>") -gt 0 ]; then
        echo "Error message from imgur:" >&2
        echo $response | sed -r 's/.*<error_msg>(.*)<\/error_msg>.*/\1/' >&2
        exit 3
fi

# parse the response and output our stuff
url=$(echo $response | sed -r 's/.*<original_image>(.*)<\/original_image>.*/\1/')
deleteurl=$(echo $response | sed -r 's/.*<delete_page>(.*)<\/delete_page>.*/\1/')
echo $url
echo "Delete page: $deleteurl" >&2
done

exit 0
}


take_shot () {

  shotdir="."
  filename=`date "+%F"_${RANDOM}`
  [ ! -d ~/shots ] && { mkdir -p ~/shots && shotdir='~/shots'; } || shotdir='~/shots'
  echo "Taking shot..."
  scrot -cd 3${thumb} "${shotdir}/${filename}.png"
  [ $? -eq 0 ] && echo "Shot saved as ${shotdir}/${filename}.png" || { echo "Failed" >&2; exit 20; }
  upload_shot
}


thumb=""

if [ -z "$1" ]; then
 take_shot
else
case "$1" in
 -t)
   thumb=' -t 20'
   take_shot
   ;;
 -h)
   usage
   ;;
 *)
  if [ -e "$1" ]; then
  filename=$(basename "$1")
  a=$(realpath "$1") && shotdir="${a%/*}" && upload_shot  #theere's a problem with realpath. doesnt resolve ~ in quotes.
  else
  echo -e "   File not found / unxpected argument!" >&2 && usage && exit 10
  fi
  ;;
esac
fi

currently, nothing happens after upload_shot is called..at least there's nothing on the screen.
and there's more

$ ./scripts/imgur_me.sh 
+ '[' 0 -gt 1 ']'
+ thumb=
+ '[' -z '' ']'
+ take_shot
+ shotdir=.
++ date +%F_14526
+ filename=2012-05-13_14526
+ '[' '!' -d /home/users/debd//shots ']'
+ shotdir='~/shots'
+ echo 'Taking shot...'
Taking shot...
+ scrot -cd 3 '~/shots/2012-05-13_14526.png'
Taking shot in 3.. 2.. 1.. 0.
giblib error: Saving to file ~/shots/2012-05-13_14526.png failed

+ '[' 2 -eq 0 ']'
+ echo Failed
Failed
+ exit 20

What wrong am I doing? (its not a problem with overwriting)
Probably the approach itself is wrong. I'll rewrite. neutral

EDIT: okey, only uploading is working. But still stuck at scrot.
Finally, things are working. I needed to remove any quotes around ~/shots. even double quotes doesn't let bash expand it (?)

Last edited by debdj (2012-05-13 08:25:14)

Offline

#5 2012-05-13 08:28:55

debdj
Member
Registered: 2012-01-19
Posts: 163

Re: [solved] whats going wrong with this script

#!/bin/bash

usage () {
   cat <<- EOF
   Usage:
   $0 <filename> to upload a file to imgur
   without any arguments the script takes a shot and uploads it.
   -t : for creating a thumbnail and uploading it as well.
   -h : for this help text.
EOF
}

if [ $# -gt 1 ]; then usage; exit 1; fi #


# upload the image

upload_shot () {

apikey="b3625162d3418ac51a9ee805b1840452"

cd "$shotdir"

for j in `ls ${filename}.png ${filename}-thumb.png 2>/dev/null`; do
response=$(curl -F "key=$apikey" -H "Expect: " -F "image=@$j" http://imgur.com/api/upload.xml 2>/dev/null)
# the "Expect: " header is to get around a problem when using this through the
# Squid proxy. Not sure if it's a Squid bug or what.
if [ $? -ne 0 ]; then
        echo "Upload failed" >&2
        exit 2
elif [ $(echo $response | grep -c "<error_msg>") -gt 0 ]; then
        echo "Error message from imgur:" >&2
        echo $response | sed -r 's/.*<error_msg>(.*)<\/error_msg>.*/\1/' >&2
        exit 3
fi

# parse the response and output our stuff
url=$(echo $response | sed -r 's/.*<original_image>(.*)<\/original_image>.*/\1/')
deleteurl=$(echo $response | sed -r 's/.*<delete_page>(.*)<\/delete_page>.*/\1/')
echo -e "$j $url \n"
echo -e "Delete page for $j: $deleteurl" >&2
done

exit 0
}


take_shot () {

  shotdir="."
  filename=`date "+%F"_${RANDOM}`
  [ ! -d ~/shots ] && { mkdir -p ~/shots && shotdir=~/shots; } || shotdir=~/shots
  echo "Taking shot..."
  scrot -cd 3${thumb} ${shotdir}/${filename}.png
  [ $? -eq 0 ] && echo "Shot saved as ${shotdir}/${filename}.png" || { echo "Failed" >&2; exit 20; }
  upload_shot
}


thumb=""

if [ -z "$1" ]; then
 take_shot
else
case "$1" in
 -t)
   thumb=' -t 20'
   take_shot
   ;;
 -h)
   usage
   ;;
 *)
  if [ -e "$1" ]; then
  b=$(basename "$1") && filename="${b%.*}"
  a=$(realpath "$1") && shotdir="${a%/*}" && upload_shot  #theere's a problem with realpath. doesnt resolve ~ in quotes.
  else
  echo -e "   File not found / unxpected argument!" >&2 && usage && exit 10
  fi
  ;;
esac
fi

Offline

Board footer

Powered by FluxBB