jeudi 22 février 2018

shell script to print multiplication table

This is a simple Bash shell script to print out the multiplicaiton
./table.sh 5 10
0*10=0
1*10=5
2*10=10
3*10=15
4*10=20
5*10=25
6*10=30
7*10=35
8*10=40
9*10=45

Version 1
#! /bin/bash

for i in  {0..12}
do
    echo "$i*$1=" $(($i*$1))
done



Version 2

#! /bin/bash
for ((number=0; number<$2; number++))
do
echo "$number*$2="$(($number*$1))
done


 Run table.sh
chmod u+x table.sh
./table.sh 5 10


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