Negating shell interpretation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Negating shell interpretation
# 8  
Old 01-22-2012
Try:
Code:
#!/bin/ksh

final="test bla bla"
line="01 01 * * 5 /home/user01/bin/start"

if echo "$line" | grep 'start' >/dev/null 2>&1
then
  start="$final"
fi

echo "$start"

Or without external programs...

Code:
#!/bin/ksh

final="test bla bla"
line="01 01 * * 5 /home/user01/bin/start"

case $line in
  *start*) start="$final"
esac

echo "$start"


Last edited by Scrutinizer; 01-22-2012 at 01:28 PM..
# 9  
Old 01-22-2012
Why use grep at all? Take advantage of Kshell's pattern matching:

Code:
#!/usr/bin/env ksh

final="test bla bla"
line="01 01 * * 5 /home/user01/bin/start"

if [[ $line == *"start"* ]]
then
  start="$final"
fi

Depending on how this is being used, and thus the number of grep processes that are avoided, this is much more efficient.
This User Gave Thanks to agama For This Post:
# 10  
Old 01-22-2012
Quote:
Originally Posted by agama
Why use grep at all? Take advantage of Kshell's pattern matching:

Code:
#!/usr/bin/env ksh

final="test bla bla"
line="01 01 * * 5 /home/user01/bin/start"

if [[ $line == *"start"* ]]
then
  start="$final"
fi

Depending on how this is being used, and thus the number of grep processes that are avoided, this is much more efficient.

Why limit it to ksh? Scrutinizer's case statement (which also avoids external commands) will work in all Bourne-type shells.
This User Gave Thanks to cfajohnson For This Post:
# 11  
Old 01-22-2012
Quote:
Originally Posted by cfajohnson

Why limit it to ksh? Scrutinizer's case statement (which also avoids external commands) will work in all Bourne-type shells.
Right. I missed the case example and specifically mentioned kshell because the original post indicated kshell was being used.
# 12  
Old 01-22-2012
Hey guys,

Thanks for all the input so far. It's been really helpful. It looks like this line is working currently:
Code:
if [[ $line == *"start"* ]]


In this following snippet, line 9 only needs one set of square brackets but lines 13 and 16 needs two set of square brackets. Any thoughts?
Code:
     8	function match {
     9	    temp=$( echo "$line" | egrep '(start|stop)' )
    10	    
    11	    if [ ! -z "$temp" ]
    12	    then
    13		if [[ $( echo "$temp" | grep 'start') ]]
    14		then
    15		    start="$temp"
    16		elif  [[ $( echo "$temp" | grep 'stop') ]]
    17		then
    18		    end="$temp"
    19		fi
    20	    fi
    21	}

# 13  
Old 01-22-2012
The open square bracket [ is a symbolic link to the test command and gives the appearance of wrapping the expression in parenthesis-like format that most programmers are used to. The double bracket, is a part of the shell syntax.

The expression that appears between single brackets is given to the test command while the expression inside of double brackets is interpreted by the shell. The shell interpretation supports the evaluation of the command exit code like you want to do, while the test command does not and that is why you need the double brackets where you've discovered that you do.

In my humble opinion, there is rarely a case where single brackets (the test command) are needed; using double bracketed expressions is much more efficient especially when loops are involved.

Last edited by agama; 01-22-2012 at 07:06 PM.. Reason: typo
# 14  
Old 01-22-2012
Quote:
Originally Posted by sprucio
Hey guys,

Thanks for all the input so far. It's been really helpful. It looks like this line is working currently:
Code:
if [[ $line == *"start"* ]]


In this following snippet, line 9 only needs one set of square brackets but lines 13 and 16 needs two set of square brackets. Any thoughts?
Code:
     8	function match {
     9	    temp=$( echo "$line" | egrep '(start|stop)' )
    10	    
    11	    if [ ! -z "$temp" ]
    12	    then
    13		if [[ $( echo "$temp" | grep 'start') ]]
    14		then
    15		    start="$temp"
    16		elif  [[ $( echo "$temp" | grep 'stop') ]]
    17		then
    18		    end="$temp"
    19		fi
    20	    fi
    21	}


You don't need grep, and you don't need double (or single) brackets.

Use case:
Code:
match() {
  case $line in
    *start*) start=$line ;;
    *stop*)  end=$line ;;
  esac
}

