Unix Shell Scripts and Boolean Expressions

  • Follow


Hello,
I am new to Unix and shell scripting. I am being asked to write some
shell scripts which utilize Boolean expressons.

For example in the script I have defined the following variables:
@ X = 2
@ Y = 3
@ XY = 0

Then I am being asked to implement the following Boolean Expression:
X.Y + X'.Y'
Where "." stands for AND; "+" stands for OR; and "'" stands for
complement.
So it would be (X AND Y) OR (X' AND Y')

In the script I tried the following:

$ XY = (($X -a $Y) -o (!$X -a !$Y))
echo "XY = $XY"

This is obviously wrong because I am getting an error which says 0:
Event Not Found

Can someone please explain how to use Boolean expressions with unix
shell scripts?

Thanks

0
Reply aeroman10 (7) 4/23/2005 5:56:45 AM

On 22 Apr 2005 22:56:45 -0700, webzila 
  <aeroman10@yahoo.com> wrote:
> Hello,
> I am new to Unix and shell scripting. I am being asked to write some
> shell scripts which utilize Boolean expressons.
>
> For example in the script I have defined the following variables:
> @ X = 2
> @ Y = 3
> @ XY = 0
>
> Then I am being asked to implement the following Boolean Expression:
> X.Y + X'.Y'
> Where "." stands for AND; "+" stands for OR; and "'" stands for
> complement.
> So it would be (X AND Y) OR (X' AND Y')
>
> In the script I tried the following:
>
> $ XY = (($X -a $Y) -o (!$X -a !$Y))
> echo "XY = $XY"
>
> This is obviously wrong because I am getting an error which says 0:
> Event Not Found
>
> Can someone please explain how to use Boolean expressions with unix
> shell scripts?
>
WHICH shell are you using?  The syntax looks almost like ksh or bash, 
but those shells do not allow spaces around the = sign in an assignment.
The -a and -o operators are used inside [ ] or [[ ]].  If you are using 
bash, read the man page, especially the "Arithmetic Evaluation" and 
"Conditional Expressions" sections.


-- 
"I deleted a file from my PC last week and I have just realized that I
need it. If I turn my system clock back two weeks will I have my file
back again?"
0
Reply Bill 4/24/2005 4:49:17 AM



Bill Marcum wrote:
> On 22 Apr 2005 22:56:45 -0700, webzila 
>   <aeroman10@yahoo.com> wrote:
> 
>>Hello,
>>I am new to Unix and shell scripting. I am being asked to write some
>>shell scripts which utilize Boolean expressons.
>>
>>For example in the script I have defined the following variables:
>>@ X = 2
>>@ Y = 3
>>@ XY = 0
>>
>>Then I am being asked to implement the following Boolean Expression:
>>X.Y + X'.Y'
>>Where "." stands for AND; "+" stands for OR; and "'" stands for
>>complement.
>>So it would be (X AND Y) OR (X' AND Y')
>>
>>In the script I tried the following:
>>
>>$ XY = (($X -a $Y) -o (!$X -a !$Y))
>>echo "XY = $XY"
>>
>>This is obviously wrong because I am getting an error which says 0:
>>Event Not Found
>>
>>Can someone please explain how to use Boolean expressions with unix
>>shell scripts?
>>
> 
> WHICH shell are you using?  The syntax looks almost like ksh or bash, 
> but those shells do not allow spaces around the = sign in an assignment.
> The -a and -o operators are used inside [ ] or [[ ]].  If you are using 
> bash, read the man page, especially the "Arithmetic Evaluation" and 
> "Conditional Expressions" sections.
> 
> 
I'm guessing csh (the @ x = 2, for example), and the Event not found
message could be coming from the exclamation marks on the right side
that are not escaped, so history substitution is taking place but
there is no event with that number.

man expr

In a Bourne-like shell try:

XY=`expr \( $X != 0 \& $Y != 0 \) \| \( $X = 0 \& $Y = 0 \)`

The parentheses, ampersand and vertical stroke need to be escaped
since they are special to the shell.  In the C shell, the
exclamation mark would need to be escaped as well.

Bill Seivert


0
Reply Bill 4/24/2005 5:56:17 AM

Hello,
I am using the csh shell
I figured out the correct syntax and the following code seems to do the
job:

@ XY = ($X && $Y) || (~$S && ~$R)
echo "X.Y + X'.Y' = $XY"

But now I am having a problem writing the code for the following
expression:
X + X'.Y = X + Y
I have to verify that they are equal

