Skip to content

Zeroplex 生活隨筆

軟體開發、伺服器和生活瑣事

小 縮小字型大小。 中 重設字型大小。 大 放大字型大小。

標籤: Bash

jq (JSON parser) 處理含有 “-” (dash) 的 key 名稱

Posted on 2018 年 12 月 26 日2021 年 3 月 12 日 By 日落 在〈jq (JSON parser) 處理含有 “-” (dash) 的 key 名稱〉中尚無留言

爬 log 剛好遇到 JSON 的其中一個 key 名稱中有「-」(dash) 符號,所以 console 怎麼寫都有錯誤訊息:

$ cat log | cut -d$'t' -f 4 | jq   .http-post
jq: error: post/0 is not defined at , line 1:
.http-post
jq: 1 compile error

從錯誤訊息可以看到「http-post」被切斷了。

若 key name 有特殊符號,或是特殊字元,記得要用引號包起來進行查詢:

$ cat log | cut -d$'t' -f 4 | jq '."http-post"'
{
  "action": "get",
  "name": "John",
  "category": "RD",
}
Tags:Bash, Linux

Increase Bash History Size

Posted on 2018 年 9 月 28 日2021 年 3 月 12 日 By 日落 在〈Increase Bash History Size〉中尚無留言

It is really convenient to use Ctrl+R to find often used commands.

If Bash history is not enough to save those commands, try to add ENV vairables below into .bashrc:

# amount of commands you want to store in .bash_history
export HISTSIZE=1000

# amount of commands you want to store in current bash session
export HISTFILESIZE=200000

Reference:

  • bash HISTSIZE vs. HISTFILESIZE?
    https://stackoverflow.com/questions/19454837/
Tags:Bash, Linux

type (in Bash) 來判別 shell 實際執行的命令

Posted on 2018 年 8 月 27 日2021 年 3 月 12 日 By 日落 在〈type (in Bash) 來判別 shell 實際執行的命令〉中尚無留言

Linux 的 shell 提供了很多彈性,讓使用者可以客製化自己的工作環境。但也因此有時候把環境搞亂了自己也沒發現。

例如說:

johnroyer@box:~$ phpunit --version
PHPUnit 7.3.2 by Sebastian Bergmann and contributors.

johnroyer@box:~$ cd devel/laravel/
johnroyer@box:~/devel/laravel$ phpunit --version
PHPUnit 6.5.12 by Sebastian Bergmann and contributors.

這個時候雖然可以使用 which 來找出實際上執行的指令是哪一個 binary,但有時不一定與執行的是同一個。例如,上面的環境中,執行 which 都會有相同的結果:

$ which phpunit
/home/johnroyer/.config/composer/vendor/bin/phpunit

如果使用 type (Bash built-in) 來檢查的話,則會更清楚的告訴你,指令會如何執行,例如:

$ type phpunit
phpunit is a function
phpunit () 
{ 
    REPO_PHPUNIT=`pwd`"/vendor/bin/phpunit";
    if [ -e $REPO_PHPUNIT ]; then
        $REPO_PHPUNIT $*;
    else
        /home/johnroyer/.config/composer/vendor/bin/phpunit $*;
    fi
}

which 是由 $PATH 環境變數中的路徑來找出對應的執行擋路徑;type 則是由 Bash 當下的環境去檢查當下到底會如何執行。

Tags:Bash, Linux

grep 時保留前後 N 行內文

Posted on 2016 年 9 月 27 日2021 年 3 月 12 日 By 日落 在〈grep 時保留前後 N 行內文〉中尚無留言

一般 grep 只會將出現關鍵字的那一行文字顯示出來,例如:

