Loop through a comma-separated shell variable

前端 未结 8 1942
醉话见心
醉话见心 2020-12-22 18:15

Suppose I have a Unix shell variable as below

variable=abc,def,ghij

I want to extract all the values (abc, def an

8条回答
  •  情深已故
    2020-12-22 18:55

    Try this one.

    #/bin/bash   
    testpid="abc,def,ghij" 
    count=`echo $testpid | grep -o ',' | wc -l` # this is not a good way
    count=`expr $count + 1` 
    while [ $count -gt 0 ]  ; do
         echo $testpid | cut -d ',' -f $i
         count=`expr $count - 1 `
    done
    

提交回复
热议问题