You are not logged in.
So let me show you my problem straightaway:
If I write the script this way:
#!/bin/bash
site='http://omploader.org/'
upload () {
images=($(curl -# -F "file1=@${1};filename=test" "${site}upload" | \
sed '/.*\[url=\(.*\)\]\[img\]\(.*\)\[\/img\]\[\/url\].*/!d;s//\1 \2/'))
image=${images[0]}
thumb=${images[1]}
}
upload "$1" #note here
echo $image
echo $thumb
And I run it:
$ convert image.png -resize '250x' - | ./test -
http://omploader.org/vMmt6ZQ
http://omploader.org/tMmt6ZQ
If I write the script this way:
#!/bin/bash
site='http://omploader.org/'
upload () {
images=($(curl -# -F "file1=@${1};filename=test" "${site}upload" | \
sed '/.*\[url=\(.*\)\]\[img\]\(.*\)\[\/img\]\[\/url\].*/!d;s//\1 \2/'))
image=${images[0]}
thumb=${images[1]}
}
convert "$1" -resize "250x" - | upload - #note here
echo $image
echo $thumb
The result is not as expected:
$ ./test image.png
Weird, isn't it?
What am I missing here?
Last edited by lolilolicon (2009-10-22 15:12:36)
This silver ladybug at line 28...
Offline
Does this work? (I don't know why yours fails though..)
#!/bin/bash
site='http://omploader.org/'
upload () {
images=($(curl -# -F "file1=@${1};filename=test" "${site}upload" | \
sed '/.*\[url=\(.*\)\]\[img\]\(.*\)\[\/img\]\[\/url\].*/!d;s//\1 \2/'))
image=${images[0]}
thumb=${images[1]}
}
upload $(convert "$1" -resize "250x" -) #note here
echo $image
echo $thumb
Last edited by Ramses de Norre (2009-10-20 11:27:51)
Offline
Nope, with that you get:
convert: option requires an argument `-resize' @ convert.c/ConvertImageCommand/2191.
The problem is a very unexpected behavior to me, really.
This silver ladybug at line 28...
Offline
Nope, with that you get:
convert: option requires an argument `-resize' @ convert.c/ConvertImageCommand/2191.
The problem is a very unexpected behavior to me, really.
I missed a dash.. Corrected.
Offline
Nope, it does not work either (according to prophecy ). I actually tried it and it did not work.
This silver ladybug at line 28...
Offline
Actually the whole script is broken for me.... Even the first one!
Last edited by fumbles (2009-10-21 12:16:24)
Offline
That does not work, my friend.
Actually, if "$(blah)" returns a name or something(like a dash indicating stdin), it might work. but as i understand it, "$(convert $1 -resize 250x -)" is sending binary-data itself out, which, to me, seems bound to fail...
I'm more of "why my code did not work?", rather than "just give me a working script"... since it's very odd to me... something i've never encountered before.
Anyway, thanks for the input guys. Let me wait for the answer a little longer.
This silver ladybug at line 28...
Offline
@fumbles
that's strange... the first script works fine for me.
bash test.sh image.png
######################################################################## 100.0%
http://omploader.org/vMmwwMA
http://omploader.org/tMmwwMA
convert image.png -resize '250x' - | bash test.sh -
######################################################################## 100.0%
http://omploader.org/vMmt6ZQ
http://omploader.org/tMmt6ZQ
Last edited by lolilolicon (2009-10-21 12:37:40)
This silver ladybug at line 28...
Offline
Works ok here. Problem with the bash shell perhaps? I am having a problem with the script not printing:
echo $image
echo $thumb
Odd, I don't see any problem in it.
Setting Up a Scripting Environment | Proud donor to wikipedia - link
Offline
Gen2ly, do you mean the first script run and print ok but the second does not print? I guess you got the same result as mine.
I tested a little more, it's so different from my experience! Look, now I add several echo's inside the upload () function:
#!/bin/bash
site='http://omploader.org/'
upload () {
images=($(curl -# -F "file1=@${1};filename=test" "${site}upload" | \
sed '/.*\[url=\(.*\)\]\[img\]\(.*\)\[\/img\]\[\/url\].*/!d;s//\1 \2/'))
image=${images[0]}
thumb=${images[1]}
echo "+ $image"
echo "+ ${images[1]}"
}
convert "$1" -resize 250x - | upload - # this not fully working
#upload "$1" # this works
echo "- $image"
echo "- $thumb"
echo "- ${images[@]}"
Output here,
./test image.png
+ http://omploader.org/vMmt6ZQ
+ http://omploader.org/tMmt6ZQ
-
-
-
As you already see, the echo's inside the upload () function does work but the echo's after the upload () is run does not print as expected.
Why does the value assignment seem broken here? They seem to be "local" while I haven't declared so.
Last edited by lolilolicon (2009-10-22 03:13:44)
This silver ladybug at line 28...
Offline
That's good debugging. So it's a subshell issue. Try this:
upload - <<< "$(convert "$1" -resize 250x -)"
Offline
Procyon, you definitely are the coolest guy I ever know!
Can you think of a possible explanation why my code failed?
Edit: All seemed well until i went to see what had been uploaded: http://omploader.org/vMmxndg http://omploader.org/tMmxndg
Last edited by lolilolicon (2009-10-22 14:04:39)
This silver ladybug at line 28...
Offline
The variables were lost when the subshell terminated. Just like
echo test | read
echo $REPLY
And you're right, there are two problems with <<<, it seems to be really just for strings: there is a newline appended and all NULL bytes are removed (jpegs have a lot of them).
Offline
Gen2ly, do you mean the first script run and print ok but the second does not print? I guess you got the same result as mine.
I tested a little more, it's so different from my experience! Look, now I add several echo's inside the upload () function:
#!/bin/bash site='http://omploader.org/' upload () { images=($(curl -# -F "file1=@${1};filename=test" "${site}upload" | \ sed '/.*\[url=\(.*\)\]\[img\]\(.*\)\[\/img\]\[\/url\].*/!d;s//\1 \2/')) image=${images[0]} thumb=${images[1]} echo "+ $image" echo "+ ${images[1]}" } convert "$1" -resize 250x - | upload - # this not fully working #upload "$1" # this works echo "- $image" echo "- $thumb" echo "- ${images[@]}"
Output here,
./test image.png + http://omploader.org/vMmt6ZQ + http://omploader.org/tMmt6ZQ - - -
As you already see, the echo's inside the upload () function does work but the echo's after the upload () is run does not print as expected.
Why does the value assignment seem broken here? They seem to be "local" while I haven't declared so.
Actually everything happens as expected. When you pipe to the upload function, it forks another shell, in which the function is executed. The variables are thus not in the global namespace of the "outer" shell, but in the forked one.
Offline
Ah!! Thank you both so much, Procyon and Xilon. I didn't think of pipe as subshell...
I now think it's not a good idea to assign variables inside a function, which I'll avoid wherever possible from now on.
Thanks guys
Edit:
Oh, and look I've come up with a work-around too
#!/bin/bash
site='http://omploader.org/'
upload () {
images=($(curl -# -F "file1=@${1};filename=test" "${site}upload" | \
sed '/.*\[url=\(.*\)\]\[img\]\(.*\)\[\/img\]\[\/url\].*/!d;s//\1 \2/'))
image=${images[0]}
thumb=${images[1]}
}
convert "$1" -resize "250x" - | (upload - ; echo $image ; echo $thumb)
./test image.png
http://omploader.org/vMmt6ZQ
http://omploader.org/tMmt6ZQ
Just like:
echo test | (read ; echo $REPLY)
test
Last edited by lolilolicon (2009-10-22 15:25:06)
This silver ladybug at line 28...
Offline
Well done lolilolicon. Nice script. I too just learned about the subshell, hadn't thought of that before.
Setting Up a Scripting Environment | Proud donor to wikipedia - link
Offline