Help!need Help reading the bash script

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2007
Posts: 7
Reputation: bergy_nj is an unknown quantity at this point 
Solved Threads: 0
bergy_nj bergy_nj is offline Offline
Newbie Poster

Help!need Help reading the bash script

 
0
  #1
May 24th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: Help!need Help reading the bash script

 
0
  #2
May 29th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Help!need Help reading the bash script

 
0
  #3
May 29th, 2007
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC