-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18_largest_username.sh
36 lines (30 loc) · 1006 Bytes
/
18_largest_username.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<<Documentation
NAME : V. Karthikeyan
DATE : 15.05.2021
DESCRIPTION : Display the longest and shortest user-names on the system.
INPUT : ./18_largest_username.sh
OUTPU : The Longest Name is : gnome-initial-setup
The Shortest Name is : lp
Documentation
#!/bin/bash
arr=(`cut -d ":" -f 1 /etc/passwd`) #store the usernames in the system in the array "arr"
len=${#arr[@]} #store the length of the array "arr"
s_size=${#arr[0]} #initialise the values
small=${arr[0]}
l_size=${#arr[0]}
large=${arr[0]}
for i in `seq $((len-1))`
do
if [ ${#arr[i]} -gt $l_size ] #size of arr[i] element greater than previous large element-->store arr[i] as large
then
l_size=${#arr[i]}
large=${arr[i]}
fi
if [ ${#arr[i]} -lt $s_size ] #size of arr[i] element less than previous small element-->store arr[i] as small
then
s_size=${#arr[i]}
small=${arr[i]}
fi
done
echo "The Longest Name is : $large"
echo "The Shortest Name is : $small"