---------- Post updated at 06:12 PM ---------- Previous update was at 06:06 PM ----------

Quote:
Originally Posted by agama
The open square bracket [ is a symbolic link to the test command and gives the appearance of wrapping the expression in parenthesis-like format that most programmers are used to.

The open square bracket [ is a shell builtin command, and it is synonymous with test:
Code:
$ type test [   
test is a shell builtin
[ is a shell builtin

Quote:
In my humble opinion, there is rarely a case where single brackets (the test command) are needed; using double bracketed expressions is much more efficient especially when loops are involved.

IMNSHO, there is rarely a case where double brackets are needed (I never use them).

The double brackets are not part of the POSIX definition of the shell, and thus are less portable.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sql multi line comment /* shell interpretation issue

Greetings Experts, I do have some basic knowledge of Unix. The task I am trying to do through shell script is to generate the view script for all of the tables which is in YYYYMMDD format (I assume I am on Ksh). I have certain tables that ends in YYYYMMDD format (eg: tbl_20150630) For each... (1 Reply)
Discussion started by: chill3chee
1 Replies

2. AIX

lspath output interpretation

On my VIo I see the following for my disks: $ lspath | grep hdisk6 Enabled hdisk6 fscsi0 200600a0b82193f7,4000000000000 Enabled hdisk6 fscsi0 200700a0b82193f7,4000000000000 Enabled hdisk6 fscsi2 200600a0b82193f8,4000000000000 Failed hdisk6 fscsi2 200700a0b82193f8,4000000000000 $ lspath |... (8 Replies)
Discussion started by: petervg
8 Replies

3. UNIX for Dummies Questions & Answers

No Space Message Interpretation

Hi, I get the message NOTICE HTFS :No Space on dev hd (1/104), What does (1/104) mean? Is there any link, I can get material on understanding unix message log? thanks. (4 Replies)
Discussion started by: scomrade
4 Replies

4. Shell Programming and Scripting

perl negating Regular Expression

hi all, I have this particular piece of code perl -pi -e 's#\&amp\;(\w\W*\w+\;)#\&$1#gm' filename.txt the (\w\W*\w+\;) part should not contain an "&". That means that if a string like &xxxxx&ecute; comes i wount be replacing the amp;. thanks Jathin (17 Replies)
Discussion started by: jathin12
17 Replies

5. AIX

interpretation of sar

hello with a sar i have this result: System configuration: lcpu=48 ent=4.00 14:06:37 %usr %sys %wio %idle physc %entc 14:06:39 26 9 3 62 1.63 40.7 14:06:41 26 9 3 63 1.58 39.4 14:06:43 ... (0 Replies)
Discussion started by: pascalbout
0 Replies

6. UNIX for Advanced & Expert Users

SAR -b interpretation

I have used SAR -b to get some Unix cache / buffer metrics and the results are confusing me a bit. The pread/s & pwrit/s are showing 0. However the lread/s and lwrit/s are showing figures. I note also that the bread/s and bwrit/s are showing figures. I believe that pread/s and pwrit/s is not... (3 Replies)
Discussion started by: jimthompson
3 Replies

7. UNIX for Dummies Questions & Answers

Interpretation of the uptime command

Hi there, do someone have detailed information how to interpret the uptime command or rather which values can be called normal? (i know what the information means, but i have no idea if these values are ok or to high: 3:02pm an 13:53, 2 Benutzer, Durchschnittslast: 10,06, 12,05, 13,00) ... (5 Replies)
Discussion started by: odin1999
5 Replies

8. UNIX for Dummies Questions & Answers

Prevent bash from interpretation :

I am using bash shell; my requirement is to run a long command. Now I have split this long command into a number of shell variables. Some of these shell variables contain special character ':' At the end, when the intended long command is executed as a series of small shell variables the ':'... (7 Replies)
Discussion started by: uday
7 Replies

9. UNIX for Dummies Questions & Answers

shell interpretation

I executed the following command in the korn shell: $ variable1="qwerty" ls | sort and the shell executed the 'ls | sort' command. I would have expected an error message from the shell, but instead of that the shell ran the 'ls | sort' command and didn't realize the variable assignement. ... (1 Reply)
Discussion started by: PhilippeCrokaer
1 Replies
Login or Register to Ask a Question