Cshell 没有原生函数支持,也没有太多花哨的东西,比如变量域等。

但是对于 cshrc 启动程序来说,函数确实是必不可少的。
目标
- 像使用脚本一样调用方法
- 将设置应用到当前的 cshell 环境中。
- 易于获取帮助或应用自动补全
- 与我们的 cshrc 结构集成
- 调用函数时支持参数
这里有一些在 cshell 中编写类函数文件的好做法。
- 提供一个名为 “–program” 的选项。这样用户可以轻松更改 “函数” 的名称。
- 提供以下布尔选项
- is_help_mode
- is_debug_mode
- is_dryrun_mode
- is_complete_mode
- 在文件中编写自动补全
- 使用 while 循环遍历 argv
- 注意一些特殊行为,比如 exit 命令是否会退出整个程序等。
模板
#!/usr/bin/env tcsh
# Use this program like a csh function.
# Once you saved this file to $__MYSH_ROOT/cshsub/myecho.csh
# alias "myecho" "source $__MYSH_ROOT/cshsub/myecho.csh --program myecho \!*"
# Example: myecho -pcolor "blue" -prefix "MY-TEST" -color yellow "This is my message."
# Initialize the environment context
set program = $0 ;
set is_help_mode = 0 ;
set is_debug_mode = 0 ;
set is_dryrun_mode = 0
set is_complete_mode = 0 ;
# Prefix text color
set pcolor = "" ;
# Message body color
set color = "" ;
# Default prefix text
set prefix = "MY-INFO" ;
set argc = $#argv ;
set i = 1 ;
set argnomatch = "" ;
while ($i <= $argc)
set is_argmatch = 0 ;
set nexti = `expr $i + 1` ;
# Bool: Help mode
if ("$argv[$i]" == "-h" || "$argv[$i]" == "-help" || "$argv[$i]" == "--help") then
set is_help_mode = 1 ;
set is_argmatch = 1 ;
endif
# Bool: Set the debug mode when -debug is present
if ("$argv[$i]" == "--debug") then
set is_debug_mode = 1 ;
set is_argmatch = 1 ;
endif
# Bool: Set the debug mode when -dryrun is present
if ("$argv[$i]" == "--dryrun") then
set is_dryrun_mode = 1 ;
set is_argmatch = 1 ;
endif
# Bool: Set the debug mode when -dryrun is present
if ("$argv[$i]" == "--complete") then
set is_complete_mode = 1 ;
set is_argmatch = 1 ;
endif
# Param: Set the program name from the argument, so no link is needed
if ("$argv[$i]" == "--program") then
set program = $argv[$nexti] ;
set i = $nexti ;
set is_argmatch = 1 ;
endif
# -------------------------------------------------------------------------------------------------------------------
# Bool: Keep the tmp git repo if -keep_tmp is present
# default is 0, tmp git repo will be removed
if ("$argv[$i]" == "-color") then
set color = $argv[$nexti] ;
set i = $nexti
set is_argmatch = 1 ;
endif
if ("$argv[$i]" == "-pcolor") then
set pcolor = $argv[$nexti] ;
set i = $nexti
set is_argmatch = 1 ;
endif
if ("$argv[$i]" == "-prefix") then
set prefix = $argv[$nexti] ;
set i = $nexti
set is_argmatch = 1 ;
endif
# -------------------------------------------------------------------------------------------------------------------
if ($is_argmatch == 0) then
set argnomatch = "$argnomatch $argv[$i]" ;
endif
set i = `expr $i + 1` ;
end
set colors = ("red" "green" "yellow" "blue" "magenta" "cyan" "white" "black") ;
if ("$pcolor" != "") then
set is_pcolor_available = 0 ;
foreach each_color ($colors)
if ("$each_color" == "$pcolor") then
set is_pcolor_available = 1 ;
endif
end
if ($is_pcolor_available == 0) then
echo "Available color code are:\n$colors\nPlease correct and rerun, exiting myecho function ..." ;
exit ;
endif
endif
if ("$color" != "") then
set is_color_available = 0 ;
foreach each_color ($colors)
if ("$each_color" == "$color") then
set is_color_available = 1 ;
endif
end
if ($is_color_available == 0) then
echo "Available color code are:\n$colors\nPlease correct and rerun, exiting myecho function ..." ;
exit ;
endif
endif
set program = `basename $program` ;
# Print out the help message
if ($is_help_mode > 0) then
echo "\n$program [options]\n rsync files from local git repo to deployment area\n" ;
exit ;
endif
if ($is_complete_mode > 0) then
echo "Setting the command auto completion." ;
set __myexec_option_words = (help h complete debug dryrun color pcolor prefix) ;
complete $program \
'c/-/$__myexec_option_words/'
exit ;
echo "This message should not be printed out since it is after the exit command" ;
endif
if ($is_debug_mode == 1) then
# Debug mode settings
echo $mycolor_magenta"MY-DEBUG: "$mycolor_end"Current program is "$program ;
else
# Non-debug mode settings
endif
if ("$color" != "") then
set color_var = "mycolor_"$color ;
if ("$pcolor" != "") then
set pcolor_var = "mycolor_"$pcolor
eval echo \$"{"$pcolor_var"}"{$prefix}":"$mycolor_end \$"{"$color_var"}"$argnomatch $mycolor_end ;
else
set echo_cmdstr = "{$prefix}: \$argnomatch"
eval "echo "$echo_cmdstr
endif
else
set echo_body = $argnomatch ;
if ("$pcolor" != "") then
set color_var = "mycolor_"$pcolor
eval echo \$"{"$color_var"}"{$prefix}":"$mycolor_end $argnomatch ;
else
set echo_cmdstr = "{$prefix}: \$argnomatch"
eval "echo "$echo_cmdstr
endif
endif
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...