Bash - Exporter tous les utilisateurs

Voici un script qui permet de récupérer la liste de tous les utilisateurs Linux en parsant le fichier /etc/passwd.

#!/bin/bash

# "/etc/passwd" data in format "LOGIN:UID:GID:DESCRIPTION".
accounts=$(awk -F ':' '{print $1";"$3";"$4";"$5}' /etc/passwd | grep -v "^#" | sort)

result=""

while IFS= read -r account; do
login=$(echo "$account" | awk -F ';' '{print $1}')

uid=$(echo "$account" | awk -F ';' '{print $2}')

gid=$(echo "$account" | awk -F ';' '{print $3}')

# Description data in format "Last_Name First_Name - Job - Date, Office, Phone".
description=$(echo "$account" | awk -F ';' '{print $4}')

short_description=$(echo "$description" | awk -F ',' '{print $1}')
name=$(echo "$short_description" | awk -F ' - ' '{print $1}')
job=$(echo "$short_description" | awk -F ' - ' '{print $2}')
date=$(echo "$short_description" | awk -F ' - ' '{print $3}')

office=$(echo "$description" | awk -F ',' '{print $2}')
phone=$(echo "$description" | awk -F ',' '{print $3}')

echo "$login;$uid;$gid;$name;$job;$date;$office;$phone"
done <<< "$accounts"

Le rendre exécutable puis lancer la commande

$ . monscript.sh > monfichier.csv

documentation

IFS : https://stackoverflow.com/questions/26479562/what-does-ifs-do-in-this-bash-loop-cat-file-while-ifs-read-r-line-do

> Partager <