shell脚本使用技巧5--字符分隔

#!/bin/bash
#filename:ifs.sh
data="name,sex,rollon,location"
oldIFS=$IFS
IFS=, 
for item in $data;
do
    echo Item: $item
done

#IFS=$oldIFS

设置IFS为,号分隔符

#!/bin/bash
#filename:fenge.sh
line="root:x:0:0:root:/root:bin/bash"
oldIFS=$IFS
IFS=":"
count=0
for item in $line;
do
    [ $count -eq 0 ] && user=$item;
    [ $count -eq 6 ] && shell=$item;
    let count++
done;
IFS=$oldIFS
echo $user's shell is $shell;

注意:[ ]空格;

怕什么真理无穷,进一寸有一寸的欢喜。---胡适
原文地址:https://www.cnblogs.com/hujianglang/p/6978896.html