Created
July 12, 2012 17:09
-
-
Save dekokun/3099398 to your computer and use it in GitHub Desktop.
内部状態を持つ関数(シェルスクリプト)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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}') | |
new_count=$(expr $pre_count + 1) | |
new_func_definition=$(echo $pre_func_definition | sed -e "s/echo $pre_count/echo $new_count/") | |
eval $new_func_definition | |
} | |
func | |
func | |
func | |
func | |
func | |
---------- | |
2 | |
3 | |
4 | |
5 | |
6 |
evalの挙動より先にwhichの挙動の関係でbashでは使えません。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
evalの挙動の関係でbashでは使えません。