Advertisement
adziavitski

Examination bash scripting task

Aug 5th, 2015
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.58 KB | None | 0 0
  1. #!/usr/bin/bash
  2. #GNU bash, version 4.3.33(1)-release (x86_64-redhat-linux-gnu)
  3.  
  4. ####Environment block####
  5. param_key=$1
  6. param_type=$2
  7. curr_dir=`dirname "$BASH_SOURCE"`
  8. file=$curr_dir"/GTS_LINUX-MULTI-001A.results"
  9. fl_buff=$curr_dir"/buf_fl.tmp"
  10. #last_buf=$curr_dir"/buf_last.tmp"
  11. ptrn_mv_start=MEASURED_VALUE
  12. ptrn_mv_end=MEASURED_VALUE_EOF
  13. ln_mv_start=`grep -nw $ptrn_mv_start -F $file|awk -F : '{print $1}'`
  14. ln_mv_end=`grep -nw $ptrn_mv_end -F $file|awk -F : '{print $1}'`
  15. ln_indx=1
  16. ####End of environment block###
  17.  
  18. #####Function Block#####
  19. # Function is parse file from $file variable. Any line between $ptrn_mv_start and $ptrn_mv_end would be writen to
  20. # buffer file from $fl_buff variable.  
  21. function file_parser () {
  22.     cat /dev/null > $fl_buff
  23.     while read line
  24.     do
  25.         if [[ $ln_indx -gt $1 && $ln_indx -lt $2 ]]
  26.         then
  27.             echo $line >> $fl_buff
  28.         fi
  29.         (( ln_indx +=1 ))
  30.     done < "$file"
  31. }
  32.  
  33. #Function for parse buffer file from $fl_buff variable. It check parameters and do filtration of output.
  34.  
  35. function str_parser_any () {
  36.     ###Check input parameters###
  37.     #Checking of input parameters, sorting of output and removing of duplicates
  38.     if [ "$1" == "-t" ]
  39.         then   
  40.             if [ "$2" == "f" ]
  41.                 then
  42.                     echo "Output filtered by files "
  43.                     str_parser_f_bufer=`cat $fl_buff |  grep ^- | awk -F ";" '{print $2 "\t" $3 }' |sort -u`
  44.             elif [ "$2" == "d" ]
  45.                 then
  46.                     echo "Output filtered by directories "
  47.                     str_parser_f_bufer=`cat $fl_buff |  grep ^d | awk -F ";" '{print $2 "\t" $3 }' |sort -u`
  48.             else
  49.                 echo "Output not filtered. Use f and d keys"
  50.                 str_parser_f_bufer=`cat $fl_buff | awk -F ";" '{print $2 "\t" $3 }' |sort -u`
  51.             fi
  52.         else
  53.             str_parser_f_bufer=`cat $fl_buff | awk -F ";" '{print $2 "\t" $3 }' |sort -u`
  54.     fi
  55.     ###End check input parameters###
  56.  
  57.     ###Checking of last login date and output to console
  58.     buf_indx=`echo "$str_parser_f_bufer"| wc -l`
  59.     echo "$users_lld"| 
  60.     for (( indx=1; indx <=buf_indx; indx++))
  61.     do
  62.         str_parser_uname=`echo "$str_parser_f_bufer"| awk "FNR == $indx {print}" | awk '{print $1}'`
  63.         str_parser_gname=`echo "$str_parser_f_bufer"| awk "FNR == $indx {print}" | awk '{print $2}'`       
  64.         str_parser_usr_ltime=`last $str_parser_uname | head -n 1 | awk '{print $4 " " $5 " " $6 }'`
  65.         echo "User:""$str_parser_uname" "   " "group:" "$str_parser_gname" "    " " last login at: " "$str_parser_usr_ltime"
  66.     done
  67. }
  68. #####End of function block#####
  69.  
  70.  
  71. ######Function execution block ############
  72. file_parser $ln_mv_start $ln_mv_end
  73. str_parser_any $param_key $param_type
  74. ######End of function execution block #####
  75.  
  76.  
  77. rm $fl_buff
  78. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement