LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Simple if - else logic problem?? - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/simple-if-else-logic-problem-newbie-4175450644/)

Batistuta_g_2000 02-18-2013 06:54 AM

Simple if - else logic problem?? - Newbie
 
Why doesnt this work? - just looking to understand if/else statements (am checking for Mutt, then asking to install, no install commands yet, just using echo to see if it will work logically)
Code:

#!/bin/bash

dpkg -s mutt 2>error
if [[ $? -eq 1 ]]
 then
    echo "install now y/n"
    read entry1
  if [[ "$entry1" == "Y" || "$entry1 == "y" ]]
    then
    echo "INSTALLING/n INSTALLING/n/n/n/n/n"
  else
    echo "You need Mutt for program to work/n/n"
    echo "Exiting to main menu"
sleep 3s
fi
else
echo "installed already exiting in 3,2,1...."
fi
exit 0


Habitual 02-18-2013 07:15 AM

Quote:

#!/bin/bash
set -x
...
for starters. Then analyze the output on the screen when you run the script interactively.

Please let us know...

Edit:
Please use [code][/code] tags around script(s) or their output.
Thank you.

Batistuta_g_2000 02-18-2013 07:20 AM

Quote:

Originally Posted by Habitual (Post 4894333)
for starters. Then analyze the output on the screen when you run the script interactively.

Please let us know...

Edit:
Please use [code][/code] tags around script(s) or their output.
Thank you.

Not much more in output: I'm not sure about the order I have: if-then-if-then-else-fi-fi
So dpkg gives true/false, if true read input, if false exit, if input Y do something, if input anything else exit.....Help appreciated.

UBUNTU::./mutt
+ dpkg -s mutt
./mutt: line 12: conditional binary operator expected
./mutt: line 12: syntax error near `"You'
./mutt: line 12: ` echo "You need Mutt for program to work/n/n"'

colucix 02-18-2013 07:26 AM

You missed a quote here:
Code:

if [[ "$entry1" == "Y" || "$entry1" == "y" ]]

Batistuta_g_2000 02-18-2013 07:31 AM

Quote:

Originally Posted by colucix (Post 4894338)
You missed a quote here:
Code:

if [[ "$entry1" == "Y" || "$entry1" == "y" ]]

You sir....are a hero - many thanks (having some newbie freak-out moments)....

grail 02-18-2013 07:40 AM

Just to add, bashes if can also take a function directly so you can place your dpkg statement straight the first if:
Code:

if ! dpkg -s mutt 2>error
I do not know much about dpkg, but I am guessing a positive value means its status is not available. Hope this helps.

Batistuta_g_2000 02-18-2013 08:00 AM

Quote:

Originally Posted by grail (Post 4894352)
Just to add, bashes if can also take a function directly so you can place your dpkg statement straight the first if:
Code:

if ! dpkg -s mutt 2>error
I do not know much about dpkg, but I am guessing a positive value means its status is not available. Hope this helps.

Good one, thanks a lot.


All times are GMT -5. The time now is 10:10 AM.