need help deciphering this if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help deciphering this if statement
# 1  
Old 12-06-2010
need help deciphering this if statement

I'm going through my bash book and came across this if statment.
Code:
if [ -n "$(echo $1|grep '^-[0-9][0-9]*$)" ]; then

the book says that the grep expression means "an initial dash followed by a digit" (which I understand) "optionally followed by one or more digits" That's the part I can't figure out -- I know the * is a wildcard symbol, but the $ I'm only familiar with in displaying the values of variables and the like, so how do these symbols mean what they do in this expression? If you could break it down for me I'd really appreciate it. Do these symbols have another meaning that I'm not aware of?
# 2  
Old 12-06-2010
It probably is supposed to mean "if $1 is a negative number, then", but a single quote is missing after the $ sign
It is equivalent to this:
Code:
if [ $1 -lt 0 ]; then

or this if $1 may contain values other than a number
Code:
if [ $1 -lt 0 ] 2>/dev/null; then


Last edited by Scrutinizer; 12-06-2010 at 02:01 PM..
# 3  
Old 12-06-2010
Quote:
Originally Posted by Straitsfan
I know the * is a wildcard symbol, but the $ I'm only familiar with in displaying the values of variables and the like, so how do these symbols mean what they do in this expression?
$1 is the 1st positional parameter passed to your script.
# 4  
Old 12-06-2010
actuall I did forget the single quote, but it isn't referring to a negative number. It's part of a sample script. I should have included the whole thing.

Code:
if [ -n "$(echo $1 | grep '^-[0-9][0-9]*$')" ]; then
    howmany=$1
    shift
elif [ -n "$(echo $1 | grep '^-')" ] ;then
     print 'usage: highest [-N] filename'
     exit 1
else
     howmany="-10"
fi
 
filename=$1
sort -nr $filename |head $howmany

what I wanted to know was how the * and $ mean what my book says it means, because I don't see it (particularly the dollar sign at the end of the grep expression; I know what it means in reference to the positional parameter).
# 5  
Old 12-06-2010
$ marks the end of a line (or string) if used at the end of a regular expression (match-end-of-line operator).
* means zero or more occurrences of the preceding character (repetition operator), in this case a digit ([0-9])
# 6  
Old 12-06-2010
Maybe this can bring some lights in the dark too:

POSITIONAL PARAMETER REFERENCE
# 7  
Old 12-06-2010
So just to be clear, the grep expression means "look at the first positional prameter and see if it begins with a -. If it does, then see if it's followed by a digit from 0 to 9. If that's the case, then see if there are or not any other digits after it until the end of the line, with no letters or symbols in between" ?

Also, am I correct in assuming that these symbols have these charateristics only in a grep expression?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Deciphering AWK code

Dear experts, I am a relative novice in the Unix and came across a very useful code that I regularly use for my research blindly. I am wondering if any of the professional members could kindly briefly explain to me what the code actually does? Many thanks in advance The script is awk... (4 Replies)
Discussion started by: arsalane
4 Replies

2. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

3. Programming

Deciphering a tag character string

I have a string, eg 7f30.3 and I want to store things in the following way npos = 7 decform = true width = 30 ndp = 3 I need to read each character one by one. I am coding in fortran but I can try to code it should answer be given in C in the above way. (2 Replies)
Discussion started by: kristinu
2 Replies

4. Programming

Some help with Perl please (deciphering)

I am trying to simplify the coding in a script I was given, but it was written 7-10 years ago and is pretty complicated. below is a tidbit, if someone can break it down for me I would appreciate it. sub ParseText { my ($line, $key, $value, $sub, $script); foreach $line (@_)... (0 Replies)
Discussion started by: callyvan
0 Replies

5. UNIX for Dummies Questions & Answers

Need help deciphering this

I'm reading about command substitutions and came across this little function in my book: function lsd { date=$1 ls -l |grep -i "^.\{42\}$date"|cut -c55- } it's a little example which is supposed to select files by modification date, given as an argument to the function. I... (3 Replies)
Discussion started by: Straitsfan
3 Replies

6. Shell Programming and Scripting

Deciphering strings or variable values

Hi, I have a script at the moment of which reads in simply what the latest version is within a folder i.e. v001, v002, v003 etc and then stores this latest version in a variable i.e. $LATEST would echo v003. I have then cut this string so that I only consider the 003 part. I would then like to... (3 Replies)
Discussion started by: cyberfrog
3 Replies

7. UNIX for Dummies Questions & Answers

Deciphering the Code

Hi people I am trying to learn this code and see how it relates to the old DOS days. I have a line of code that I am not sure what the first part does. Any help will be greatly appreciated. It is from a Save command that is used to backup files to a directory. It goes like this if ;then... (10 Replies)
Discussion started by: coyote1967
10 Replies

8. Shell Programming and Scripting

Help deciphering FTP get perl script

I found this very useful perl script that will check a remote ftp server, search for files of a specific time and get them. When I run the script it works, but it gave me the following error: Couldn't get filename_12-13-07.txt Bad file number What in this script would cause this? I know... (2 Replies)
Discussion started by: bbbngowc
2 Replies

9. Shell Programming and Scripting

Help deciphering script

There are files on a remote server with the file name ending in "mm-dd-yy.txt". The script I am running is: mls "Daily_Service_Text_File_*" /my/local/dir/Filelisting.txt nawk -F_ -f file.awk /my/local/dir/Filelisting.txt | sort -k1n | cut -f2- | tail -1 It worked up too "12-31-07.txt" but... (3 Replies)
Discussion started by: bbbngowc
3 Replies
Login or Register to Ask a Question