jeudi 22 février 2018

How to read file with Bash shell programme

Hi to all
Here are simple programmes to read and ordianary file line by line.
Suppose you have a file called user.txt 

toto1 passtoto1 M.toto1 prof1, réseau
toto2 passtoto2 M.toto2 prof2, réseau
toto3 passtoto3 M.toto3 prof3, réseau
toto4 passtoto4 M.toto4 prof4, réseau
toto5 passtoto5 M.toto5 prof5, réseau

Version 1 file looks like this

#! /bin/bash 
cat < user.txt | while true
read -r ligne
if [ "$ligne" = "" ]; then break ; fi
echo $ligne

exit 0

Run the
Make runnable
chmod u+x 

Version 2 takes the file to read as parameter

while read -r line
    echo $line
done < "$1"

exit 0