I tried writing the following code:

@ XY = $X + (~$X && $Y)
@ XY1 = $X + $Y
echo "$XY = $XY1"

With the code above I get an error that says "Invalid User: #X" where
#X is whatever integer that X was set to earlier in the script.
Then if I put a space between the ~ symbol and $X I dont get the error
anymore but the left and right sides do not equal.

What am I doing wrong there?

Thanks

0
Reply webzila 4/24/2005 7:59:19 PM


webzila wrote:
> Hello,
> I am using the csh shell
> I figured out the correct syntax and the following code seems to do the
> job:
> 
> @ XY = ($X && $Y) || (~$S && ~$R)
> echo "X.Y + X'.Y' = $XY"
> 
> But now I am having a problem writing the code for the following
> expression:
> X + X'.Y = X + Y
> I have to verify that they are equal
> 
> I tried writing the following code:
> 
> @ XY = $X + (~$X && $Y)
> @ XY1 = $X + $Y
> echo "$XY = $XY1"
> 
> With the code above I get an error that says "Invalid User: #X" where
> #X is whatever integer that X was set to earlier in the script.
> Then if I put a space between the ~ symbol and $X I dont get the error
> anymore but the left and right sides do not equal.
> 
> What am I doing wrong there?
> 
> Thanks
> 

The tilde (~) is not "NOT", it is shorthand for $HOME when used by
itself, or when followed by a string, the home directory of that
user, ~bob is bob's home directory.  If X has a number, ~$X will
look for a user name of that number, and failing to find it,
probably produces the Invalid user message.

Bill Seivert

0
Reply Bill 4/24/2005 10:49:45 PM

"webzila" <aeroman10@yahoo.com> writes:

> Hello,
> I am using the csh shell
> I figured out the correct syntax and the following code seems to do the
> job:
>
> @ XY = ($X && $Y) || (~$S && ~$R)
> echo "X.Y + X'.Y' = $XY"
>
> But now I am having a problem writing the code for the following
> expression:
> X + X'.Y = X + Y
> I have to verify that they are equal
>
> I tried writing the following code:
>
> @ XY = $X + (~$X && $Y)
> @ XY1 = $X + $Y
> echo "$XY = $XY1"
>
> With the code above I get an error that says "Invalid User: #X" where
> #X is whatever integer that X was set to earlier in the script.
> Then if I put a space between the ~ symbol and $X I dont get the error
> anymore but the left and right sides do not equal.
>
> What am I doing wrong there?

You assumed that you got the syntax right.

Try bash, its syntax is a tad simplier.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
The rule for today:
Touch my tail, I shred your hand.
New rule tomorrow.
0
Reply Pascal 4/24/2005 10:51:28 PM

> The tilde (~) is not "NOT", it is shorthand for $HOME when used by
> itself, or when followed by a string, the home directory of that
> user, ~bob is bob's home directory.  If X has a number, ~$X will
> look for a user name of that number, and failing to find it,
> probably produces the Invalid user message.
>
> Bill Seivert

What is the sign/symbol for a Complement then?
I am using a book called "Practical Guide to Solaris" it only mentions
the ~ as being used to create a complement. Would it help if I use [ ]
instead of ( )?

I tried searching the web but couldnt find much help. Some sites say
that when you use boolean expressions you need to use [  ] but this
book that I am using didnt mention anything about that.

0
Reply webzila 4/24/2005 10:57:45 PM

> You assumed that you got the syntax right.
>
> Try bash, its syntax is a tad simplier.
>
> --
> __Pascal Bourguignon__
http://www.informatimago.com/
> The rule for today:
> Touch my tail, I shred your hand.
> New rule tomorrow.

csh is a requirement for this exercise so I have to use that...
Boolean expressions is the only thing that I seem to have problems with
so far

0
Reply webzila 4/24/2005 10:59:45 PM

"webzila" <aeroman10@yahoo.com> writes:
> csh is a requirement for this exercise so I have to use that...
> Boolean expressions is the only thing that I seem to have problems with
> so far

No, the problem is that you're gluing tokens together. This is not C:

> @ XY = $X + (~$X && $Y)

  @ XY = $X + ( ~ $X && $Y )

You should read man csh, in particular the "Lexical structure" section.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

This is a signature virus.  Add me to your signature and help me to live
0
Reply Pascal 4/25/2005 2:53:40 AM

8 Replies
602 Views

(page loaded in 0.487 seconds)

Similiar Articles:













7/21/2012 2:51:22 AM


Reply: