szilard-dobai

SO - Test Shell

Dec 1st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. 1. citeste numere cu stdin, afiseaza cele intregi si pozitive, face suma celor > 3 cifre, scrie in file daca e fisier obisnuit
  2. testare_numar_intreg_pozitiv ()
  3. {
  4. if test $1 -ge 0
  5. then
  6. echo $1
  7. fi
  8. }
  9.  
  10. if test $# -ne 1
  11. then
  12. echo "wrong number of arguments"
  13. exit -1
  14. fi
  15.  
  16. file=$1
  17. sum=0
  18.  
  19. if [ ! -h $file ] && [ -f $file ]
  20. then
  21. while read linie && test "$linie" != "End"
  22. do
  23. testare_numar_intreg_pozitiv $linie
  24. if test ${#linie} -ge 3
  25. then
  26. sum=`expr $sum + $linie`
  27. fi
  28. done
  29.  
  30. echo "suma = $sum" > $file
  31. else
  32. echo "not regular file"
  33. exit -2
  34. fi
  35.  
  36.  
  37.  
  38.  
  39.  
  40. 2. citeste cu stdin, daca e director, parcurge nerecursiv si numara legaturile simbolice care incep cu majuscula si nu au cifra in nume, scrie in file daca e fisier normal
  41. if test $# -ne 1
  42. then
  43. echo "wrong number of arguments"
  44. exit -1
  45. fi
  46.  
  47. file=$1
  48. contor=0
  49.  
  50. if [ ! -h $file ] && [ -f $file ]
  51. then
  52. while read linie && test "$linie" != "Stop"
  53. do
  54. if test -d $linie
  55. then
  56. echo $linie
  57. for f in $linie/*; do
  58. if test -h $f
  59. then
  60. if [[ "$(basename -- $f)" =~ ^[A-Z][^0-9]*$ ]];
  61. then
  62. contor=`expr $contor + 1`
  63. fi
  64. fi
  65. done
  66. echo "$linie: $contor legaturi" >> $file
  67. contor=0
  68. fi
  69. done
  70. else
  71. echo "not a regular file"
  72. exit -2
  73. fi
  74.  
  75.  
  76.  
  77. 3. ia un file si un prefix. citeste de la tastatura pana la 'End'. numara toate cuvintele care incep cu prefix si face suma lungimilor tuturor cuvintelor. afiseaza contorul si suma la fiecare pas, las final scrie suma in file.
  78. if test $# -ne 2
  79. then
  80. echo "wrong number of arguments"
  81. exit -1
  82. fi
  83.  
  84. file=$1
  85. prefix=$2
  86. contor=0
  87. suma=0
  88.  
  89. if [ ! -h $file ] && [ -f $file ]
  90. then
  91. while read linie && test "$linie" != "End"
  92. do
  93. if [[ "$linie" =~ ^$prefix ]];
  94. then
  95. echo $linie
  96. contor=`expr $contor + 1`
  97. fi
  98. suma=`expr $suma + ${#linie}`
  99. echo "contor = $contor"
  100. echo "suma = $suma"
  101. done
  102. echo "suma = $suma" >> $file
  103. else
  104. echo "not a regular file"
  105. exit -2
  106. fi
Add Comment
Please, Sign In to add comment