function test_argv echo$argv echo$argv[1] $argv[2] echo$argv[3] echo$status count $argv end
调用:
1
test_argv 测试 一下 命令行
输出:
1 2 3 4 5
测试 一下 命令行 测试 一下 命令行 0 3
3. 语法
a. if 语句
1 2 3 4 5 6 7 8 9
function test_if if [ (count $argv) -eq 0 ] echo"false" elseif [ "$argv[1]" -eq "$argv[2]" ] echo$argv[1] "=="$argv[2] else echo$argv[1] "!="$argv[2] end end
1 2 3 4
# 输入 test_if 2 3 # 输出 2 != 3
b. switch 语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14
function test_switch for string in$argv switch $string case"switch" echo"this is switch" case"if" echo"this is if" case"while" echo"this is while" case'*' echo"none" end end end
1 2 3 4 5 6 7
# 输入 test_switch "if""while""switch""dsads" # 输出 this is if this is while this is switch none
c. while 语句
1 2 3 4 5
function test_while whiletrue echo"fish shell" end end