LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-09-2008, 09:03 AM   #1
jonlake
Member
 
Registered: Apr 2004
Distribution: Slackware 11.0, Gentoo
Posts: 252

Rep: Reputation: 31
Korn Shell test if multiple variables are set


I'm writing a script in korn shell and cannot figure how to do the following:

In bash, it works like
Code:
#!/bin/bash

A="stuff"
B="more stuff"

if [[ -n $A && -n $B ]]; then
        echo "A is set to $A and B is set to $B"
else
        echo "A or B is not set"
fi
Results in
Code:
A is set to stuff and B is set to more stuff
Unfortunately, I don't have bash available in the environment I'm working in and don't know how to do this in ksh.
In ksh, I get
Code:
[[: ./test.sh  expression syntax error
A or B is not set
edit: I also tried
Code:
if [[ $A != "" && $B != "" ]]
but it didn't like a space in B

Last edited by jonlake; 04-09-2008 at 09:27 AM.
 
Old 04-09-2008, 09:39 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Uh, this:
Code:
#!/bin/ksh
A="stuff"
B="more stuff"

if [[ -n $A && -n $B ]]; then
        echo "A is set to $A and B is set to $B"
else
        echo "A or B is not set"
fi
produces
Code:
A is set to stuff and B is set to more stuff
Isn't that what you want?

Look and see if your login shell is sh or ksh, maybe.
 
Old 04-09-2008, 09:51 AM   #3
jonlake
Member
 
Registered: Apr 2004
Distribution: Slackware 11.0, Gentoo
Posts: 252

Original Poster
Rep: Reputation: 31
Sorry, I don't think I was clear. I posted the bash script I had made because I know bash just a little better than ksh. In my ksh script, I have #!/bin/ksh at the top, so I have:

Code:
#!/bin/ksh

A="stuff"
B="more stuff"

if [[ -n $A && -n $B ]]; then
        echo "A is set to $A and B is set to $B"
else
        echo "A or B is not set"
fi
and it returns
Code:
[[: ./test.sh  expression syntax error
A or B is not set
as apparently bash and ksh treat test differently?

More specifically, I am using the MKS toolkit which installs ksh, csh, tcl shell, and other stuff to run in windows.

edit:
apparently it is this toolkit I am using, because I just installed ksh on my gentoo laptop and ran the script in ksh, and it runs as expected, but when ran from the toolkit's korn shell, the if test fails and outputs:
Code:
[[: ./test.sh  expression syntax error
A or B is not set
Yet, if I test just one variable, it will run as expected.

Last edited by jonlake; 04-09-2008 at 09:57 AM.
 
Old 04-09-2008, 10:20 AM   #4
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Ah, winders (well, maybe).

OK, here's another way to do conditionals
Code:
if [ -n "${A}" -a -n "${B}" ]
(the braces are not necessary, but are a good habit to get into; with this syntax, the quotes should be used)

Try that and see if it works for you.
 
Old 04-09-2008, 10:41 AM   #5
jonlake
Member
 
Registered: Apr 2004
Distribution: Slackware 11.0, Gentoo
Posts: 252

Original Poster
Rep: Reputation: 31
Thanks

Adding the braces and quotes worked.

Thanks
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
korn shell loop jonlake Programming 2 03-28-2008 12:10 PM
korn shell ( ksh ) lechuga Puppy 3 02-05-2007 09:43 PM
korn shell tutorial/course kapilcool Linux - Certification 2 11-13-2006 09:18 PM
Korn shell [[ ... ]] operator andybengurion Linux - General 1 09-26-2006 03:18 AM
Korn shell script Muzica Solaris / OpenSolaris 4 09-06-2004 12:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration