#!/bin/bash input=("" "" "" "" "" "" "") myFile=imenik_26097.dat function potrdi(){ read -p "Potrdi? y/n : " -n 1 -r if [[ $REPLY != "y" ]]; then echo "" echo "Prekinjeno!" exit 1 fi echo "" } function isciPod(){ formString=$(printf '%s;' "${input[@]/#/.*}") if grep -q "$formString" "$myFile"; then grep "$formString" "$myFile" else echo "Ni kontakta" fi } function urediPod(){ # The '-F' option specifies the field separator as a semicolon and the '-v' option creates a variable 'ref' with the value of the first element of the 'input' array. # The pattern to search for is '$1==ref', which means the first field of the line (i.e., the part of the line before the first semicolon) should match the value of 'ref'. searchedLine=$(awk -F';' -v ref="${input[0]}" '$1==ref {print}' $myFile) if [[ $searchedLine == "" ]]; then echo "ERROR: Ni osebe z tem id" exit 1 fi IFS=';' read -r -a array <<< "$searchedLine" for i in "${!input[@]}"; do [[ "${input[$i]}" != "" ]] && array[$i]="${input[$i]}" done newLine=$(IFS=';' ; echo "${array[*]}") # s substitution # g all occurrences of the searched string should be replaced sed -i "s|${searchedLine}|${newLine}|g" $myFile echo "Urejeno uspesno." } function odstraniPod(){ #-i modify the file in-place. # /d delete sed -i "/${input[0]};/d" "$myFile" echo "Kontakt zbrisan." } function dodajPod(){ input[0]=$(($RANDOM % 99999 + 1)) # Set any empty input values to "PRAZNO" for i in "${!input[@]}" do if [[ -z "${input[$i]}" ]]; then input[$i]="PRAZNO" fi done # Append each input value to the file separated by ';' printf "%s;" "${input[@]}" >> $myFile echo "" >> $myFile # Print success message echo "Kontakt dodan ${input[@]}" } # The `! -f` operator checks if the file does not exist. if [ ! -f "$myFile" ]; then echo "" > $myFile fi selection=$1 shift while [ $# -gt 0 ] ; do case $1 in -id) input[0]="$2" ;; -ime) input[1]="$2" ;; -priimek) input[2]="$2" ;; -naslov) input[3]="$2" ;; -posta) input[4]="$2" ;; -kraj) input[5]="$2" ;; -tel) input[6]="$2" ;; *) echo "ERROR napacen kljuc $1" exit 1 ;; esac shift shift done potrdi # izbiranje funkcije case $selection in -dodaj) dodajPod "$@" ;; -izbrisi) odstraniPod "$@" ;; -isci) isciPod "$@" ;; -uredi) urediPod "$@" ;; *) echo "ERROR napacen ukaz $selection" exit 1 ;; esac