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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
01-30-2005, 02:22 PM
|
#1
|
Member
Registered: Sep 2003
Distribution: Mandriva 2006,OpenSuse 10.1
Posts: 154
Rep:
|
? 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
|
|
|
01-30-2005, 02:32 PM
|
#2
|
Moderator
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047
Rep:
|
I think you want to use an AND operator and not OR.
|
|
|
01-30-2005, 03:48 PM
|
#3
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
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
|
|
|
01-30-2005, 07:15 PM
|
#4
|
Member
Registered: Sep 2003
Distribution: Mandriva 2006,OpenSuse 10.1
Posts: 154
Original Poster
Rep:
|
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.
|
|
|
01-31-2005, 01:50 PM
|
#5
|
Moderator
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047
Rep:
|
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.
|
|
|
01-31-2005, 02:04 PM
|
#6
|
Member
Registered: Jan 2005
Location: Finland
Distribution: Ubuntu, Debian, Gentoo, Slackware
Posts: 827
Rep:
|
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.. (:
|
|
|
01-31-2005, 02:17 PM
|
#7
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
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...
|
|
|
01-31-2005, 07:26 PM
|
#8
|
Member
Registered: Sep 2003
Distribution: Mandriva 2006,OpenSuse 10.1
Posts: 154
Original Poster
Rep:
|
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
|
|
|
02-01-2005, 10:07 AM
|
#9
|
Member
Registered: Sep 2003
Distribution: Mandriva 2006,OpenSuse 10.1
Posts: 154
Original Poster
Rep:
|
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
|
|
|
02-01-2005, 12:38 PM
|
#10
|
Moderator
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047
Rep:
|
You could make the script suid root if you want to let it run f-protect as root.
|
|
|
02-01-2005, 01:12 PM
|
#11
|
Member
Registered: Sep 2003
Distribution: Mandriva 2006,OpenSuse 10.1
Posts: 154
Original Poster
Rep:
|
ok but how do i do that??
alan
|
|
|
All times are GMT -5. The time now is 10:41 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|