顯示具有 Bash 標籤的文章。 顯示所有文章
顯示具有 Bash 標籤的文章。 顯示所有文章

2019年10月22日 星期二

定時排程刪除超過一定天數的檔案

https://softwareengineering.stackexchange.com/questions/149824/automatically-delete-files-after-they-expire
https://superuser.com/questions/1072679/bash-what-does-means
http://linux.vbird.org/linux_basic/0430cron.php

find $search_path -atime +3 -exec rm {} \;

加到crontab裡面
# crontab -e

{}的意思就是find輸出的結果會帶到rm的參數,\;是確保command結束符號,可用+取代

2014年9月12日 星期五

pushd & popd

d就是directory,pushd 之後馬上切換到 路徑,在pushd 之後馬上在切換到,popd會切到,在popd一次會切到最一開始的路徑。
通常應該是寫bash script比較會用到
在這看到的應用(build docker images的script)
https://github.com/CentOS/CentOS-Dockerfiles
Wiki:
http://en.wikipedia.org/wiki/Pushd_and_popd

2013年12月12日 星期四

How to PAUSE and CONTINUE a Linux process

Use kill command to send -STOP signal to pause a process and -CONT signal to continue it.

Example on CentOS6 Desktop:
1. Open a Terminal (name as T1) on Desktop and send "ps -aux | grep bash" to see how much bash in there.

2. Open another Terminal (name as T2) on Desktop and on T1 send "ps -aux | grep bash" to see process id of new created bash (T2)

3. On T1, send "kill -STOP " to pause T2
>T2 is hang

4. On T1, send "kill -CONT " to continue T2
>T2 is back to work
This is useful if a script is running on T2 and you want to pause it and continue it later

Reference:
http://tombuntu.com/index.php/2007/11/23/how-to-pause-a-linux-process/

2011年11月2日 星期三

linux bash getopt


getopt 是 shell 裡抓參數的好工具
例: getopt abc:d: 容許參數 -a -b -c -d, -c and -d 後面要接參數

#!/bin/sh
set - `getopt abc:d: $*`
while true; do
  case $1 in
    -a) echo option -a
      shift ;; 
    -b) echo option -b
      shift ;; 
    -c) echo option -c=$2
      shift 2 ;; 
    -d) echo option -d=$2
      shift 2 ;; 
    --) shift
      break ;; 
    *) echo "error!"
      exit 1 ;; 
  esac
done

執行結果 
# ./go -a
option a
# ./go -c jack
option -c=jack
# ./go -b -c jack
option -b
option -c=jack
# ./go -b -c test -d
getopt: option requires an argument -- d
option -b
option -c=test
(使用後面要加參數的 option 會提示)
# ./go -b -c test -f
getopt: invalid option -- f
option -b
option -c=test
(使用未支援的參數會提示)

 http://pank.org/blog/2004/05/getopt-example.html

set - 的用法

#set - -a a -b b c d e f g
#echo $1
-a
#echo $*
-a a -b b c d e f g
#echo $0
bash
link

2011年7月4日 星期一

Manipulating Strings

bash下字串的操作

chars="12345678"
echo ${chars:2:3} #234
format: ${string:position:length}
position index從1開始
echo ${chars:2} #345678
從0開始算

reference:
http://tldp.org/LDP/abs/html/string-manipulation.html

2011年2月22日 星期二

/etc/init.d/functions

裡面有echo_passed, echo_success, echo_failure, echo_warning
可以輸出有顏色的[ OK ], [PASSED]等等
但是若output到文字檔會有顏色控制碼 (cat輸出還是有顏色)

http://bash.cyberciti.biz/guide//etc/init.d/functions

2011年1月13日 星期四