Query on decision making...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Query on decision making...
# 1  
Old 11-29-2010
Query on decision making...

Code:
is_number()
 {
         echo $1|egrep '^[1-9][0-9]*$' 2>&1 1>/dev/null
         return $?
 }

why the following snippet always give an output as "no" and never "yes" whatever the parameter I give to function is_number?
Code:
if [[ `is_number 0100` ]]; then echo yes; else echo no; fi

In addition, the function is_number() is working fine as per the below command line:
Code:
linux-hhaq:/apps/informatica/current/pmserver/tes/scripts # is_number 100
linux-hhaq:/apps/informatica/current/pmserver/tes/scripts # echo $?
0
linux-hhaq:/apps/informatica/current/pmserver/tes/scripts # is_number 0100
linux-hhaq:/apps/informatica/current/pmserver/tes/scripts # echo $?
1
linux-hhaq:/apps/informatica/current/pmserver/tes/scripts #


Last edited by Scott; 11-29-2010 at 09:42 AM.. Reason: Code tags, please...
# 2  
Old 11-29-2010
Lose the brackets and backticks completely...
Code:
if is_number 0100; then echo yes; else echo no; fi

# 3  
Old 11-29-2010
That's right, but why [[ ]] does not support `` ?

---------- Post updated at 09:31 AM ---------- Previous update was at 09:16 AM ----------

bro, what if I need to put multiple conditions in an if statement without using the brackets?
like:
if is_number $3 || $# -ne 3

Quote:
Originally Posted by JerryHone
Lose the brackets and backticks completely...
Code:
if is_number 0100; then echo yes; else echo no; fi

# 4  
Old 11-29-2010
Quote:
Originally Posted by biglau
That's right, but why [[ ]] does not support `` ?
As your function has all its outputs redirected to /dev/null, it outputs nothing.
so the test [[ `is_number 0100` ]] is interpreted as [[ ]] with nothing inside.

By the way, the function could be written: is_number() { echo $1 | egrep -q '^[1-9][0-9]*$'; }
  • The -q (quiet) option removes output
  • The return code of the function is the return code of the last command in it

---------- Post updated at 15:39 ---------- Previous update was at 15:35 ----------

Quote:
Originally Posted by biglau
bro, what if I need to put multiple conditions in an if statement without using the brackets?
like:
if is_number $3 || $# -ne 3
Tthe first part doesn't need test (where the brackets stand for) bur the second, yes. Youi should write
Code:
if is_number $3 || [ $# -ne 3 ]
# or using arithmetic evaluation
if is_number $3 || (( $# != 3 ))

# 5  
Old 11-29-2010
and the following test in bash:
can anyone could explain the differences of the behaviors?

linux-hhaq:/apps/informatica/current/pmserver/tes/scripts # if [[ `echo` ]]; then echo yes; else echo no; fi
no
linux-hhaq:/apps/informatica/current/pmserver/tes/scripts # if [[ `echo aa` ]]; then echo yes; else echo no; fi
yes
linux-hhaq:/apps/informatica/current/pmserver/tes/scripts # if [[ `function_not_defined` ]]; then echo yes; else echo no; fi
yes
linux-hhaq:/apps/informatica/current/pmserver/tes/scripts # function_return1()
> {
> return 1
> }
linux-hhaq:/apps/informatica/current/pmserver/tes/scripts # if [[ `function_return1` ]]; then echo yes; else echo no; fi
no
linux-hhaq:/apps/informatica/current/pmserver/tes/scripts # function_return0() { return 0; }
linux-hhaq:/apps/informatica/current/pmserver/tes/scripts # if [[ `function_return0` ]]; then echo yes; else echo no; fi
no
# 6  
Old 11-29-2010
linux-hhaq:/apps/informatica/current/pmserver/tes/scripts # if [[ `echo` ]]; then echo yes; else echo no; fi
no
echo returns nothing

linux-hhaq:/apps/informatica/current/pmserver/tes/scripts # if [[ `echo aa` ]]; then echo yes; else echo no; fi
yes
echo returns 'aa'

linux-hhaq:/apps/informatica/current/pmserver/tes/scripts # if [[ `function_not_defined` ]]; then echo yes; else echo no; fi
yes
It returns an error on standard output (i redirected 2>/dev/null and got an output !?)
linux-hhaq:/apps/informatica/current/pmserver/tes/scripts # function_return1()
> {
> return 1
> }
linux-hhaq:/apps/informatica/current/pmserver/tes/scripts # if [[ `function_return1` ]]; then echo yes; else echo no; fi
no
It echoes NOTHING or don't use backticks !
linux-hhaq:/apps/informatica/current/pmserver/tes/scripts # function_return0() { return 0; }
linux-hhaq:/apps/informatica/current/pmserver/tes/scripts # if [[ `function_return0` ]]; then echo yes; else echo no; fi
no
It echoes NOTHING or don't use backticks !

---------- Post updated at 16:01 ---------- Previous update was at 15:56 ----------

When You use backticks (it's better to use $() which can be nested and is more readable), you evaluate the ouput of the function (what it echoes on stdout) but not the return code.
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Handling decision making logic

------- Output Screen -------- Choose the option ----------------- 1.Input 2.Output 3.CFT Uniq x.Exit ----------------- 2 Enter the Output flow IDF PBL5572U Enter the Output existing flow IDF PBL5198H sf_PBL5572U.cmd file exist in invalid path or doesn't exist (0 Replies)
Discussion started by: Vijaykannan T
0 Replies

2. Shell Programming and Scripting

Regarding a query on making changes to a running script

Hello All, Greetings !! I have a query here to all is as follows: Question: Let's say we are running a script in a UNIX box and we have opened an another session and then made changes in script of some statements NOT to be print some values(just an example) so when I am monitoring the... (5 Replies)
Discussion started by: RavinderSingh13
5 Replies

3. Shell Programming and Scripting

shell script with decision making

Hi all I need help for the issue below. I need to create script: FORM_cmd=query || import FORM_command=add FORM_msisdn=389881234567 FORM_provcode=SK FORM_attr=12 FORM_cmd can be "query" or "import" when FORM_cmd="query" then execute -> spdci -cmd $FORM_cmd FORM_cmd when... (3 Replies)
Discussion started by: vasil
3 Replies

4. Shell Programming and Scripting

Numerical Decision

I'm tryning to do something like this, I have this file: spaces12tabgoodbye spaces3tabhello I want to copy to another file the lines that have the number above 10... I thought using sort -rn but I don't know how to discard the lines that have the number below 10. Any idea? Thanks (3 Replies)
Discussion started by: pmpx
3 Replies

5. UNIX for Dummies Questions & Answers

Platform decision

Hi folks First, let me apologize in advance for the long message. I know this will sound like a typical "newbie" message but I am really working to understand the technology in order to make informed decisions. I am currently in the process of evaluating Unix hardware and OS platforms for an... (5 Replies)
Discussion started by: Scooter
5 Replies

6. Shell Programming and Scripting

decision, case, it then else?

I'm writing this script in Korn Shell, on AIX. The script will print a log file, and it needs to decide what region it's in before printing. ( the output of db2 "get instance" is either "The current database manager instance is: db2q" or "The current database manager instance is: db2u") ... (6 Replies)
Discussion started by: jpprial
6 Replies
Login or Register to Ask a Question