λ在Chito的第二個CSS設置[原主題:SimpleModern]
Linux下递归列出仅所有常规文件或文件夹路径的方法

〔Bash〕pastebincl快捷提交脚本

λ posted @ 2012年3月20日 03:32 in Mixture with tags exercise tutorial , 4848 阅读

pastebincl」是Pastebin.com提供的一個在Unix命令行下上傳代碼的工具,不過剛用就感覺很不方便了,自己寫了個bash腳本,順便給不會用getopts1的同學一個參考的機會。

#!/bin/bash

## 使用方法:保存爲~/.local/bin/pcl,然後 pcl [options] [file]
## 注意:默認不公開上傳,且只提供-n和-s的選項。

OPTERR=0 #減少錯誤提示信息

# getopts 的用法:getopts OPTSTRING VARNAME [ARGS...]
# OPTSTRING和VARNAME看下面的代碼,ARGS爲要處理的參數的數組,默認爲$@
# ↓最前面的冒號表示減少錯誤提示信息(安靜模式),字母後的冒號表示該選項需要參數
while getopts :n:s: opt; do
  case $opt in
    n)
      NAME=$OPTARG #-n後的參數
      ;;
    s)
      SYNTAX=$OPTARG
      ;;
    \?) #遇到無效選項時,opt會被置爲'?'
      echo "Invalid option: -$OPTARG" >&2 #安靜模式下,遇到錯誤選項時OPTARG爲錯誤的選項
      exit 1
      ;;
    :) #類上,當找不到參數時opt爲":"
      echo "Option -$OPTARG requires an argument." >&2 #同樣是安靜模式下。不推薦詳細模式。
      exit 1
      ;;
  esac
done

shift $((OPTIND-1)) #這行是用來清除已經被處理過的參數的。
FILEPATH=$*
cat "$FILEPATH" | pastebincl -p ${NAME:+-n "$NAME"} ${SYNTAX:+-s "$SYNTAX"}

好了,這樣子就和原來的pastebincl沒什麼分別了,cat默認從stdin接收輸入真是恰到好處了。喪失重定向讀取功能……

爲了重獲重定向功能知道怎麼做了,如下爲修改後的腳本:

#!/bin/bash

## USAGE: pcl [options] [file]
## NOTICE: Always in private mode

OPTERR=0
REDIR="off"
while getopts :f:n:s:p opt; do
  case $opt in
    n)
      NAME=$OPTARG
      ;;
    s)
      SYNTAX=$OPTARG
      ;;
    p)
      REDIR=""
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

shift $((OPTIND-1))
FILEPATH=$*
cat ${REDIR:+"$FILEPATH"} | pastebincl -p ${NAME:+-n "$NAME"} ${SYNTAX:+-s "$SYNTAX"}

關於最後一行代碼2getopts,可看文章腳註。歡迎提問和建議。

 


本網站無註明「轉載」的著作均由Jak Wings製作 CC BY-NC-SA 2.5
Creative Commons 保持署名-相同方式分享 2.5

小天 说:
Aug 07, 2012 11:13:25 PM

在http://pastebin.com/tools#pastebincl 下载了tar.gz源码再解压后,该如何做?
1、需要编译吗?如果是,却没有发现autogen.sh 也没有configure等文件
2、cmake吗?

小天 说:
Aug 07, 2012 11:15:57 PM

你的第二张图是完整的最终脚本内容吗?

Avatar_small
λ 说:
Aug 11, 2012 10:34:40 AM

第二个是最终的~
编译方式在COMPILE.txt里有说,❶make ❷sudo make install

Avatar_small
λ 说:
Aug 11, 2012 10:35:36 AM

我说的是在Linux下的安装方式

凍仁翔 说:
Nov 20, 2012 03:00:59 PM

hello,

在下的 Ubuntu 12.04 有內建個名叫 pastebinit 的套件,它就可以直接將 code 丟到 http://paste.ubuntu.com/ 上面了 :)

ex:
$ pastebinit LICENCE.txt
http://paste.ubuntu.com/1371887/

小天 说:
Nov 24, 2012 05:25:13 PM

请问,你的脚本源码在哪里可以下载到最新的?

我竟然找不到链接了。

Avatar_small
λ 说:
Jan 26, 2013 02:51:24 AM

脚本就上面两个啊……假如是问pastbincl的话,目前国内pastebin貌似被墙中。

Avatar_small
λ 说:
Jan 26, 2013 02:52:14 AM

;) Thx,最近pastebin被墙,不怎么用pastebin了。

ESIC e payment 说:
Aug 06, 2022 10:52:33 PM

Insurance has become a vital enrollment for all employees in this age and the state governments in India have provided their employees with various schemes that allow them to find themselves good plans from the state government itself, and one of the finest options of this is the security provided since the Insurance is provided by the State Government. ESIC e payment In this context, if employee opted for Employee state insurance, then you will have to pay monthly contributions towards insurance. This monthly ESI contribution deducts from the employee or the employee can choose to pay at their appropriate times.

boardmodelmodel-pape 说:
Apr 25, 2023 02:30:55 PM

We are very conscious about mail spam model-paper.com and we try to protect every email as much as possible. In certain cases your mail may be exposed to public that Modelpapers works on giving out better service in different forms and we do not sell or giveaway your personal information other than public info giving out by you. We are very conscious about mail spam and we try to protect every email as much as possible.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter