Nieuws:

Welkom, Gast. Alsjeblieft inloggen of registreren.
Heb je de activerings-mail niet ontvangen?

Auteur Topic: Bash: Fout met IFS opgelost. Maar waarom fout?  (gelezen 567 keer)

Offline bart85

  • Lid
Bash: Fout met IFS opgelost. Maar waarom fout?
« Gepost op: 2022/12/29, 17:55:42 »
In mijn script word een tekstbestand uitgelezen. Het heeft kolommen gescheiden met een tab. Ik had IFS ingesteld met:
IFS='\t'
Verbeterd met:
IFS=$'\t'

Waarvoor is dit dollar-teken nodig?
Relying on complex tools to manage and build your system is going to hurt the end-users. [...] "If you try to hide the complexity of the system, you'll end up with a more complex system". Layers of abstraction that serve to hide internals are never a good thing. Instead, the internals should be designed in a way such that they NEED no hiding.

— Aaron Griffin

Offline Bloom

  • Lid
Re: Bash: Fout met IFS opgelost. Maar waarom fout?
« Reactie #1 Gepost op: 2022/12/29, 21:44:48 »
Zogenaamd "escaped characters" (een backslash gevolgd door een speciaal karakter) moeten geïnterpreteerd worden.
Als je '\t' opgeeft in Bash, dan is dat een backslash en een t, het wordt dus niet vertaald naar tab, wat je eigenlijk wil.
Daarom moet je dat dollarteken opgeven: $'iets' wordt vertaald naar "iets" met escaped characters geïnterpreteerd en dus een tab of een newline effectief in de string ingevuld.

Offline bart85

  • Lid
Re: Bash: Fout met IFS opgelost. Maar waarom fout?
« Reactie #2 Gepost op: 2022/12/30, 08:38:28 »
Dankjewel voor de uitleg.
Relying on complex tools to manage and build your system is going to hurt the end-users. [...] "If you try to hide the complexity of the system, you'll end up with a more complex system". Layers of abstraction that serve to hide internals are never a good thing. Instead, the internals should be designed in a way such that they NEED no hiding.

— Aaron Griffin