LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   ? on logical operators in BASH (https://www.linuxquestions.org/questions/linux-software-2/on-logical-operators-in-bash-284166/)

eroica 01-30-2005 02:22 PM

? 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

david_ross 01-30-2005 02:32 PM

I think you want to use an AND operator and not OR.

homey 01-30-2005 03:48 PM

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


eroica 01-30-2005 07:15 PM

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

david_ross 01-31-2005 01:50 PM

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.

Artanicus 01-31-2005 02:04 PM

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.. (:

acid_kewpie 01-31-2005 02:17 PM

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...

eroica 01-31-2005 07:26 PM

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

eroica 02-01-2005 10:07 AM

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

david_ross 02-01-2005 12:38 PM

You could make the script suid root if you want to let it run f-protect as root.

eroica 02-01-2005 01:12 PM

ok but how do i do that??
alan


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