Wednesday, May 15, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 1915  / 2 Years ago, sun, august 28, 2022, 6:07:39

I wrote this script to store some information about my machine such as the users and the running processes .



I tried to store the retrieved data into an array. To test array I printed the length of an array as follows :



#!/bin/bash


###################################################################################

openFilesCount=$(lsof -Fn -u teeba| sort | uniq | grep /home | wc -l);

openPortsCount=$(lsof -Fn -u teeba| sort | uniq | grep /home | wc -l);

readingTime=$(date +%Y-%m-%d_%T);

usersArr=$(awk -F: '$3 >= 1000 && $1 != "nobody" {print $1}' /etc/passwd);

pidsArr=$(ps axo pid);

###################################################################################

echo "${#usersArr[@]}";


The output is 1 ... although the users are three ? do I need to split the retrieved data on "
" for example before store it in the array ? if yes , how ?


More From » 12.04

 Answers
0

You can use,



usersArr=($(awk -F: '$3 >= 1000 && $1 != "nobody" {print $1}' /etc/passwd) )

for i in "${usersArr[@]}"
do
echo "$i"
done

[#26993] Monday, August 29, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
antebrow

Total Points: 291
Total Questions: 135
Total Answers: 117

Location: New Caledonia
Member since Thu, Mar 23, 2023
1 Year ago
;