? 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 |
I think you want to use an AND operator and not OR.
|
Maybe something like this...
Code:
#!/bin/bash |
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 |
Quote:
Quote:
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. |
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 |
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... |
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 |
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 |
You could make the script suid root if you want to let it run f-protect as root.
|
ok but how do i do that??
alan |
All times are GMT -5. The time now is 07:07 PM. |