954,135 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help!need Help reading the bash script

Hi..
im a newbie in shell scripting, and also new in this forum..
i understand the command below is just reading from /etc/passwd
and getting whatever in field 1 and 3 (which are username and userID)

after the cut the format will be
username:uid
my guess,its making the array with the index in respect to its userID

but i dont understand the expression
{i#*:}]=${i%: *}
could somebody please explain how to read those...
and what those will evaluate to...
and how to work with things like those(i dont think those're regex-i have no clue)..

=========================================================
for i in $(cut -f 1,3 -d: /etc/passwd) ; do array[${i#*:}]=${i%: *}done

bergy_nj
Newbie Poster
7 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 
for i in $(cut -f 1,3 -d: /etc/passwd)  
do 
  echo "${i#*:}" "${i%:*}" 
done


This lets you see what is going on.

${i#*:}


is parameter substitution- returns the value just before the colon - a number that is the user id.

${i%:*}


returns the username, so:
arr[number] = name

${i%:*}


was changed from

${i%: *}


which doesn't work, by the way.
Try man (or info) for bash or ksh. This is called parameter substitution using a POSIX shell. It finds substrings.

jim mcnamara
Junior Poster
180 posts since May 2004
Reputation Points: 62
Solved Threads: 10
 

bash's parameter substitution is a good feature to have, but it has ugly syntax. for what you want to do, you can use awk

awk -F":" '{print $3,$1}' /etc/passwd
ghostdog74
Junior Poster
156 posts since Apr 2006
Reputation Points: 75
Solved Threads: 44
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You