Skip to content

Instantly share code, notes, and snippets.

@dekokun
dekokun / recursive.sh
Created January 19, 2012 13:40
シェルスクリプトで再帰を用いて階乗計算
expr $1 + 1 >/dev/null 2>&1
if [ $? -ne 1 -a $? -ne 0 ]; then
echo "must be numeric"
exit
elif [ $1 -lt 0 ]; then
echo "must be more than 0"
exit
elif [ $1 -eq 1 -o $1 -eq 0 ]; then
echo 1;
@dekokun
dekokun / color.php
Created February 3, 2012 10:11
PHPで標準出力の色やスタイルを変える
<?php
$color = array(
'black' => 30,
'red' => 31,
'green' => 32,
'brown' => 33,
'blue' => 34,
'purple' => 35,
'cyan' => 36,
'grey' => 37,
@dekokun
dekokun / toclip.sh
Created February 27, 2012 10:09
for Mac, Copy File to clipboard or Copy standard input to clipboard .
#!/bin/bash
COMMAND=`basename $0`
usage(){
echo "Usage: $COMMAND FILE"
echo "With no FILE, read standard input."
}
error(){
local ERROR_MESSAGE=$@
@dekokun
dekokun / test.coffee
Created March 15, 2012 16:32
mocha, sinon, should test
sinon = require("sinon")
should = require("should")
a = {}
a.test = () ->
[1, 2]
describe "calc", ->
describe ".add", ->
it "should return sum of 2 arguments", ->
result = 1 + 2
@dekokun
dekokun / gist:2179707
Created March 24, 2012 07:39
プロセス見て動いてなかったら何かする
run=$(ps aux | grep '[h]oge')
if [ -z "$run" ]; then
do something
fi
@dekokun
dekokun / test.js
Created March 27, 2012 13:02
おかしい→おかしくなさそう
% mocha *.js [~/work/test]
..
✖ 1 of 2 tests failed:
1) hoge fuga:
AssertionError: expected 1 to equal 2
以下エラーメッセージ
@dekokun
dekokun / gist:2397408
Created April 16, 2012 09:59
シェルスクリプトで、引数が0以上の整数であることをチェック
usage()
{
echo $0 hoge
}
if [ -z $1 ]; then
usage
exit
else
expr $1 + 1 >/dev/null 2>&1
@dekokun
dekokun / test.sh
Created July 6, 2012 07:22
y/Nを見てコマンド実行
function yes_no {
msg=$1
while :
do
echo -n "${msg} y/N: "
read ans
case $ans in
[yY]) return 0 ;;
[nN]) return 1 ;;
esac
@dekokun
dekokun / util.sh
Created July 6, 2012 07:25
引数を見てコマンド発行するスクリプト
case $1 in
hogehoge)
hogehoge
;;
fugafuga)
fugafuga
;;
*)
echo hogehoge fugafuga
exit 1
@dekokun
dekokun / func.sh
Created July 12, 2012 17:09
内部状態を持つ関数(シェルスクリプト)
#!/bin/zsh
func(){
echo 2
local pre_func_definition
local pre_count
local new_count
local new_func_definition
pre_func_definition=$(which func)
pre_count=$(echo $pre_func_definition | awk 'NR==2, NR==2 {print $2}')