johnroyer@box:~/logs$ zgrep 'parse' *gz
2016-02-27.log.gz:[2016-02-27 12:00:35] local.INFO: DOMDocument cannot parse XML: Premature end of data in tag html line 2
2016-02-27.log.gz:[2016-02-27 12:00:36] local.INFO: DOMDocument cannot parse XML: Premature end of data in tag html line 2
2016-02-27.log.gz:[2016-02-27 12:01:08] local.INFO: DOMDocument cannot parse XML: Premature end of data in tag html line 2
....

但有時顯示出來的訊息只是 function call stack trace 的其中一行,單看這一行無法理解到底發生了什麼事情。

遇到這種情況時,可以透過參數「-A」和「-B」來設定保留前後文:

johnroyer@box:~/logs$ zgrep 'Exception' *.gz -A 5 -B 2
[2016-05-29 23:41:22] production.INFO: RuntimeException: https://theinitium.com/newsfeed/
[2016-05-29 23:41:22] production.INFO: DOMDocument cannot parse XML: PCDATA invalid Char value 8
[2016-05-29 23:41:23] production.ERROR: exception 'RuntimeException' with message 'Invalid host label, check its content' in /home/segm/prod/www-crawler/vendor/league/url/src/Components/Host.php:164
Stack trace:
#0 /home/segm/prod/www-crawler/vendor/league/url/src/Components/AbstractSegment.php(47): LeagueUrlComponentsHost->validate('rss_Content.jsp')
#1 /home/segm/prod/www-crawler/vendor/league/url/src/Components/AbstractSegment.php(39): LeagueUrlComponentsAbstractSegment->set('rss_Content.jsp')
#2 /home/segm/prod/www-crawler/vendor/league/url/src/Components/Host.php(72): LeagueUrlComponentsAbstractSegment->__construct('rss_Content.jsp')
#3 /home/segm/prod/www-crawler/vendor/league/url/src/AbstractUrl.php(226): LeagueUrlComponentsHost->__construct('rss_Content.jsp')
.....

上面的範例是關鍵字前保留 2 行,往後保留 5 行。

Tags:Bash, Linux, 資訊學習

Helper for PHPUnit path detection in bash

Posted on 2016 年 7 月 15 日2021 年 3 月 12 日 By 日落 在〈Helper for PHPUnit path detection in bash〉中有 1 則留言

較新的 PHP 專案都會在 composer require_dev 自帶 phpunit,這個時候要執行 phpunit 都應該要使用專案中設定的 phpunit 版本:

$ cd /path/to/repository
$ vendor/bin/phpunit

若該專案沒有設定 phpunit 時,才使用系統上,或是 composer global 的 phpunit:

$ cd /path/to/repository
$ ~/.composer/vendor/bin/phpunit  # or "phpunit" for system global

不過這實在有點麻煩,所以乾脆寫 script 處理掉:

phpunit() {
   REPO_PHPUNIT=`pwd`"/vendor/bin/phpunit"

   if [ -e $REPO_PHPUNIT ]; then
      echo "... Run by vendor/bin/phpunit ..."
      $REPO_PHPUNIT $*
   else
      ~/.composer/vendor/bin/phpunit  $*
   fi
}

這樣一來,只要執行 phpunit 就會自動去檢查專案底下是否有 phpunit 可以用;若沒有則自動使用系統的 phpunit。

Tags:Bash, PHPUnit

文章分頁

上一頁 1 ... 3 4 5 6 下一頁

其他

關於我  (About me)

小額贊助

  文章 RSS Feed

  留言 RSS Feed

Apache AWS Bash C/C++ Docker FreeBSD GCP Git Google Java JavaScript Laravel Linux Microsoft MSSQL MySQL Nginx PHP PHPUnit PostgreSQL Python Qt Ubuntu Unix Vim Web Windows WordPress XD 作業系統 分享 好站推薦 專題 攝影 新奇搞笑 新聞 旅遊 生活雜記 程式設計 網路架站 網頁設計 資訊學習 資訊安全 遊戲 音樂


創用 CC 授權條款
本著作係採用創用 CC 姓名標示-相同方式分享 4.0 國際 授權條款授權.