1. 程式人生 > >shell腳本使用技巧5--字符分隔

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

let clas div shell腳本 bash 分隔 count location lin

#!/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;

註意:[ ]空格;

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