|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Bash question
I have some lines of code that will not port from bash version 3.1 to bash 2.0 something.
I need to know how I could make this work in earlier versions of bash shell that did not have the regex bind operator: Code:
if [[ "$IT" =~ "s" ]] && [[ ! "$INIT" =~ "$TD=" ]]
__________________
Bugs that go away by themselves come back by themselves Never take life seriously, Nobody gets out alive anyway. |
|
#2
|
||||
|
||||
|
Bump.
I'm seriously at a loss here. Are there any bash wizards in the audience .. perhaps any circa *before version 3.0 of Bash* wasn't that long ago .... Need to know how *test* with regular expression? Ive tried using grep with little success, thought I might use that and then test the variable but alas. Anyone? Thanks in advance. |
|
#3
|
||||
|
||||
|
Quote:
Could you make use of substring extraction? |
|
#4
|
||||
|
||||
|
Well .. lets see ..
The first test does a match on some time .. that might work because there wont ever be a 00:00s only 0:00s (this is a dicing and matching of the data received from the `w` command). The second match .. probably not .. its a space separated list of custom data (fields from the command reorganized around a `=` with a little bit of AwK) Problem there is I dont know the order, so I wouldn't know the length or position of the current device im looking at (I broke it down by tty numbers for uniqueness). Here is some of the script to give an idea: Code:
while : ; do
INP=`w -h | awk '{ print $2"="$5 }'`
for IDX in $INP ; do
TD=`echo $IDX | awk -F'=' '{ print $1 }' | sed 's/[^0-9]//g'`
IT=`echo $IDX | awk -F'=' '{ print $2 }'`
if [[ "$IT" =~ "s" ]] && [[ ! "$INIT" =~ "$TD=" ]] ; then
ST=`date '+%s'`
INIT="$INIT $TD=$ST"
fi
....
|
|
#5
|
||||
|
||||
|
Here is the appropriate conversion .. this ports well:
This: Code:
if [[ "$IT" =~ "s" ]] && [[ ! "$INIT" =~ "$TD=" ]] Becomes this: Code:
if [ `echo "$IT" | grep -c "s"` ] && [ ! `echo "$INIT" | grep -c "$TD="` ] Test requires a binary expression so grep -c gives you a positive integer when the match is made, zero if it isnt. |
|
#6
|
||||
|
||||
|
And to continue this conversation with myself, the previous lines of code were not actually correct.
`test` will return true if its only argument is not-null. So when the result of the grep returned zero the test still returned true because 0 != "" .. so it must be written this way: Code:
if [ `echo "$IT" | grep -c "s"` -gt 0 ] && [ `echo "$INIT" | grep -c "$TD="` -lt 1 ] |
![]() |
| Viewing: Dev Shed Forums > Web Site Management > Scripts > Bash question |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|