LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-30-2005, 02:22 PM   #1
eroica
Member
 
Registered: Sep 2003
Distribution: Mandriva 2006,OpenSuse 10.1
Posts: 154

Rep: Reputation: 30
Question ? on logical operators in BASH


hello,
i am writing my first little script to automate virus scanning w/ f-protect. i cant get this little snippett of code to work in bash. i get stuck in the while loop and even when i input a 1 or 2 at the keyboard i dont break out of the while loop:

read var1
# Check input to make sure input is valid

while [ $var1 != 1 -o $var1 != 2 ]; do
echo "You did not type a 1) or a 2). Please select from the above choices..."
read var1
done

any suggestions most appreciated...
alan
 
Old 01-30-2005, 02:32 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 67
I think you want to use an AND operator and not OR.
 
Old 01-30-2005, 03:48 PM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Maybe something like this...
Code:
#!/bin/bash

echo ""
echo "Option 1 does something"
echo "Option 2 does something else"
echo
echo "Please select #1 or #2: "
read var1

if [ $var1 = 1 ]; then

   echo You selected 1
   echo Do something here
   echo
   
elif [ $var1 = 2 ]; then
   
   echo You selected 2
   echo Do something else here
   echo
   
else
   echo The number you selected is $var1
   echo You have not chosen wisely.
   echo
fi
exit 0
 
Old 01-30-2005, 07:15 PM   #4
eroica
Member
 
Registered: Sep 2003
Distribution: Mandriva 2006,OpenSuse 10.1
Posts: 154

Original Poster
Rep: Reputation: 30
no i think i want the logical "or" . here is more of the program:
#! /bin/bash

echo "Please pick an option:"
echo "(1) to run a Scan"
echo "(2) to Update F-Protect .dat files"
read var1

while [$var1 != 1] -o [$var1 != 2]; do
echo " You did not input a 1 or 2. Please choose one of the above options"
read var1
done

if [ $var1 = 1 ]; then

echo "Please type the Directory or full path to filename you would like to scan:"
read var2
etc.....
i want to stay in the while loop until the person running the script punches in a 1 or 2 and then will continue on depending on choice. i know i could use if then else but this way person stays in the loop until he types in the correct choice.i'm having trouble getting that damn "or" operator working. ....

alan

Last edited by eroica; 01-30-2005 at 07:19 PM.
 
Old 01-31-2005, 01:50 PM   #5
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 67
Quote:
Originally posted by eroica
no i think i want the logical "or" . here is more of the program:
Are you sure?

Quote:
while [$var1 != 1] -o [$var1 != 2]; do
Assume $var1="1", the loop will continue because $var1!="2". If $var1="2", the loop will continue because $var1!="1". If $var1="x", the loop will continue because $var1!="1" and $var1!="2".

With an AND operator:
Assume $var1="1", the loop will stop because $var1="1". If $var1="2", the loop will stop because $var1="2". If $var1="x", the loop will continue because $var1!="1" and $var1!="2".

$var1 can never be equal to 1 and 2 at the same time so you get stuck in an infinite loop.
 
Old 01-31-2005, 02:04 PM   #6
Artanicus
Member
 
Registered: Jan 2005
Location: Finland
Distribution: Ubuntu, Debian, Gentoo, Slackware
Posts: 827

Rep: Reputation: 31
just wanted to point out that when making "menus" of some sort in scripts, I usually find the case method alot easier and cleaner than a generic loop with if:s.. Heres a grab from my digicam script:

Code:
#!/bin/bash
again=true

while [ $again = true ]; do
clear

echo "         Choose action"
echo "          ----------------"
echo
echo "1 : Mount and download all pics to /share/images/digicam/" 
echo "2 : Empty cam memory"
echo "3 : Umount and quit"
echo "4 : Umount, quit and open the image dir"
echo

read kumpi

case "$kumpi" in

  "1" | "d" )
  echo
  echo "Downloading all pics from camera"
        ./mount.sh
        ./download.sh
        sleep 2
  ;;
# Note double semicolon to terminate
# each option.

  "2" | "e" )
  echo
  echo "Empty"
  echo "Are you sure?"
  echo "(y/N)"
  read empty

        case "$empty" in
        "Y" | "y" )
        echo "let it be so"
        ./empty.sh
        ;;

        * )
        echo
        echo "Fine, lets not empty"
        ;;
        esac
  ;;

  "3" | "q" )
  echo
  ./umount.sh
  echo "Cyas, the cam is unmounted now"
  exit 0
  ;;

  "4" | "k" )
  ./umount.sh
  ./konqueror.sh
  echo
  echo "Cyas, the cam is unmounted now"
  exit 0
  ;;



          * )
   # Default option.
   echo
   echo "Un-used option, check your dial"
   sleep 2
  ;;

esac
done
Hope this gives you some new ideas.. (:
 
Old 01-31-2005, 02:17 PM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982
As david_ross has outlined, your basic algebraic inversions are not correct. essentially you're not followng DeMorgans Theorems: http://www.allaboutcircuits.com/vol_4/chpt_7/8.html

ahhh, i used to love boolean algebra...
 
Old 01-31-2005, 07:26 PM   #8
eroica
Member
 
Registered: Sep 2003
Distribution: Mandriva 2006,OpenSuse 10.1
Posts: 154

Original Poster
Rep: Reputation: 30
thanx for the input. i have the program @ work and will try it out. its been a long time since i've programmed and i learned using pascal in school. its funny how u can look at a problem for hours and not see the obvious and someone can look and focus right in on the problem. i will let u know if that solves it. i guess i should have used case but that came later in the chapter and of course i didnt wait. thanx again
alan
 
Old 02-01-2005, 10:07 AM   #9
eroica
Member
 
Registered: Sep 2003
Distribution: Mandriva 2006,OpenSuse 10.1
Posts: 154

Original Poster
Rep: Reputation: 30
thanx guys. that fix works like a charm. i have one other question. running the f-protect update script requires root permision. i tried changing the permisions on this script to root but that didnt work. aside from launching from root console or issuing the command with a kdesu -c it dies when it tries to call the perl script in f-protect for updating. is there anyway of giving my script power to run the perl scripts in f-protect?because i would like to put a shortcut to this script on my mandrake menu.
thanx,
alan
 
Old 02-01-2005, 12:38 PM   #10
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 67
You could make the script suid root if you want to let it run f-protect as root.
 
Old 02-01-2005, 01:12 PM   #11
eroica
Member
 
Registered: Sep 2003
Distribution: Mandriva 2006,OpenSuse 10.1
Posts: 154

Original Poster
Rep: Reputation: 30
ok but how do i do that??
alan
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Bash - how to write logical expressions? lowpro2k3 Programming 2 03-24-2005 03:39 PM
Operators *^ and >> GodSendDeath Programming 4 11-01-2004 09:47 PM
about bitwise operators? eshwar_ind Programming 17 10-25-2004 02:13 AM
operators linuxanswer Programming 3 12-14-2003 06:09 PM
bash script test file operators... bulliver Programming 3 10-17-2003 12:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:06 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