LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-25-2011, 06:08 PM   #1
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Rep: Reputation: Disabled
help with if - else statement?


removed

Last edited by Batistuta_g_2000; 10-31-2011 at 05:06 PM.
 
Old 10-25-2011, 06:41 PM   #2
nenpa8lo
Member
 
Registered: Oct 2008
Location: Ireland
Distribution: Slackware 13.0, Slackware 13.37
Posts: 68
Blog Entries: 2

Rep: Reputation: 18
In
Code:
echo "or presss any "function key to get a full list of BACKUPS"
you have a quote in front of function. This clearly stated in the unexpected EOF while looking for matching `"' read error(s) and try to solve on your own first.
 
Old 10-25-2011, 06:44 PM   #3
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
Please see my other post: http://www.linuxquestions.org/questi...6/#post4508256

The syntax for a bash if statement is:
Code:
if list; then
    list;
[ elif list; then 
    list; ] ...
[ else 
    list; ]
fi
Basically, you have a spurious 'if' before the 'else', which ends your first if statement.
 
Old 10-25-2011, 09:22 PM   #4
j1alu
Member
 
Registered: Apr 2009
Distribution: debian gnu/linux
Posts: 798

Rep: Reputation: Disabled
Code:
if $RESCHO = [a-zA-Z0-9]
a) to check if the input contains only alphanumeric characters see here: http://pthree.org/2008/01/03/testing...ments-in-bash/
b) besides that you would need to use either "test" or "[" or "[[", as you want to test if $RESCHO is aphanumeric. Like you do it here:
Code:
if [[ $? -eq 0 ]]
----
before else there is no "fi"

----
after "else" you don't test again. The else body gets executed in case the condition of "if" was false

----
Instead of this:
Code:
echo "Files beginning with "$RESCHO" in BACKUP catalog:"
echo "________________________________________________"
echo " "
echo " "
you could write this:
Code:
echo "
Files beginning with "$RESCHO" in BACKUP catalog:
________________________________________________

"
You could also make use of -e and newline:
Code:
echo -e " 
Files beginning with "$RESCHO" in BACKUP catalog
-------------------------------------------\n\n"

-----
And there are some typos, like:
/home/ubuntu/backup_menu
cd or such is missing

sleep4s is "sleep 4"

Last edited by j1alu; 10-25-2011 at 09:25 PM.
 
Old 10-27-2011, 06:07 PM   #5
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
removed

Last edited by Batistuta_g_2000; 10-31-2011 at 05:06 PM.
 
Old 10-27-2011, 07:45 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,499

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
1. No need to not keep it all in bash
Code:
alnu="$(echo $filelet | sed -e 's/[^[:alnum:]]//g')"

#or just

alnu=${filelet//[^[:alnum:]]/}
2. Be impressed to see what you could echo that would give you anything but a 0 return code:
Code:
    echo "input valid"
    retvalue=$?
3. What would you get if you executed this on the command line (remember that a script is simply a way to bring multiple repetitive commands together):
Code:
find $BACKUPS $filelet* -eq 0 ]  ### Am I allowed to do this? is syntax right?
4. Allowed yes, but why not use the return code $? which you have used before?
Code:
initrestore=$(tar -xvzf "$rfilename".tar.gz $RESTORES) ##### am I allowed do this either?
5. Not sure I see the point to listing something already stored manually in another file. Also you have a typo in your 'filename' variable:
Code:
         if [ initrestore -eq 0 ]; then
          echo "Restoring "$filename" selected" # variable above is called rfilename
          ls -l $RESTORES
6. Not sure what this supposed to be doing?
Code:
/$BACKUPMENU
7. You still seem to have an issue with counting when it comes to how many times you open an if and how many times you close it. Count here is open 3 and close 2 (hence error message)
 
Old 10-27-2011, 11:33 PM   #7
thinmintaddict
LQ Newbie
 
Registered: Apr 2011
Location: Austin, TX
Distribution: Arch Linux
Posts: 12

Rep: Reputation: 6
Also, make sure that every 'if' has an accompanying 'then'.
 
Old 10-28-2011, 02:09 PM   #8
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
removed

Last edited by Batistuta_g_2000; 10-31-2011 at 05:06 PM.
 
Old 10-28-2011, 05:56 PM   #9
thinmintaddict
LQ Newbie
 
Registered: Apr 2011
Location: Austin, TX
Distribution: Arch Linux
Posts: 12

Rep: Reputation: 6
except for the misspelling in this line:
Code:
     echo "Not a proper character, need to be a letter or nuimber "
It seems like it should work fine :-)
 
Old 10-28-2011, 08:14 PM   #10
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
removed

Last edited by Batistuta_g_2000; 10-31-2011 at 05:06 PM.
 
Old 10-29-2011, 02:25 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,499

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
The script you have supplied above is incomplete. This will make it difficult to diagnose??

And at first glance I still see typos in variables.
 
Old 10-29-2011, 02:37 AM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
These lines are strange:
Code:
 15      sleep 3
 16    retvalue=$?
The return code from sleep is always 0 (unless it is interrupted?).

This line is wrong:
Code:
if [ $retvalue -eq 0 && find $BACKUPS -name $filelet* -eq 0 ]
find $BACKUPS -name $filelet* is not a numeric term so cannot be compared to 0.

You probably want something like
Code:
find_out=$(find $BACKUPS -name "$filelet*")
if [[ $find_out != '' ]]
   ...
The double quotes are essential to ensure file name expansion does not change the value of $filelet*
[[ ... ]] is [ ... ] evolved. It is more convenient to use for reasons explained here.

Last edited by catkin; 10-29-2011 at 02:37 AM. Reason: typo
 
Old 10-29-2011, 02:46 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,499

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
@catkin - I am not sure they are reading all posts or just the ones they want as I had covered some of this previously
 
Old 10-30-2011, 02:44 PM   #14
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
removed

Last edited by Batistuta_g_2000; 10-31-2011 at 05:05 PM.
 
Old 10-30-2011, 07:39 PM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,499

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Code:
#!/bin/bash


source /etc/backup.conf
echo "Please enter the first character of the file name"
echo "to search our current restores catalog, or else "
echo -n "ENTER alphanumeric input:"
echo "                            "
echo "                           "
read filelet
alnu=${filelet//[^[:alnum:]]/}
if [ "$alnu" == "$filelet" ];
   then
   echo "Good choice, checking for letter or number you selected..."

        
find_out=$(find $BACKUPS -name "$filelet*")
if [[ $find_out != '' ]]
      then
      clear
      echo "Files beginning with "$filelet" letter in BACKUP catalog:"
      echo "                                 "
      echo "$findout"
      echo "Enter filename you wish to restore:"
      echo "                                   "
      read rfilename
      initrestore=$(tar -xvzf $RESTORES$rfilename-$day.tar.gz\
                             \$BACKUPS-$rfilename)
                    if [ initrestore -eq 0 ]; # How is this supposed to work without a $?
            then
            echo "Restoring "$filename" selected to $RESTORES directory"
             $initrestore # What do you expect this to do??
               ls -l $RESTORES
         else
             echo "Restore failed, check error log for details."
             echo "Returning to Back up menu"
             sleep 5
             /$BACKUPMENU # Still not sure what you are trying to do here?
            exit 1
          fi

       else
          echo "No files found for that letter in Backup"
          echo "Returning to Back up menu"
          sleep 5
          /$BACKUPMENU
          exit 1
      fi
     else
        echo "Not a proper alpha-numeric Character"
        echo "Returning to Backup Menu"
        sleep 5
        /$BACKUPMENU
        exit 1
 fi

     fi

exit 1
Why do you need to 'exit' from every if?

You have 'exit 1' on completion of the script, ie you are saying it will never finish correctly (remember that if you test successful finish of this script it can never occur)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 04:50 AM
[SOLVED] Shell script for adding a statement in a file after a particular statement TheIndependentAquarius Programming 4 06-28-2010 03:07 AM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 12:38 AM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 08:05 PM
If Statement junjem0702 Programming 4 08-23-2004 09:49 AM


All times are GMT -5. The time now is 08:42 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration