# I comandi vanno invocati includendo questo file nello script che li userà# con il comando source# Utilizzo: comando STRINGA FILE# (assicurarsi di avere i permessi necessari sul file)# si può usare --nocheck come terzo parametro per evitare il checkfilecheckfile() { if [ $# -ne 2 ]; then return 1; fi # Controlla che i comandi siano #+ stati invocati con parametri sufficienti (usato anche successivamente) STRING="$1" # Assegna nomi comprensibili ai parametri FILE="$2" #+ posizionali (usato anche successivamente) if [ ! -f "$FILE" ] # Controlla: then # L'esistenza del file echo "Error: \"$FILE\" does not exist" EXIST="err_file" return 1 elif [ ! -w "$FILE" ] # La presenza del permesso di scrittura then echo "Error: \"$FILE\" is not writeable" EXIST="err_file" return 1 else info=`cat "$FILE" | grep ""$STRING""` if [ "$info" != "" ] # La presenza della stringa nel file then EXIST="yes" else EXIST="no" fi fi}write() { # Scrive la stringa nel file if [ $# -lt 2 ]; then return 1; fi STRING="$1" FILE="$2" if [ $3 != "--nocheck" ] then checkfile "$STRING" "$FILE" fi if [ "$EXIST" = "yes" ] then # Se è già presente, ritorna 1 echo "Error: \""$STRING"\" already wrote in \""$FILE"\"" return 1 elif [ "$EXIST" = "err_file" ] then return 1 fi echo "$STRING" >> "$FILE" # Altrementi la scrive echo "\""$STRING"\" wrote in \""$FILE"\""}delete() { # Cancella la stringa dal file if [ $# -lt 2 ]; then return 1; fi STRING="$1" FILE="$2" if [ $3 != "--nocheck" ] then checkfile "$STRING" "$FILE" fi if [ "$EXIST" = "no" ] then # Se non è presente, ritorna 1 echo "Error: \""$STRING"\" not wrote in \""$FILE"\"" return 1 elif [ "$EXIST" = "err_file" ] then return 1 fi touch "$FILE".delete # Altrementi la cancella sed s/"$STRING"// $FILE > "$FILE".delete mv "$FILE".delete "$FILE" echo "\""$STRING"\" deleted from \""$FILE"\""}