Beri bash i ebash / Говнокод #6222 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
#!/bin/bash

######################################################################################
#
#	Write a bash script that obtains active processes for the current user, 
#	breaks them into chains and prints all unique chains containing 3 or more 
#	PIDs in the following format:
#		PID1:PID2:PID3:…:PIDn
#		PID:PID:PID:…:PID
#		…
#	So that each next PID is a parent of the previous PID, the first PID in 
#	each chain does not have children and the last PID in each chain 
#	does not have parent.
#
######################################################################################

TEMPFILE="/tmp/$0$$"				# file needs to save the process list
						# it's really needed to divide columns
						# generated 'ps'
						
# parameters:
# 	-H 	- sorts the list as process forest (it lets to find connection between child and parent faster)
#	-o	- sets the output format
#	"%p %P" - thow columns in output: PID and PPID
#	-u	- sets username
#	`whoami` - get the current user name
ps -H -o "%p %P" -u `whoami` > $TEMPFILE
PIDLIST=(`awk '/[0-9]/ {print $1}' $TEMPFILE`)	# make an array using the first column
PPIDLIST=(`awk '/[0-9]/ {print $2}' $TEMPFILE`)	# 	and the second

SIZE=${#PIDLIST[*]}
K=0
# bypassing the forest using stack which emulates LINKS array. K is the pointer to stack's top
for (( i=0; i<$SIZE; i++ ))
do
    if [[ $K = 0 || ${PPIDLIST[$i]} = ${LINKS[$K-1]} ]]	# new tree or new edge in the tree.
    then
        LINKS[$K]=${PIDLIST[$i]}		# push PID to stack
        K=`expr $K + 1`
    else 					# the chain is complete.
	if [[ $K > 2 ]]				# enough size to print the chain
	then				# reversed formatted output the chain
	    echo ${LINKS[*]} | awk '{ printf $NF; for (i = NF-1; i > 0; --i) printf ":"$i}'
	    echo
	fi
        until [[ $K == 0 || ${PPIDLIST[$i]} == ${LINKS[$K-1]} ]]
        do 					# deleting elements from stack until find a
    						#	parent or tree is end
    	    unset LINKS[$K-1]
    	    K=`expr $K - 1`
        done
	LINKS[$K]=${PIDLIST[$i]}		# push PID to stack
	K=`expr $K + 1`
    fi
done

rm -rf $TEMPFILE				# removing temp file

Запостил: cahekm cahekm, (Updated )

Комментарии (16) RSS

  • В чём разница между = и == в условных операторах типа until и if?
    Ответить
    • Обьясните как работает until, зачем удалять элемент в массиве LINKS когда он равен процессу в массиве PPID?
      Ответить
      • Девочкам алисам это знать не обязательно. Вам прямая дорога в общепит или толстопит-макдональдс.
        Ответить
        • Я, AliceGoth, находясь в здравом уме и твердой памяти, торжественно заявляю: я хакерские журналы читаю, я всё знаю.
          Ответить
          • Слушай, если знаешь, подскажи тогда, как же всё таки пропатчить KDE2 под FreeBSD??
            Ответить
          • Девушка - хакер в треде! Берегись. Все обидчиков взломает. Ничего плохого про неё не пишите. Тссс! Девушки вспылчивые. Всем не поздоровится тогда...
            Ой.
            Уже чувствую, как мой ип начали ддидосить!!!
            Ответить
  • У жeны oкaзaлся любoвник, и я уeхaл, брoсив всё, в дaльнee Пoдмoскoвьe, гдe у мeня былa oднoкoмнaтнaя квaртирa
    Ответить

Добавить комментарий

Где здесь C++, guest?!

    А не использовать ли нам bbcode?


    8