help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] set and IFS


From: Pierre Gaston
Subject: Re: [Help-bash] set and IFS
Date: Tue, 20 Nov 2018 13:26:33 +0200

On Tue, Nov 20, 2018 at 12:51 PM Gérard ROBIN <address@hidden> wrote:

> Hello,
> I encounter a problem when I modify IFS with this file:
>
> cat nomutilsuid.sh
>
> #!/bin/bash
>
> while read nom reste
> do
>         svIFS=$IFS
>         IFS=:
>          set $nom
>          IFS=$svIFS
>          echo $1 $4
> done < modfile
>
> cat modfile
>
> g:gg:ggg:1:bin
> q:qq:qqq:2:bin
> f:ff:fff:3:bin
> k:*:kkk:4:bin
>
> the output of ./nomutilsuid.sh
>
> g 1
> q 2
> f 3
> k danbin.sh
>
> where danbin.sh is the third file of the current directory instead of 4.
>
> The issue rises with the file /etc/passwd
> How can we solve the problem ?
>

IFS is working  fine the problem is that the * gets expanded because your
variables are  not quoted.
( var=*; echo $var)

Some of the possible fix include:
* read the fields directly: while IFS=: read one two three four rest;do
echo "$one" "$four" ;done
* disable globbing with set -f/set +f
* parse again the line with read
* parse manually with parameter expansion eg ${var##:*}


reply via email to

[Prev in Thread] Current Thread [Next in Thread]