SHARE
TWEET

SO - Bash Script #3

razvanth21 Dec 3rd, 2018 (edited) 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /bin/bash
  2.  
  3. if [ $# -lt 1 ]
  4. then
  5.     echo "Usage: $0 <file> <n1, n2, ...>"
  6.     exit 1
  7. fi
  8.  
  9. nr=0
  10. s=0
  11. k=0
  12.  
  13. if [ -f $1 -a ! -L $1 ] # e ok, merge mai departe
  14. then
  15.     while read n
  16.     do
  17.         rez=`echo $n | grep -Eo '[0-9]+'`
  18.         if [ ! -z "$rez" ] # rezultatul nu e string gol (-z)
  19.         then
  20.             nr=$rez
  21.             break
  22.         fi
  23.     done < $1
  24.  
  25.     echo "$nr"
  26.  
  27.     if [ $nr -eq 0 ]
  28.     then
  29.         echo "number not found"
  30.         exit 1
  31.     else
  32.         for i in "${@:2}" # prin toate argumentele, incepand cu al 2-lea
  33.         do
  34.             if [ $(($i % $nr)) -eq 0 ]
  35.             then
  36.                 k=$(($k + 1))
  37.             fi
  38.  
  39.             s=$(($s + ${#i})) # suma cifrelor
  40.         done
  41.     fi
  42. else
  43.     echo "$1 not a regular file"
  44.     exit 1
  45. fi
  46.  
  47. echo "$k numere divizible cu $nr"
  48. echo "suma cifrelor $s"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top