Using grep with test and without using [[ ]]


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using grep with test and without using [[ ]]
# 8  
Old 01-23-2019
Hi wisecracker,


please regard ;; as a fix syntax element to separate "cases". You can't use single semicolons as the "list" (in your man bash excerpt) can consist of several commands separated by ; .
These 2 Users Gave Thanks to RudiC For This Post:
# 9  
Old 01-23-2019
Thanks RudiC...

That makes sense, I can see it in my minds eye now.
Sorry to tie up this thread with that question but it is useful for our new friend to know.

<Thumbs Up>
This User Gave Thanks to wisecracker For This Post:
# 10  
Old 01-23-2019
You could get away without tests, greps, square brackets, ifs, just using case ... esac:


Code:
case "$INT" in
   *[^0-9-]*)   echo no int;;
   -*[^0]*)     echo negative;;
   *[^-0]*)     echo positive;;
   *)           echo null;;
esac

The logics may need to be refined (e.g. to eliminate minus sign in the middle of the string), but should be doable.
This User Gave Thanks to RudiC For This Post:
# 11  
Old 01-23-2019
@RudiC
The standard shell glob only mentions the ! as a negation of the following character set. The ^ is an extension in bash (and maybe some other shells).
Code:
case "$INT" in
   *[!0-9-]*|?*-*)   echo no int;;
   -[0-9]*)     echo negative;;
   [0-9]*)     echo positive;;
   *)           echo null;;
esac

@apmcd47
expr uses BRE that defines \{m,n\}
The GNU-extensions \? and \+ are generally \{0,1\} and \{1,\}
Unfortunately expr needs a work-around for a leading dash in its first argument
Code:
expr X"$INT" : X'-\{0,1\}[0-9]\{1,\}$'

And expr is an external command as well.

@wisecracker
You even posted the man page that mentions the list ;;
A list is a list of commands, separated with newline or ; (as usual).

Last edited by MadeInGermany; 01-23-2019 at 05:36 PM..
These 2 Users Gave Thanks to MadeInGermany For This Post:
# 12  
Old 01-23-2019
@MadeInGermany: Thanks for pointing that out. I double checked before posting; man bash
Quote:
If the first character following the [ is a ! or a ^ then any character not enclosed is matched.
, and I (frequently but not always) test proposals on two systems (Linux and FreeBSD), but I'm afraid doing so on all possible systems / configurations is beyond me.


Thanks as well for extending the logics as I alluded to. Be aware that "-0" is identified as "negative" with your approach..
This User Gave Thanks to RudiC For This Post:
# 13  
Old 01-23-2019
Thanks to everyone!
Code:
#!/bin/bash

INT=9

if grep "^[-0-9]\+$" <<< "$INT"; then
    if test "$INT" -eq 0; then
        echo "INT is zero."
    else
        if test "$INT" -lt 0; then
            echo "INT is negative."
        else
            echo "INT is positive."
        fi
        if test $((INT % 2)) -eq 0; then
            echo "INT is even."
        else
            echo "INT is odd."
        fi
    fi
else
    echo "INT is not an integer." >&2
    exit 1
fi

# 14  
Old 01-24-2019
-0 feels slightly less than 0 Smilie

I have found now:
[!a-z] works in all Bourne-derived shells, only an interactive zsh does history substitution (and needs [^a-z] or "setopt no_bang_hist").
the [^a-z] works in ksh93 and dash-0.5.8 and zsh in addition to the [!a-z].

A curious example:
dash-0.5.8 has got the ^ in addition, but its man page still says
Quote:
The character class may be complemented by making
an exclamation point the first character of the character class.
@Xubuntu56
I do not understand why you want the builtin [[ =~ ]] replaced with grep but introduce the <<< operator that is again specific to bash.
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is it correct? if test grep EOF $a ...

read a if test grep EOF $a then echo yes file else echo no fi (1 Reply)
Discussion started by: iamsumibisht
1 Replies

2. Shell Programming and Scripting

Grep/print/ a test file

cat abc.txt Filename: SHA_AED_Monthly_SNR_20150331.txt.gz Data Format: ASCII with carriage returns and linefeeds Compression: GZIP GZIP Bytes: 36893068 Unzipped Bytes : 613794510 Records: 851310 Record Length: 738 Blocksize: 32472 Filename: SHA_AED_SNR_ChangeLog_20150331.txt.gz Data... (16 Replies)
Discussion started by: dotran
16 Replies

3. Shell Programming and Scripting

If test grep.... always returns 0 status

Hi all. I am trying to compare and filter two files. I have a bigfile.txt of names and ids and a smallfile.txt of ids only. What I am trying to do is use a while read loop to read the ids in the bigfile and then echo the name and id only if the id exists in the small file. Basically, I'm trying to... (5 Replies)
Discussion started by: jameswatson3
5 Replies

4. Shell Programming and Scripting

searching fileextentions (suffix) with grep and/or test

Hello, I have a problem. I will search files on fileextentions (suffix). It can with the command find, but I will do it with the commands grep and/or test. When i start the script I will see all files with that extention (suffix). Can anyone help me, please? Thanks! Regards, Arjan... (4 Replies)
Discussion started by: arjanengbers
4 Replies

5. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

6. Shell Programming and Scripting

grep functions, how to test if succeeded

Hello ...again. I am stuck on this part, I have a loop with processes an operations file. and calls different functions depending on what is in loop, which processes a database file... #so far my add function works as intended add() { ...blah blah; } # delete is kinda working... (13 Replies)
Discussion started by: gcampton
13 Replies

7. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

8. Shell Programming and Scripting

Using grep inside a test

Hi, I want to use grep inside a test statement, but I am getting an error message. Two variables testvarNum=5 testvarNonNum=x echo $testvarNum | grep * The result of this is as follows: 5 However, when I try the following (i.e. to test if the variable is numeric or non-numeric):... (3 Replies)
Discussion started by: dkieran
3 Replies

9. UNIX for Dummies Questions & Answers

Unix grep/test command

Hello, i have a script which checks if the user entered 8 numeric characters in the form of YYYYMMDD (birth date). If the user entered any non numeric characters, an error will be displayed: # Check to see if the 8 characters are all numbers # If not show error essage # And prompt user... (4 Replies)
Discussion started by: netmaster
4 Replies

10. Shell Programming and Scripting

Using grep in a test/if statement

Okay, well this is more or less my first attempt at writing a shell script. Anyways, here's my code: cd ${PATH} if then rm ${FILE} ./anotherScript else exit 1 fi exit 1 Anyways, it's a pretty simple script that is supposed to search for the... (4 Replies)
Discussion started by: cbo0485
4 Replies
Login or Register to Ask a Question