LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-14-2014, 09:51 AM   #1
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,350

Rep: Reputation: 127Reputation: 127
Bash OR condition


Solved, I'm blind as a bat...

A small script that shall echo "Stopping" if user presses "n" or "N", else echo "Starting".
I can't figure out how to get the syntax right?
Code:
#!/bin/bash
while [[ "$SVAR" != "n" && "$SVAR" != "N" && "$SVAR" != "j" && "$SVAR" != "J" ]]
do
        echo "Start or stop?"
        read -n 1 SVAR 
        echo "" 
        echo "Typed letter is $SVAR"
        if [ "$SVAR" == "N" ] || [ "$SVAR2" == "n" ] ; then
                echo "Stopping"
        else
                echo "Starting" 
        fi
done
Typing "N" echoes "Stopping", correct. However, typing "n" echoes "Starting"!
I have tried several variants of that if statement:
Code:
if [ "$SVAR" == "N" ||  "$SVAR2" == "n" ] ; then
if [[ "$SVAR" == "N" ||  "$SVAR2" == "n" ]] ; then
if [ "$SVAR" == "N" -o  "$SVAR2" == "n" ] ; then
 if [[ ("$SVAR" == "N") || ("$SVAR2" == "n") ]] ; then
The variable $SVAR is assigned correct letter, so it has to be the if statement that's not correct.

The reply when pressing "n" is always
Code:
# /usr/local/sbin/test
Start or stop?
n
Typed letter is n
Starting

Last edited by pingu; 04-14-2014 at 09:53 AM.
 
Old 04-14-2014, 09:54 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
i think this line is causing you issues:
Code:
        if [ "$SVAR" == "N" ] || [ "$SVAR2" == "n" ] ; then
should be:
Code:
        if [ "$SVAR" == "N" ] || [ "$SVAR" == "n" ] ; then
you are never assigning $SVAR2 so it is never equal to 'n' or 'N'.


... i edited my post after you edited your response. glad you figured it out.

Last edited by schneidz; 04-14-2014 at 10:08 AM.
 
Old 04-14-2014, 09:57 AM   #3
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,350

Original Poster
Rep: Reputation: 127Reputation: 127
Thanks, but I tried that too.
Marked it solved because I'm sometimes blind - typo "$SVAR2" in that if statement. :-)
 
Old 04-14-2014, 10:53 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Maybe also suggest you only need to test the variable once will help with not typing the wrong one
Code:
if [[ "${SVAR^}" == "N" ]] ; then
 
1 members found this post helpful.
  


Reply



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
multiple if condition in a bash script grima Programming 4 03-28-2013 05:09 AM
BASH scripting Condition Statements Xyaib Linux - General 1 01-23-2013 02:18 PM
Bash: Repeat commands until a condition is matched iniuria Programming 4 03-09-2011 09:54 AM
Two square brackets in bash condition tirengarfio Programming 1 07-07-2009 12:36 PM
multiple if condition in bash tikit Programming 6 11-12-2008 01:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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