V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ysicing
V2EX  ›  Bash

shell 数组问题

  •  
  •   ysicing · 2017-11-16 14:59:35 +08:00 · 1136 次点击
    这是一个创建于 2345 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #!/bin/bash
    
    todo=(docker1 docker2 docker3)
    taskid=()
    
    function check(){
    	if [ "$1" = "docker1"];then
        	taskid+=("install_docker1")
        elif [ "$1" = "docker2"];then
        	taskid+=("install_docker2")
        else
        	[ -f "/tmp/mb.sh" ] && taskid=+("install_docker3")
        fi
    }
    
    function run(){
    	for ido in ${todo[@]};do
        	check $ido
        done
        echo ${taskid[@]}
        echo '{
        "num":'\"${#taskid[@]}\"'
        }'
    }
    
    case $1 in
    	*)
        run
    ;;
    esac
    

    大概如上的代码,现在想向一个数组里添加数据问题,如果函数复杂点,貌似就 check 的数据就没加上,是不是用法有问题

    SoloCompany
        1
    SoloCompany  
       2017-11-16 22:51:13 +08:00
    你就不能注意一下格式吗,要知道 shell 的方括号可不是你想的那么简单,右方括号前面缺少空格科室会死人的
    ysicing
        2
    ysicing  
    OP
       2017-11-17 10:17:44 +08:00
    @SoloCompany 哦哦,抱歉了,没注意
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   958 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 20:48 · PVG 04:48 · LAX 13:48 · JFK 16:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.