Skip to content

Instantly share code, notes, and snippets.

@bluebruce
Created October 4, 2012 04:10
Show Gist options
  • Select an option

  • Save bluebruce/3831421 to your computer and use it in GitHub Desktop.

Select an option

Save bluebruce/3831421 to your computer and use it in GitHub Desktop.
shell/parse string with token. #ifs #shift
snippets()
{
# Parse e_vw_v_scale_${VW_VAR_V_SCALE}
# The token is '_'
_IFS="$IFS";IFS='_';set -- $*;IFS="$_IFS"
# Save the result
VW_VAR_V_SCALE=$5
}
explain()
{
# 把舊的 $IFS 存起來
_IFS="$IFS"
# 設定 parse 的 token 為 '_'
IFS='_'
# 用 $IFS 來 parse $*, 結果會存在 $0, $1, $2.....
set -- $*
# 將 $IFS 設回
IFS="$_IFS"
# 抓取要的結果, 如 $5
parsed_result=$5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment