這個 sed-縮網址程式何時會爆炸? - Linux
By Sandy
at 2020-11-04T06:52
at 2020-11-04T06:52
Table of Contents
#!/bin/sh
#
# 1604436674 created for testing in Linux/PTT
#
# 這是個 YouTube 縮網址的 one-liner. 必須很 portable.
# 我已測試過各種不同形狀的水管 url's 例如:
#
# /v/<VID>
# watch?v=<VID>
# embed/<VID>?rel=0
# watch?argv=xyz&v=<VID>
# watch?v=<VID>&list=PLDB852818BF378DAC
# watch?v=<VID>&feature=related
# watch?argv=xyz&v=<VID>
# watch?v=<VID>&feature=feedrec_grec_index
# user/IngridMichaelsonVEVO#p/a/u/1/<VID>
# v/<VID>?fs=1&hl=en_US&rel=0
# watch?v=<VID>#t=0m10s
# embed/<VID>?rel=0
# watch?v=<VID>
# http://youtu.be/<VID> (idempotent)
#
# 能不能幫忙看看還有哪些 url's 會出錯, 並幫忙想辦法?
#
# 我本來不喜歡縮網址的, 因為不知道有效期限多久...
# 但如果我沒誤解的話, youtu.be 是水管自家的,
# 而且保留了原始的影片 ID (確定都是11個字嗎?).
# 所以還可以接受.
#
# 解說:
#
# 0. 它必須儘可能 portable, 不管甚麼系統, 必須隨抄即用
# 誰有 Solaris, SunOS, OsX, Ultrix, AIX, ... 拜託!
# 我只是很好奇, 它能有多廣的 portability.
#
# 1. 請忽視與 termux 有關的東西, 那是讓手機也可以用的,
#
# 2. youtu() 就已經是個充份的 one-liner.
# 為了應付可能出現的雜七雜八的選項及形態
# 我決定擷取 \1. protocol 跟 \2. video_id
# 然後忽略掉其它可能出現的所有東西.
#
# 3. 為方便測試, 所以它要可以從 X-clipborad 讀取,
# 由 stdin 讀取, 也可以由指令行讀取.
#
# 4. 用了 sed(1) tr(1) grep(1) xsel(1) termux-clipboard-get(1)
#
# 5. 1604555294 新增, 原本的 -e 's/$/\n/' | tr -s '\n' 是為了確保
# 行尾起碼有一個 newline, 而且只有一個. 這也是為了使用上方便.
youtu()
{
# sed -e 's|^\(http.\?\):.*[/vd]\{0,1\}[0-9vd][/=]\([0-9a-zA-Z_-]\{11\}\).*$|\1://youtu.be/\2|' -e 's/$/\n/' | tr -s '\n'
# 哇-- 這行那麼長不知道會不會壞掉....
#
# 1604555294 更新, 上面那一行到 FreeBSD 就燒了, 先斷成兩行吧 (lantw44)
# sed 's|^\(http[s]\{0,1\}\):.*[/vd]\{0,1\}[0-9vd][/=]\([0-9a-zA-Z_-]\{11\}\).*$|\1://youtu.be/\2\
#|' | tr -s '\n'
# 1604671459 找了 awk 幫忙來確保 one & only one newline
sed 's|^\(http[s]\{0,1\}\):.*[/vd]\{0,1\}[0-9vd][/=]\([0-9a-zA-Z_-]\{11\}\).*$|\1://youtu.be/\2|' | awk 1
}
if [ -t 0 ] # priority: stdin > "$1" > X-clipboard
then
# echo "$HOME" | grep -q termux && XGET="termux-clipboard-get" || XGET="xsel"
# [ "$1" ] && echo "$1" | youtu || $XGET | youtu
# 1604570722 還是改一下吧, 以上兩行是錯的, A && B || C 不是 if-then-else
# (contributors: lantw44 rickieyang bitlife)
# if echo "$HOME" | grep -q termux
# then
# XGET="termux-clipboard-get"
# else
# XGET="xsel"
# fi
#
# if [ "$1" ]
# then
# echo "$1" | youtu
# else
# $XGET | youtu
# fi
# 1604671459 讓它在 Mac 上也會動 (rickieyang)
if [ "$1" ]
then
echo "$1" | youtu
else # termux > Mac > X11
XGET="xsel"
uname | grep -q "Darwin" && XGET="pbpaste"
echo "$HOME" | grep -q "termux" && XGET="termux-clipboard-get"
$XGET | youtu
fi
else
youtu
fi
--
#
# 1604436674 created for testing in Linux/PTT
#
# 這是個 YouTube 縮網址的 one-liner. 必須很 portable.
# 我已測試過各種不同形狀的水管 url's 例如:
#
# /v/<VID>
# watch?v=<VID>
# embed/<VID>?rel=0
# watch?argv=xyz&v=<VID>
# watch?v=<VID>&list=PLDB852818BF378DAC
# watch?v=<VID>&feature=related
# watch?argv=xyz&v=<VID>
# watch?v=<VID>&feature=feedrec_grec_index
# user/IngridMichaelsonVEVO#p/a/u/1/<VID>
# v/<VID>?fs=1&hl=en_US&rel=0
# watch?v=<VID>#t=0m10s
# embed/<VID>?rel=0
# watch?v=<VID>
# http://youtu.be/<VID> (idempotent)
#
# 能不能幫忙看看還有哪些 url's 會出錯, 並幫忙想辦法?
#
# 我本來不喜歡縮網址的, 因為不知道有效期限多久...
# 但如果我沒誤解的話, youtu.be 是水管自家的,
# 而且保留了原始的影片 ID (確定都是11個字嗎?).
# 所以還可以接受.
#
# 解說:
#
# 0. 它必須儘可能 portable, 不管甚麼系統, 必須隨抄即用
# 誰有 Solaris, SunOS, OsX, Ultrix, AIX, ... 拜託!
# 我只是很好奇, 它能有多廣的 portability.
#
# 1. 請忽視與 termux 有關的東西, 那是讓手機也可以用的,
#
# 2. youtu() 就已經是個充份的 one-liner.
# 為了應付可能出現的雜七雜八的選項及形態
# 我決定擷取 \1. protocol 跟 \2. video_id
# 然後忽略掉其它可能出現的所有東西.
#
# 3. 為方便測試, 所以它要可以從 X-clipborad 讀取,
# 由 stdin 讀取, 也可以由指令行讀取.
#
# 4. 用了 sed(1) tr(1) grep(1) xsel(1) termux-clipboard-get(1)
#
# 5. 1604555294 新增, 原本的 -e 's/$/\n/' | tr -s '\n' 是為了確保
# 行尾起碼有一個 newline, 而且只有一個. 這也是為了使用上方便.
youtu()
{
# sed -e 's|^\(http.\?\):.*[/vd]\{0,1\}[0-9vd][/=]\([0-9a-zA-Z_-]\{11\}\).*$|\1://youtu.be/\2|' -e 's/$/\n/' | tr -s '\n'
# 哇-- 這行那麼長不知道會不會壞掉....
#
# 1604555294 更新, 上面那一行到 FreeBSD 就燒了, 先斷成兩行吧 (lantw44)
# sed 's|^\(http[s]\{0,1\}\):.*[/vd]\{0,1\}[0-9vd][/=]\([0-9a-zA-Z_-]\{11\}\).*$|\1://youtu.be/\2\
#|' | tr -s '\n'
# 1604671459 找了 awk 幫忙來確保 one & only one newline
sed 's|^\(http[s]\{0,1\}\):.*[/vd]\{0,1\}[0-9vd][/=]\([0-9a-zA-Z_-]\{11\}\).*$|\1://youtu.be/\2|' | awk 1
}
if [ -t 0 ] # priority: stdin > "$1" > X-clipboard
then
# echo "$HOME" | grep -q termux && XGET="termux-clipboard-get" || XGET="xsel"
# [ "$1" ] && echo "$1" | youtu || $XGET | youtu
# 1604570722 還是改一下吧, 以上兩行是錯的, A && B || C 不是 if-then-else
# (contributors: lantw44 rickieyang bitlife)
# if echo "$HOME" | grep -q termux
# then
# XGET="termux-clipboard-get"
# else
# XGET="xsel"
# fi
#
# if [ "$1" ]
# then
# echo "$1" | youtu
# else
# $XGET | youtu
# fi
# 1604671459 讓它在 Mac 上也會動 (rickieyang)
if [ "$1" ]
then
echo "$1" | youtu
else # termux > Mac > X11
XGET="xsel"
uname | grep -q "Darwin" && XGET="pbpaste"
echo "$HOME" | grep -q "termux" && XGET="termux-clipboard-get"
$XGET | youtu
fi
else
youtu
fi
--
Tags:
Linux
All Comments
By Eartha
at 2020-11-05T19:26
at 2020-11-05T19:26
By Rosalind
at 2020-11-07T08:01
at 2020-11-07T08:01
By Una
at 2020-11-08T20:36
at 2020-11-08T20:36
By Donna
at 2020-11-10T09:10
at 2020-11-10T09:10
By Eartha
at 2020-11-11T21:45
at 2020-11-11T21:45
By Annie
at 2020-11-13T10:20
at 2020-11-13T10:20
By Elma
at 2020-11-14T22:54
at 2020-11-14T22:54
By Yuri
at 2020-11-16T11:29
at 2020-11-16T11:29
By Mason
at 2020-11-18T00:04
at 2020-11-18T00:04
By Blanche
at 2020-11-19T12:38
at 2020-11-19T12:38
By Todd Johnson
at 2020-11-21T01:13
at 2020-11-21T01:13
By Valerie
at 2020-11-22T13:48
at 2020-11-22T13:48
By Dora
at 2020-11-24T02:22
at 2020-11-24T02:22
By Sandy
at 2020-11-25T14:57
at 2020-11-25T14:57
By Oliver
at 2020-11-27T03:31
at 2020-11-27T03:31
By Lydia
at 2020-11-28T16:06
at 2020-11-28T16:06
By Dorothy
at 2020-11-30T04:41
at 2020-11-30T04:41
By Jacob
at 2020-12-01T17:15
at 2020-12-01T17:15
By Anthony
at 2020-12-03T05:50
at 2020-12-03T05:50
By Irma
at 2020-12-04T18:25
at 2020-12-04T18:25
By Belly
at 2020-12-06T06:59
at 2020-12-06T06:59
By Olga
at 2020-12-07T19:34
at 2020-12-07T19:34
By Kelly
at 2020-12-09T08:09
at 2020-12-09T08:09
By Susan
at 2020-12-10T20:43
at 2020-12-10T20:43
By Madame
at 2020-12-12T09:18
at 2020-12-12T09:18
By Megan
at 2020-12-13T21:53
at 2020-12-13T21:53
By Joe
at 2020-12-15T10:27
at 2020-12-15T10:27
By Una
at 2020-12-16T23:02
at 2020-12-16T23:02
By Olivia
at 2020-12-18T11:37
at 2020-12-18T11:37
By Edwina
at 2020-12-20T00:11
at 2020-12-20T00:11
By Elma
at 2020-12-21T12:46
at 2020-12-21T12:46
By Daniel
at 2020-12-23T01:21
at 2020-12-23T01:21
By George
at 2020-12-24T13:55
at 2020-12-24T13:55
By Bethany
at 2020-12-26T02:30
at 2020-12-26T02:30
By Una
at 2020-12-27T15:04
at 2020-12-27T15:04
By John
at 2020-12-29T03:39
at 2020-12-29T03:39
By Daph Bay
at 2020-12-30T16:14
at 2020-12-30T16:14
By Ina
at 2021-01-01T04:48
at 2021-01-01T04:48
By Candice
at 2021-01-02T17:23
at 2021-01-02T17:23
By Barb Cronin
at 2021-01-04T05:58
at 2021-01-04T05:58
By Harry
at 2021-01-05T18:32
at 2021-01-05T18:32
By Barb Cronin
at 2021-01-07T07:07
at 2021-01-07T07:07
Related Posts
求推薦Linux gcc c語言的指令的書
By Caitlin
at 2020-11-02T23:46
at 2020-11-02T23:46
apt-get 載入libraries出錯
By Noah
at 2020-11-02T22:54
at 2020-11-02T22:54
Top指令內的Mem in_d是什麼意思?
By Candice
at 2020-11-02T16:43
at 2020-11-02T16:43
kernel 5.9 with nvidia driver
By Ingrid
at 2020-10-31T07:13
at 2020-10-31T07:13
Ubuntu無法開機了
By Carol
at 2020-10-28T20:59
at 2020-10-28T20:59