LinuxQuestions.org
Visit Jeremy's Blog.
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-24-2011, 04:07 PM   #1
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Rep: Reputation: Disabled
Else if - not working.


Ok again - my disclaimer - am a newbie am struggling to write a backup script to search for a file name submitted by the user and back it up to a directory I have created.

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx#!/bin/bash
clear
echo "Welcome $USER"
echo -n "Please enter filename:: "
read filename
find -L ./"$filename"
if [ $? == 1 ]; then
echo " file not found, exiting"
exit 1
elif [ $? == 0 ]; then
echo "file "$filename" found, do you want to back it up [y/n]? "
read yn
if [ "$yn" = "" ] || [ "$yn" = "y" ] || [ "yn" = "Y"]; then
day=$(date +%A)
backupfile="/home/ubuntu/backups/$USER-$day.tgz"
date
echo
tar czf $backupfile $filename
echo
echo "Backup finished"
date
ls -lh
elif [ "$yn" = "n" ] || [ "$yn" = "N" ]; then
echo "No backup requested, returning to main menu"
/home/ubuntu/backup_menu
exit 1
else
if [ "yn" != "y" ] || [ "$yn" != "Y" ]
[ "yn" != "n" ] || [ "$yn" != "N" ]
echo "No option for that, returning to main menu" ### print no option
/home/ubuntu/backup_menu
exit 1
fi
fi
fi
done


Output below:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Welcome ubuntu
Please enter filename:: file1
./file1
./fin: line 33: syntax error near unexpected token `fi'
./fin: line 33: ` fi'
ubuntu@ubuntu:~$

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Heres my logic:

#!/bin/bash
clear ###Clear screen
echo "Welcome $USER" ### print welcome to user
echo -n "Please enter filename:: " ### new line enter filename
read filename ### read filename
find -L ./"$filename" ### find filename include symlinks
if [ $? == 1 ]; then ### if output 1 do
echo " file not found, exiting" ### print file not found
exit 1 ### exit script
elif [ $? == 0 ]; then ### or else if out put is 0 do
echo "file "$filename" found, do you want to back it up [y/n]? ## print options
read yn ### read options
if [ "$yn" = "" ] || [ "$yn" = "y" ] || [ "yn" = "Y"]; then ### if y do
day=$(date +%A) ### show date
backupfile="/home/ubuntu/backups/$USER-$day.tgz" ### backup file
date ### show date
echo ###
tar czf $backupfile $filename ### backup filename to backup directory
echo ###
echo "Backup finished" ### print backup finished
date ### show date
ls -lh ### list directory long name and time
elif [ "$yn" = "n" ] || [ "$yn" = "N" ]; then ### else if no do
echo "No backup requested, returning to main menu" ### print failure
/home/ubuntu/backup_menu ### load other script
exit 1 ### exit
else ### or else
if [ "yn" != "y" ] || [ "$yn" != "Y" ] ### if not YyNn
[ "yn" != "n" ] || [ "$yn" != "N" ] ###
echo "No option for that, returning to main menu" ### print no option
/home/ubuntu/backup_menu ### load other script
exit 1 ### exit
fi ### finish if
fi ### finish if
done ### done

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Any help greatly appreciated.
 
Old 10-24-2011, 05:20 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650
*Just a quick note, it's a lot easier to read code if you paste it in between code tags

There are a couple of big things in this script that could be improved -

Case statements may be easier to read/maintain than complex if/elif/else blocks:
Code:
read yn
case $yn in
[yY] )
    # do some stuff
    ;;
[nN] )
    # don't do some stuff
    ;;
* )
    echo "<*> unknown option"
    exit 1
    ;;
esac
When the user selects the "no" option, you're re-calling the parent menu script instead of just exiting.
Code:
echo "No backup requested, returning to main menu"
/home/ubuntu/backup_menu
exit 1
If the parent menu script called this script then a simple exit is fine, otherwise you're ending up with a chain of processes with every "no":
Code:
[menu] -> [backup - ('no' option selected)] -> [menu(new instance, not parent)] -> [backup -...]
A case statement is not the answer to every problem, if you want to stick with 'if' then try something like this to avoid confusion:

Code:
if [[ $? -eq 0 ]]
then
    # do some stuff
fi
Expand it a bit:
Code:
if [[ $? -eq 0 ]]
then
    # do some stuff
else
    # tell the use something went wrong
fi
Expand a bit more:
Code:
if [[ $? -eq 0 ]]
then
    # do some stuff
    #test something else
    if [[ $blah = "yes" ]]
    then
        # do some extra stuff
    fi
else
    # tell the use something went wrong
fi
.. and so on - make sure you indent correctly and it will be easier to see where you went wrong.
 
Old 10-24-2011, 07:09 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,401

Rep: Reputation: 1488Reputation: 1488Reputation: 1488Reputation: 1488Reputation: 1488Reputation: 1488Reputation: 1488Reputation: 1488Reputation: 1488Reputation: 1488
The problem with your script is the "if" statement on lines 28 and 29. It is missing the "||" to connect the clauses on the two lines and lacks a "; then" at the end of the second line.
 
Old 10-24-2011, 08:55 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,498

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
I would add that there appears to be a 'done' at the end but for the life of me I see no for or while loops being started??

Also, as you are using bash, you can also use the following to minimise the number of checks you require:
Code:
${yn^}
This will convert whatever is entered (single character) to upper case.

You might even want to have a look at the =~ operator (assuming you stay with ifs)
 
Old 10-25-2011, 02:32 PM   #5
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
scratch tha\t making progress now

Last edited by Batistuta_g_2000; 10-25-2011 at 03:10 PM.
 
Old 10-26-2011, 02:38 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,498

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Firstly, please mark as SOLVED if you have what you need.

Secondly I doubt you are making any great progress as you have 4 to 5 questions in this on area which all seem to be with the same type of issue.
Just a view point by the way.
 
  


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
[SOLVED] Wifi not working in Eee PPC 1000HE (Debian Squeeze), but ethernet is working someshpr Linux - Laptop and Netbook 8 10-10-2011 03:59 PM
media problem with .avi --- audio only in vlc working rest amarock ,etc. not working fedex Linux - Newbie 1 12-22-2007 08:06 AM
SED - minor changes work - Larger doesn't (working and non working code included) Nimoy Programming 17 09-22-2007 04:34 PM
Fortemedia FM801 card not working under FC5 on Intel 845 but working with windows morningkiran Linux - Hardware 0 11-30-2006 07:57 AM
acpi nearly working IBM thinkpad R40e 2684HVG no Fn key working, throttling OK Emmanuel_uk Linux - Laptop and Netbook 2 05-31-2006 01:41 AM


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

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