LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 01-11-2008, 09:56 AM   #1
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Rep: Reputation: 60
Novice Korn Shell WantAbe Programmer


This is part of a nested if statement and what I am trying to do is say'" If there is data that has a '"v0" or a "v2" or "v3" in the variable $ver then print this or else print all other variables except $ver"


PHP Code:
 if [[ "$ver"v[0-2]"]]
   
then
     printf 
"%-24s%-8s%-8s%-14s%-6s\n"  \
          
$ip_address $ver $ping_able $telnet_able $log_able >> $check_log
   
else
     
printf "%-24s%-8s%-8s%-6s\n"  \
          
$ip_address $ping_able $telnet_able $log_able >> $check_log 
Or can I say," If there is anything in $ver then print the whole thing or else print everything without $ver - help

Last edited by metallica1973; 01-11-2008 at 10:08 AM.
 
Old 01-12-2008, 09:38 AM   #2
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

I think your syntax is incorrect for matching. Here's an example similar to yours:
Code:
#!/bin/bash -

# @(#) s1       Demonstrate == for matching within [[, ]].

show_me()
{
  local ver="$1"
  echo " $FUNCNAME: looking at ver, $ver"
  if [[ "$ver" = "v[0-2]" ]]
  then
    echo " single \"=\", variable ver, $ver, matched."
  fi
  if [[ "$ver" == v[0-2] ]]
  then
    echo " double \"=\", variable ver, $ver, matched."
  fi
}

echo
echo " calling with v3"
show_me v3

echo
echo " calling with v0"
show_me v0

exit 0
Producing:
Code:
% ./s1

 calling with v3
 show_me: looking at ver, v3

 calling with v0
 show_me: looking at ver, v0
 double "=", variable ver, v0, matched.
When all else fails, reading the documentation:
Quote:
[[ expression ]]
Return a status of 0 or 1 depending on the evaluation of the
conditional expression expression. Expressions are composed of
the primaries described below under CONDITIONAL EXPRESSIONS.
Word splitting and pathname expansion are not performed on the
words between the [[ and ]]; tilde expansion, parameter and
variable expansion, arithmetic expansion, command substitution,
process substitution, and quote removal are performed.

When the == and != operators are used, the string to the right
of the operator is considered a pattern and matched according to
the rules described below under Pattern Matching. The return
value is 0 if the string matches or does not match the pattern [sic],
respectively, and 1 otherwise. Any part of the pattern may be
quoted to force it to be matched as a string

-- excerpt from man bash
Note also this is an expression for 0-2, not including 3.

Best wishes ... cheers, makyo

Last edited by makyo; 01-12-2008 at 09:39 AM.
 
Old 01-12-2008, 09:50 AM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

I just noticed that you were asking about the Korn shell, but I replied with a bash example.

I changed "bash" to "ksh" in the shebang and the results were the same as the bash example, i.e. the double "=" and omitted quotes allowed the match to succeed. I used pdksh.

Apologies for the confusion ... cheers, makyo
 
Old 01-13-2008, 07:09 PM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
Y'know, suit yourself doing things like this with shell programming... but as for me, "that's what a real programming language is for." Perl, Python, and probably PHP (not to mention the special-purpose but very handy "awk") are probably all right there at your disposal.

Sure, this is my "JM2CW" and absolutely nothing more, but my take on the situation is that shell scripting is best suited to what is "a very simple command," whereas a general-purpose language like one of the above is purpose-built for "programs." When you run them, thanks to the "shebang" #! syntax, it's impossible to tell the difference.
 
Old 01-14-2008, 05:26 AM   #5
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
I somewhat understand your example. Can you give me another example that easier to understand. You dealing with a real novice.

PHP Code:
then
    
echo " single \"=\", variable ver, $ver, matched."
  
fi
  
if [[ "$ver== v[0-2] ]]
  
then
    
echo " double \"=\", variable ver, $ver, matched." 
single, double ?

Last edited by metallica1973; 01-14-2008 at 05:28 AM.
 
Old 01-14-2008, 06:19 AM   #6
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi, metallica1973.

There were 2 issues with your code. First, the "==". That informs ksh that you want the right-hand-side to be interpreted as a pattern. You understood the pattern concept, because you used the character-class symbols "[]".

However, the use of quotes causes the enclosed characters to be interpreted as plain characters, not as a pattern.

So in the function I created, I first used what you had -- a single "=" and the quotes, followed by another test with "==" with no quotes. In the function calls later in the script, the first test failed, the second test succeeded.

The bottom-line is to use "==" and omit quotes in order to cause pattern-matching.

Code:
right:
[[ variable == pattern_string ]]

wrong:
[[ variable = "pattern_string" ]]
I have not checked all the differences between ksh and bash, but most people get a lot of good information from http://www.tldp.org/LDP/abs/html/index.html , and for the "[[ ]]" sequence specifically from http://www.tldp.org/LDP/abs/html/tes...ucts.html#EX11 , which discusses the "[ ]" sequence, then shows how "[[ ]]" improves on that. In fact, it notes that "[[ ]]" was adapted for bash from ksh88.

There may be some confusion between "==" and "=~" in bash3, unfortunately. The "=~" is intended to allow extended regular expressions to be used in tests. The "=~" operator does not exist in pdksh, but I don't know about ksh. The operator was designed to follow the use in perl.

Best wishes ... cheers, makyo
 
Old 01-14-2008, 06:25 AM   #7
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
I have a better understanding, 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 equivalent bassplayer69 Linux - General 1 08-02-2007 01:15 PM
korn shell ( ksh ) lechuga Puppy 3 02-05-2007 09:43 PM
Novice C++ Programmer looking for book suggestions. bokavitch Programming 6 01-22-2005 05:08 PM
Using a Korn shell in rc scripts desbyleo Solaris / OpenSolaris 3 11-12-2004 05:27 PM
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 03:27 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