0

I want my /etc/passwd file to be as an array like this

[user_id] => [home_directory]

e.g.

[0] => "/root" [1000] => "/home/user1" [1001] => "/home/user2" 

so after that, I can set a condition that if user_id is greater than 999 (it mean usual users) then check if in .htaccess file of its directory the values of memory_limit and display_errors are exists or not.

for the aforesaid goal, I ran the command bellow

cat /etc/passwd | awk -F':' '{print $4,$6}'

it shows all

user_id home_directory

but I don't know how to put them in the array I mentioned before.

because some users in /etc/passwd have no home directory my array keys get incorrect values.

please help.

thanks in advance.

1
  • thanks Roraima, that really helps :)
    – Mahdi
    CommentedJun 17, 2020 at 8:46

2 Answers 2

0

You can use a shell array

# Associative array p declare -A p=() # Populate the array (x is a placeholder that we otherwise ignore) while IFS=: read -r x x uid x x home x do p[$uid]=$home done </etc/passwd # Demonstrate the data for uid in "${!p[@]}" do printf "[%s]\t=> \"%s\"\n" "$uid" "${p[$uid]}" done 

Example output

[1000] => "/home/pi" [0] => "/root" [1] => "/usr/sbin" [33] => "/var/www" 
    0

    Try this

    IFS=$'\n' user=( $(awk -F':' '{print $4}' /etc/passwd | tr '\n' ' ') ) homeDir=( $(awk -F':' '{print $6}' /etc/passwd |tr '\n' ' ')) unset IFS 
    1

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.