943,518 Members | Top Members by Rank

Ad:
May 24th, 2007
0

Help!need Help reading the bash script

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bergy_nj is offline Offline
7 posts
since May 2007
May 29th, 2007
0

Re: Help!need Help reading the bash script

Shell Scripting Syntax (Toggle Plain Text)
  1. for i in $(cut -f 1,3 -d: /etc/passwd)
  2. do
  3. echo "${i#*:}" "${i%:*}"
  4. done

This lets you see what is going on.
Shell Scripting Syntax (Toggle Plain Text)
  1. ${i#*:}
is parameter substitution- returns the value just before the colon - a number that is the user id.
Shell Scripting Syntax (Toggle Plain Text)
  1. ${i%:*}
returns the username, so:
arr[number] = name

Shell Scripting Syntax (Toggle Plain Text)
  1. ${i%:*}
was changed from
Shell Scripting Syntax (Toggle Plain Text)
  1. ${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.
Last edited by jim mcnamara; May 29th, 2007 at 1:00 pm.
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004
May 29th, 2007
0

Re: Help!need Help reading the bash script

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
Shell Scripting Syntax (Toggle Plain Text)
  1. awk -F":" '{print $3,$1}' /etc/passwd
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: help... for LINUX/UNIX users only
Next Thread in Shell Scripting Forum Timeline: Swaping Files





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC