You are not logged in.

#1 2009-10-20 11:06:11

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

[Solved]Got problem with bash pipe

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

#2 2009-10-20 11:17:32

Ramses de Norre
Member
From: Leuven - Belgium
Registered: 2007-03-27
Posts: 1,289

Re: [Solved]Got problem with bash pipe

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

#3 2009-10-20 11:26:06

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [Solved]Got problem with bash pipe

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

#4 2009-10-20 11:28:11

Ramses de Norre
Member
From: Leuven - Belgium
Registered: 2007-03-27
Posts: 1,289

Re: [Solved]Got problem with bash pipe

lolilolicon wrote:

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

#5 2009-10-20 11:34:41

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [Solved]Got problem with bash pipe

Nope, it does not work either (according to prophecy tongue). I actually tried it and it did not work.


This silver ladybug at line 28...

Offline

#6 2009-10-21 12:03:45

fumbles
Member
Registered: 2006-12-22
Posts: 246

Re: [Solved]Got problem with bash pipe

Actually the whole script is broken for me.... Even the first one!

Last edited by fumbles (2009-10-21 12:16:24)

Offline

#7 2009-10-21 12:33:59

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [Solved]Got problem with bash pipe

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

#8 2009-10-21 12:35:47

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [Solved]Got problem with bash pipe

@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

#9 2009-10-21 18:16:35

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: [Solved]Got problem with bash pipe

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

#10 2009-10-22 03:05:24

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [Solved]Got problem with bash pipe

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

#11 2009-10-22 13:41:16

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [Solved]Got problem with bash pipe

That's good debugging. So it's a subshell issue. Try this:
upload - <<< "$(convert "$1" -resize 250x -)"

Offline

#12 2009-10-22 13:54:05

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [Solved]Got problem with bash pipe

Procyon, you definitely are the coolest guy I ever know! smile

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
yikes

Last edited by lolilolicon (2009-10-22 14:04:39)


This silver ladybug at line 28...

Offline

#13 2009-10-22 14:46:05

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [Solved]Got problem with bash pipe

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

#14 2009-10-22 14:47:56

Xilon
Member
Registered: 2007-01-01
Posts: 243

Re: [Solved]Got problem with bash pipe

lolilolicon wrote:

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

#15 2009-10-22 15:10:52

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [Solved]Got problem with bash pipe

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 wink

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

big_smile

Last edited by lolilolicon (2009-10-22 15:25:06)


This silver ladybug at line 28...

Offline

#16 2009-10-22 22:51:28

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: [Solved]Got problem with bash pipe

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

Board footer

Powered by FluxBB