帮忙改一下脚本 :)

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
iblicf
帖子: 3766
注册时间: 2007-01-15 17:15

帮忙改一下脚本 :)

#1

帖子 iblicf » 2011-08-03 0:09

代码: 全选

#!/bin/bash

function usage {
        echo "Usage: $(basename $0) <filename>
Upload an image to xxxxx.me and output its URL to stdout.
If xsel or xclip is available, the URL is put on the X selection for easy pasting." >&2
}

if [ $# != 1 ]; then
        echo "No file specified" >&2
        exit 1
elif [ ! -f "$1" ]; then
        echo "File \"$1\" not found" >&2
        exit 1
fi

# check for curl
which curl >/dev/null 2>/dev/null || {
        echo "Couln't find curl, which is required." >&2
        exit 1
}

# upload the image
response=$(curl -F "uploadfile=@$1" "http://xxxxx.me/index.php?type=uploadv3&key=QT5LGz7ktGFVZpfFArVHCpEvDcC3qrUZrf0kP" 2>/dev/null)
if [ $? != 0 ]; then
        echo "Upload failed" >&2
        exit 2
fi

# parse the response and output our stuff
status=$(echo $response | sed 's/[\"\{\}\[\]]*//g' | cut -d, -f1 | cut -d: -f2)
if [ $status != 1 ]; then
        echo "Some sort of error occurred" >&2
        exit 1
fi
img=$(echo $response | sed 's/[\"\{\}\[\]]*//g' | cut -d, -f2 | cut -d: -f2)
ext=$(echo $response | sed 's/[\"\{\}\[\]]*//g' | cut -d, -f3 | cut -d: -f2)

file="$img.$ext"
url="http://xxxx.me/$file"

echo $url


这个是给图床传图的,返回一个图片连接地址,目前只支持单张传 ~~ 希望能支持多张,比如 img.sh *.png

或者给一个参考类似的 shell 脚本 ~~ 多谢
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 帮忙改一下脚本 :)

#2

帖子 eexpress » 2011-08-03 10:53

curl提交啊。curl不熟悉。
perl的 WWW::Mechanize 的。要不。
● 鸣学
头像
link_01
帖子: 1024
注册时间: 2008-11-05 13:24

Re: 帮忙改一下脚本 :)

#3

帖子 link_01 » 2011-08-03 12:01

[bash]
#!/bin/bash

usage() {
echo "Usage: $0 <filename>
Upload an image to xxxxx.me and output its URL to stdout.
If xsel or xclip is available, the URL is put on the X selection for easy pasting."
}

# check for curl
which curl >/dev/null 2>&1 || {
echo "Couln't find curl, which is required."
exit 1
}

upload() {
# upload the image
response=$(curl -F "uploadfile=@$1" "http://xxxxx.me/index.php?type=uploadv3 ... 3qrUZrf0kP" 2>/dev/null)
if [ $? -ne 0 ]; then
echo "Upload failed"
exit 1
fi

# parse the response and output our stuff
status=$(echo $response | sed 's/[\"\{\}\[\]]*//g' | cut -d, -f1 | cut -d: -f2)
if [ $status != 1 ]; then
echo "Some sort of error occurred"
exit 1
fi
img=$(echo $response | sed 's/[\"\{\}\[\]]*//g' | cut -d, -f2 | cut -d: -f2)
ext=$(echo $response | sed 's/[\"\{\}\[\]]*//g' | cut -d, -f3 | cut -d: -f2)

file="$img.$ext"
url="http://xxxx.me/$file"

echo $url
}

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

for i in $@; do
if [ -f $i ]; then
upload $i
else
echo "file $i is not found!"
fi
done
[/bash]
原来能用的话,扩展下参数,你自己再改改吧。
笔记
-------------------------------------
http://blog.163.com/wqt_1101
回复