why the following snippet always give an output as "no" and never "yes" whatever the parameter I give to function is_number?
In addition, the function is_number() is working fine as per the below command line:
Last edited by Scott; 11-29-2010 at 09:42 AM..
Reason: Code tags, please...
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
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
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.
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)
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)
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)
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)
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)