Mailinglist Archive: opensuse (686 mails)

< Previous Next >
Re: [opensuse] shell script help
On 7/7/2013 9:09 PM, Felix Miata wrote:
Anyone know a good source of examples for dweebs who can't remember from
one scripting writing session to the next anything new learned to get a
task done, and requires examples rather than reading man pages to
understand anything unfamiliar?

One of my favorites was always "shelldorado" if only because of the great name :) www.shelldorado.com

I have a script containing among other things the following:

grep 'using VT' /var/log/Xorg.0.log
head -n7 /var/log/Xorg.0.log
grep Output /var/log/Xorg.0.log | egrep -v 'disconnected|no monitor'

This works fine as long as it's run on a default X session, but not
otherwise. I need to substitute for each "0" in the above strings the
value of the actual display in use, which happens to be the last of the
two visible characters of $DISPLAY. I tried to figure out if I could
somehow apply the cut command to $DISPLAY, but got nowhere via its man
page. Is what I want to do a for loop task? Can anyone here help with
this seemingly simple goal?

In bash, (this syntax only works in bash or real at&t ksh93 and maybe zsh):

echo ${DISPLAY##*:}

What that did was delete everything up to and including, the last ":" in $DISPLAY, and show you whatever was left. The contents of DISPLAY are not changed. This is just an expansion filter. It's like reading something through a filter. The original thing you read isn't changed by the filter, you just see something different than the source.

A single # would have deleted up to and including the first :, which is often the same thing if there is only one, but the more correct thing is to grab everything after the last :, even if there may just happen to be only one of them usually.

The *: is the pattern of what to delete

In "man bash" search (hit the "/" key) for "parameter#word". Of course, see the whole Parameter Expansion section while you're there. Lot's of handy goodies. Like you often don't really need the dirname or basename or cut or tr or even sed commands.

--
bkw
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
To contact the owner, e-mail: opensuse+owner@xxxxxxxxxxxx

< Previous Next >
References