You are not logged in.

#1 2008-07-29 17:06:42

Onwards
Member
From: Pakistan
Registered: 2007-04-18
Posts: 108

[SOLVED] /etc/rc.d/functions: Need to ask about a line !!

# strip the rows number, we just want columns
STAT_COL=${STAT_COL##* }

STAT_COL##* uses REGULAR EXPRESSIONS right.

Need an answer as Yes or No. I would dig out the rest of it myself smile


Thanx in advance !!

Last edited by Onwards (2008-07-29 17:19:26)

Offline

#2 2008-07-29 17:15:45

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: [SOLVED] /etc/rc.d/functions: Need to ask about a line !!

No. Considering you want to research this yourself: http://tldp.org/LDP/abs/html/
The terminology you want is "globbing"

Offline

#3 2008-07-29 17:18:58

Onwards
Member
From: Pakistan
Registered: 2007-04-18
Posts: 108

Re: [SOLVED] /etc/rc.d/functions: Need to ask about a line !!

THANKYOU for the quick reply and the link, of course !!

Offline

#4 2008-07-29 19:09:23

Onwards
Member
From: Pakistan
Registered: 2007-04-18
Posts: 108

Re: [SOLVED] /etc/rc.d/functions: Need to ask about a line !!

OK a bit of explanation for the late-comers smile


My question was about a line in /etc/rc.d/functions script. Here are the dubious lines...

STAT_COL=$(/bin/stty size)
# strip the rows number, we just want columns
STAT_COL=${STAT_COL##* }

As the commented out line says, we just want number of columns. But the output of stty size is like "40 143" i.e. "40 rows AND 143 cols". So our job is to strip the no. of rows from this string i.e. we want to reduce it to "143".

STAT_COL is a shell variable and we can use it as an array. It would be treated as an array containing a single element. To use it as an array, just embed it inside ${variable}.

Now we 've got to do Substring Removal on it, longest match from the front of the string in fact. "##* " would match anything followed by a space to be removed from the string. Th result is stord back into STAT_COL. Thus we are left with just the no. of columns.

For further details have a look at http://tldp.org/LDP/abs/html/arrays.html.

Last edited by Onwards (2008-07-29 19:11:11)

Offline

#5 2009-06-23 15:12:44

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: [SOLVED] /etc/rc.d/functions: Need to ask about a line !!

This explanation is mostly accurate. However this is not correct:

Onwards wrote:

STAT_COL is a shell variable and we can use it as an array. It would be treated as an array containing a single element. To use it as an array, just embed it inside ${variable}.

${STAT_COL} is not an array. It's an ordinary simple zero-dimensional variable. The only role that the {}s play is to let the shell's parser interpret what you're writing. If you just wrote $STAT_COL*<space>, the parser would see that as $STAT_COL followed by * followed by <space>. Instead you want the shell to see a single token: not $STAT_COL but rather <the result of chopping the glob pattern "* " off of the start of $STAT_COL>. That's what ${STAT_COL#* } gives you.

To interpret STAT_COL as an array, you'd instead have to write this:

STAT_ARRAY=( $STAT_COL )    # it's important here not to put ""s around $STAT_COL
STAT_ROWS=${STAT_ARRAY[0]}
STAT_COLS=${STAT_ARRAY[1]}

---which the authors of /etc/rc.d/functions might also reasonably have done.

Offline

Board footer

Powered by FluxBB