#!/bin/bash
# takes in params one or more file names
# if not, asks one or more file names
# checks if the file is readable
# then outputs the file line by line
[ $# -eq 0 ] && read -p "Please enter one or more file name : " files || files="$@"
[ "$files" ] && for file in $files; do
echo -e "\e[1;33m>>>>Reading $file :\e[0m"
[ "$file" ] && [ -f "$file" ] && [ -r "$file" ] && while IFS= read -r line; do
echo "$line"
done < "$file" || echo -e "\e[1;31mERROR : $file is not a readable file or you do not have the permission to read it\e[0m"
done || echo -e "\e[1;31mMissing file name parameter\e[0m"
exit
#!/bin/bash
# takes in param a file with letters followed by a number
# shows only the lines where the number is bigger than 10
# $# Le nombre de paramètres passés au script
[ $# -eq 0 ] && read -p "Please enter one or more file name : " file || file="$1"
[ "$file" ] && [ -f "$file" ] && [ -r "$file" ] && while IFS= read -r line
do
number="$(sed 's/[A-Za-z]* //' <<< "$line")"
[ $number -gt 10 ] && echo "$line"
done < "$file" || echo -e "\e[1;31mERROR : you must introduce in parameter a readable file and have the permission to read it"
exit
notes.txt
dupont 8 6 3 7 12 18 eric 7 5 10 45 francoise 5 5 9 8 18 10 20 15 19
#!/bin/bash
# takes in params a file where each line is a name followed by one or more numbers
# returns the name followed by the average of the numbers
calculate_average() {
occurences=$# # $# Le nombre de paramètres passés au script
sum=0
while (( $# ))
do
sum=$(( $sum + $1 )) # $1 est un paramètre
shift
done
echo "scale=1; $sum/$occurences" | bc # commande basic calculator
}
[ $# -eq 0 ] && read -p "Please enter one or more file name : " file || file="$1"
[ "$file" ] && [ -f "$file" ] && [ -r "$file" ] && while IFS= read -r line; do
read name notes <<< $(sed 's/\([a-z]*\) \(.*\)/\1 \2/' <<< "$line")
avg=$(calculate_average $notes)
echo "$name: $avg"
done < "$file" || echo -e "\e[1;31mERROR : $file is not a readable file or you do not have the permission to read it\e[0m"
exit
users.txt
NiSIMON students nis123 cecile students cec123 PhLEDUC students phl123 pol adm xyz345 RaDESDEMOUSTIER students rad123 jean students jea123
#!/bin/bash
# takes in params a file where each line is a name/group/passwd of users
# returns the name/group/passwd of existing users
[ $# -eq 0 ] && read -p "Please enter one or more file name : " file || file="$@"
[ "$file" ] && [ -f "$file" ] && [ -r "$file" ] && while IFS= read -r line; do
name=$(cut -d " " -f1 <<< "$line")
for user in $(cat /etc/passwd | cut -d : -f1)
do
[ $name = $user ] && echo "$line"
done
done < "$file"
exit
#!/bin/bash
# finds executables files
# returns the second line if not empty
for file in *
do
[ -f "$file" ] && [ -x "$file" ] && line=$(cat $file | sed -n 2p) && [ "$line" ] && [ -n "$line" ] && echo -e "\e[1;35m$file\e[0m: $line"
done
exit