For exercise I need to create a script that takes 2 parameters as input,
<n>
an integer value<path>
the path to a directory
I need to create as many directories in path as there are users that have at least <n>
processes running.
The files have this formatting <pid>.txt
and must contain ppid, time and command
#! /bin/bash
if [ $# -lt 2 ]
then
echo "Hai inserito $#/2 parametri"
echo "<n><path>"
exit 1
fi
if [ "$1" -lt 0 ]
then
echo "errore: Il valore inserito non può essere negativo"
exit 1
fi
if ! [ -d "$2" ]
then
echo "errore: Il path indicato non è una directory"
exit 1
fi
OCCURRENCE=$1
PAT=$2
users=($(ps -Af | awk '{print $1}' | sort | uniq ))
for user in "${users[@]}"
do
processes=$(ps -Af | awk -v user="$user" '$1==user'|wc -l)
if [ "$processes" -gt "$OCCURRENCE" ]
then
mkdir $PAT/$user
ps -Af | awk -v user="$user" -v path="$PAT/$user" '$1=="user" {print $2,$7,$8 >>"$path/"$1".txt" }'
fi
done
The problem is that the files with the contents within the respective directories are not created, but the program only correctly creates the directories of the users that have more than <n>
processes.
Is there any error in the awk command? Any better way to perform this task?