Skip to content

Zeroplex 生活隨筆

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

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

標籤: Bash

shell script 中取得前一個指令的輸出內容

Posted on 2023 年 11 月 28 日 By 日落 在〈shell script 中取得前一個指令的輸出內容〉中尚無留言

假設要將 ls 的結果儲存到變數中,則可用下面的語法:

# 將結果儲存到 $OUTPUT 中
OUTPUT=$( ls / )

echo $OUTPUT

如果要將 stderr 也儲存下來,則加上 redirect:

OUTPUT=$( ls /not/exists 2>&1 )
Tags:Bash, Linux, shellscript

Bash 設定指令逾時時間

Posted on 2023 年 11 月 17 日2023 年 11 月 17 日 By 日落 在〈Bash 設定指令逾時時間〉中尚無留言

透過 timeout 設定指令最長可以跑多久。

timeout 3s sleep 1
echo "$?"
0

timeout 3s sleep 5
echo "$?"
124
Tags:Bash, Linux, shellscript

custom function in shell script

Posted on 2023 年 11 月 16 日 By 日落 在〈custom function in shell script〉中尚無留言

之前看到的 shell script 說明,建立幾乎都是用 `function`:

function show-server-status() {
}

被 IDE 糾正以後才知道有規範 (Google 的 style guide),像是不使用 function 宣告、命名原則:

# [a-zA-Z0-9_] only
# https://stackoverflow.com/a/28115066
show_server_status() {
}

看來該找時間整理一下 scripts 了

Tags:Bash, Linux

Bash prompt

Posted on 2023 年 10 月 13 日2023 年 10 月 13 日 By 日落 在〈Bash prompt〉中尚無留言

在 console 中,bash 等待使用者輸入指令時,會顯示一串文字:

zeroplex@localhost $

這串文字就是 prompt,可以透過設定環境參數 PS1 來調整成自己喜歡的格式。例如:

export PS1="\u@\h: \w \$ "

zeroplex@localhost: /home/zeroplex $

More “Bash prompt” »

Tags:Bash, Linux

xargs 限制 argument 數量

Posted on 2023 年 6 月 1 日2023 年 6 月 1 日 By 日落 在〈xargs 限制 argument 數量〉中尚無留言

如果參數太多,導致 xargs: argument line too long 時,可以使用 -n 參數來限制 argument 數量。

限制每次執行指令時,最多 100 個 arguments:

cat thousand-files | xargs -n 100 rm -fr
Tags:Bash, Linux

文章分頁

1 2 ... 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 國際 授權條款授權.