ksh built-in function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh built-in function
# 1  
Old 09-23-2004
ksh built-in function

Does anyone know why the following expression return an error
[[ "$(echo yop | grep o)" ]]
while the following one not
[ "$(echo yop | grep o)" ]
??
Thanks
# 2  
Old 09-23-2004
There are several differences between the two types of tests. The double bracket demands a logical expression while the single bracket does not.

[ $string ]

is true if string contains something. And
[ ]

if always false. You are using a variation on that. This depends on $string completely disappearing before [ sees its arguments. That will happen, but it's why

[ $string = "cat" ]

doesn't work. You must put quotes around $string for that to work. [[ is immune to that effect so

[[ $string = "cat" ]]

works fine. You can use -n with both of them to achieve the same effect:

[ -n "$string" ]
[[ -n $string ]]

But beware of

[ -n $string ]

If string is empty, it will disappear and the single bracket is back to seeing a single argument which will be true if that argument is not empty. "-n" is not empty so the test will be true.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting shell to Perl I run into shell built in function trap and need alternative in Perl

I am working on converting shell to Perl script. In shell we have built in function trap Do you know alternative in Perl or actually we don't need it? Thanks for contribution (3 Replies)
Discussion started by: digioleg54
3 Replies

2. Programming

how to use built in sorting function alphasort

can any one help me with this function through an example.. (1 Reply)
Discussion started by: mahesh55
1 Replies

3. Shell Programming and Scripting

KSH - different syntax for function

Hi, I wanted to know what's the difference between the below two syntax used for writing ksh function: e.g. 1 ------ function fn1 { echo "Hello World" } e.g. 2 ------ fn1 () { echo "Hello World" } (4 Replies)
Discussion started by: dips_ag
4 Replies

4. Shell Programming and Scripting

Calling Function in KSH

I have a script with 2 functions 1) show_menu 2) create Ths show_menu function works fine....... Sort of.... When I select option 2 of the menu the code does a few commands and then calls another function called create. It's at this point that I get "create: not found"..... However,... (2 Replies)
Discussion started by: hxman
2 Replies

5. Shell Programming and Scripting

passing values to function in Ksh

Hi, I'm trying to work on the script given below #!/bin/ksh -x pfile() { echo "$1" } touch smp19 echo "Hi" > smp19 result=$(pfile $smp19) echo $result As highlighted , when i pass $smp19 as parameter ,it does not display the output.However when i try giving "Hi" instead... (2 Replies)
Discussion started by: Sheema
2 Replies

6. Shell Programming and Scripting

csh has function -x as ksh ?

Dear All, Normally, I use ksh to code script but I got a new assignment to check the error code of csh so I want to know csh has fuction -x(/bin/ksh -x) as ksh or not? If csh has, which mode? Another way, how can I check it my code is correctly? Thank in advance (2 Replies)
Discussion started by: unitipon
2 Replies

7. Shell Programming and Scripting

KSH list as function parameter

Hello, Could anyone help me with some KSH syntax? I'm trying to pass a list as a function parameter in a KSH? For example I have code like this: print_counter() { N=$1 C=$2 for A in $C; do echo "This is $N number $A" done } NAME=BRICK COUNT=" 1 2 3 4" ... (2 Replies)
Discussion started by: pn8830
2 Replies

8. Shell Programming and Scripting

import var and function from ksh script to another ksh script

Ih all, i have multiples ksh scripts for crontab's unix jobs they all have same variables declarations and some similar functions i would have a only single script file to declare my variables, like: var1= "aaa" var2= "bbb" var3= "ccc" ... function ab { ...} function bc { ... }... (2 Replies)
Discussion started by: wolfhurt
2 Replies

9. Shell Programming and Scripting

accessing a function in ksh from perl

is it possible? Because I know we could use open(A, `abc.ksh`); to access a ksh, but is it possible to access just one (or more) function from the ksh script? (2 Replies)
Discussion started by: ahtat99
2 Replies

10. Shell Programming and Scripting

ksh: can you use getopts in a function?

I wrote a script that uses getopts and it works fine. However, I would like to put the function in that script in a startup file (.kshrc or .profile). I took the "main" portion of the script and made it a function in the startup script. I source the startup script but it doesn't seem to parse... (4 Replies)
Discussion started by: lyonsd
4 Replies
Login or Register to Ask a Question