Who cares about math!!!

Ricordatevi: l'algebra è come il porco, non si butta mai niente! Maria Rosaria Celentani, Scienze MM.FF.NN


Result of final test

#!/bin/bash if [ "$EUID" -eq 0 ] # IF = 0 you are root then echo "You cannot run this script as root" exit fi blk='\e[0;30m' # Black - Regular red='\e[0;31m' # Red grn='\e[0;32m' # Green ylw='\e[0;33m' # Yellow blu='\e[0;34m' # Blue max=-1 min=9999 clear #will clear the screen function checkFile() { if [ ! -f $1 ] then echo File not found. exit 1 fi } function is_int() { return $(test "[email protected]" -eq "[email protected]" > /dev/null 2>&1) } function countLineWordOccurrencies() { result=$(cat $1 | grep $2 | wc -l ) echo $result } function countWordOccurrencies() #To Call this function, you need to pass NAMEFILE, Pattern To Match { result=$(cat $1 | grep -oh $2 | wc -w) echo $result } function replaceString() #To Call This Functions, you need to pass 3 parameters: string1, string2 (replace string1), filename { checkFile $3 sed -i 's/'"$1"'/'"$2"'/g' $3 } read -p "Enter the name of the file to filter " filename # =finaltest.txt checkFile $filename # result=$(countLineWordOccurrencies $filename $word) # echo -e Found the word $word on $red $result $blk lines # -o, --only-matching # Print only the matched (non-empty) parts of a matching line, # with each such part on a separate output line. # -h, --no-filename # Suppress the prefixing of file names on output. This is the # default when there is only one file (or only standard input) to # search. # -w count word occurrencies read -p "How many string you want to search? : " n if $(is_int "$n"); then echo "You've entered the Integer: $n" else echo "YOU SHALL NOT PAAAAASS!!!: $n is not an Integer" exit 1 fi #Put values into the array for (( i=0; i<n; i++ )) do read -p "Insert the string $i : " vet[$i] done #Print the Array # for (( i=0; i<n; i++ )) # do # echo ${vet[$i]} # done #As for the print cycle, we can write code into the block in order to do "something" for each element of array for (( i=0; i<n; i++ )) do result=$(countWordOccurrencies $filename ${vet[$i]}) if [ $result -gt 0 ] then echo -e "$grn\\nLet's highlight all occurrencies of the word ${vet[$i]} \n" $blk cat $filename | grep --color='auto' -i ${vet[$i]} echo -e "$grn\nNow count on each line the word $word, match" $blk res2=$(countLineWordOccurrencies $filename ${vet[$i]}) echo -e Found the word $red ${vet[$i]} $blk on $red $res2 $blk lines res3=$(countWordOccurrencies $filename ${vet[$i]}) echo -e Found the word $red ${vet[$i]} $blk a total of $red $res3 $blk times if [ "$res3" -gt "$max" ] then max=$res3 maxW=${vet[$i]} # echo res3 is $res3 # debug # echo max is $max # debug fi #if (("$res3" < "$min")) if [ "$res3" -lt "$min" ] then min=$res3 minW=${vet[$i]} # echo $min debug # echo $minW debug fi else echo -e "no match found for the word $red ${vet[$i]}" $blk fi done echo -e "Max matches of word $red $maxW $blk is $red $max" $blk if (("$min" != 9999 )) then echo -e "Min match of string $red $minW $blk is $red $min" $blk else echo -e "Min match of string $red $maxW $blk is $red $max" $blk fi read -p "What string do you want to replace? : " S1 read -p "What do you want to write instead of $S1 : " S2 read -p "What is the file you wuold like to edit? (irreversible) : " filename # We are sending to a function, 2 strings and a filename replaceString $S1 $S2 $filename echo -e $blk Verifica file di Log per il risultato echo -e $blk Test Completato Con Successo $(date) > log.txt