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

reader.sh file looks like this

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

exit 0

Run the reader.sh
Make reader.sh runnable
chmod u+x reader.sh 
 
./reader.sh


Version 2 takes the file to read as parameter

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

exit 0