LinuxQuestions.org
Review your favorite Linux distribution.
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, 08:05 PM   #1
huntaz556
LQ Newbie
 
Registered: Oct 2011
Posts: 22

Rep: Reputation: Disabled
Smile Bash If statement


Hi everyone again im back with more questions about Bash scripts

so i have my bash script and i have an if statement followed by elif statement and i would like to put an if statement inbetween the other if statements but if i do then it gives me an error saying somthing is wrong at the end of the script can you please help me thank you !
 
Old 10-24-2011, 08:14 PM   #2
roreilly
Member
 
Registered: Aug 2006
Location: Canada
Distribution: Debian, Slackware
Posts: 106

Rep: Reputation: 28
huntaz556,

Can you post your script, and the error output your receive? That will help us assist you.

You probably just need to sort out the nesting of your "if" statements and close them in the correct order.

Regards,
R.
 
Old 10-24-2011, 08:16 PM   #3
huntaz556
LQ Newbie
 
Registered: Oct 2011
Posts: 22

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by roreilly View Post
huntaz556,

Can you post your script, and the error output your receive? That will help us assist you.

You probably just need to sort out the nesting of your "if" statements and close them in the correct order.

Regards,
R.
Yes i will ! here you go !

Code:
#/bin/bash
# Init
FILE="/tmp/out.$$"
GREP="/bin/grep"
#....
# Make sure only root can run our script
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root Type su into your terminal and enter your password and try to run this script again" 1>&2
   exit 1
fi
function welcome {
clear
mkdir /mnt/usbflash/
#read -p command asks for user input on what to do

read -p "Hello, "$USER". would you like to restore or backup your home directory? Enter restore or backup and press [ENTER] : "
echo
echo
echo
read -p "Please Specify which user's home directory you would like to Backup/restore and press [ENTER] : " THUSER
echo
echo
echo
# if [ "$REPLY"] statements are to tell what to do with the user input
if [ "$REPLY" == "backup" ]; then
  #read -p "Would you like to backup to an external drive or store the backup on your harddrive? for external drive type ed for harddrive type hdd and press [ENTER] : " WHERE
  read -p "Please specify what you would like the backup to be called then press [ENTER] : " BACKUP
  read -p "Would you like to back up to a external drive or your harddrive? for external drive type ed for harddrive type hdd and press [ENTER] : " WHERE
  read -p "please specify which directory you would like backed up example /home/"$USER"/ : " DIR
  if [ "$WHERE" == "ed" ]; then
  sudo umount /dev/sdb
  read -p "Would you like to format your flashdrive ? (y/N) : " FORMAT
  [ "$FORMAT" == "y" ] 
  sudo mkfs.vfat -n Backup -I /dev/sdb
  echo "Format complete"
  cd /mnt/usbflash
  sudo mount /dev/sdb/ /mnt/usbflash
  cd /mnt/usbflash/ 
  sudo tar -cf - "$DIR" | gzip -c > /mnt/usbflash/"$BACKUP".tar.gz
  echo "Your backup is complete"
 exit
["$FORMAT" == "n"]
  cd /mnt/usbflash
  sudo mount /dev/sdb/ /mnt/usbflash/
  cd /mnt/usbflash
  sudo tar -cf - "$DIR" | gzip -c > /mnt/usbflash/"$BACKUP".tar.gz
  echo "Your backup is complete."
elif [ "$WHERE" == "hdd" ]; then
  sudo tar -cf - "$DIR" | gzip -c > /mnt/usbflash/"$BACKUP".tar.gz
  echo "Your backup is complete
  
elif [ "$REPLY" == "restore" ]; then 
  read -p "Warning this will overwrite the selected directory with the files in this backup do you want to continue? (y/n) : " CONTINUE
[ "$CONTINUE" == "y" ]
  cd /mnt/usbflash
  find -type f -name \*.tar.gz 
  read -p "type the name of your file without the .tar.gz extension and press [ENTER]"
  sudo tar xvpfz "$RESTORENAME".tar.gz -C /home/"$THUSER"/ 
  exit
  [ "$CONTINUE" == "n" ]
  exit
  else
  echo "Your selection was invalid. Please try again. Valid responses are: 'backup' and 'restore'."
  welcome
  fi
  }


  
  welcome
what im trying to do is add more options between the if [ "$REPLY" ] statement or somthing that will work out the same

and my error

Code:
root@huntaz-ThinkPad-R61i:/home/huntaz# back
/home/huntaz/script.sh: line 53: syntax error near unexpected token `('
/home/huntaz/script.sh: line 53: `  read -p "Warning this will overwrite the selected directory with the files in this backup do you want to continue? (y/n) : " CONTINUE'
there it is saying one of the read -p's is wrong..

Last edited by huntaz556; 10-24-2011 at 08:35 PM.
 
Old 10-25-2011, 03:12 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You missed the closing quotes in the echo statement at line 50.
 
Old 10-25-2011, 09:00 AM   #5
huntaz556
LQ Newbie
 
Registered: Oct 2011
Posts: 22

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
You missed the closing quotes in the echo statement at line 50.
on this line ? sudo tar -cf - "$DIR" | gzip -c > /mnt/usbflash/"$BACKUP".tar.gz
 
Old 10-25-2011, 09:26 AM   #6
roreilly
Member
 
Registered: Aug 2006
Location: Canada
Distribution: Debian, Slackware
Posts: 106

Rep: Reputation: 28
You may also need to escape your (y/n) statements. I get errors on my system when running the script.

Change them to \(y/n\) and they won't be interpreted by bash.

Test your scripts using the sh -x <script> to step through, and correct the errors as you find them.

Additionally, I believe you are missing at least one closing 'fi'
 
Old 10-25-2011, 09:27 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Nope.
Code:
echo "Your backup is complete
 
Old 10-25-2011, 09:33 AM   #8
huntaz556
LQ Newbie
 
Registered: Oct 2011
Posts: 22

Original Poster
Rep: Reputation: Disabled
now im getting this error.

Code:
./practice.sh: line 66: syntax error near unexpected token `}'
./practice.sh: line 66: `  }'
 
Old 10-25-2011, 09:37 AM   #9
huntaz556
LQ Newbie
 
Registered: Oct 2011
Posts: 22

Original Poster
Rep: Reputation: Disabled
haha ! i did the sh -x statement and went through it and i got this

Code:
./practice.sh: 66: Syntax error: "}" unexpected (expecting "fi")
but dont i need the }? to end the function and i have "fi" before it but without quotes
 
Old 10-25-2011, 10:07 AM   #10
huntaz556
LQ Newbie
 
Registered: Oct 2011
Posts: 22

Original Poster
Rep: Reputation: Disabled
AHH

i did fi } and it got rid of that error but now im getting an input/output error

so i moved the } below the fi and now it says that its not supposed to be there..

but there is another error >.<

Code:
: 1: unexpected operator
+ welcome
./practice.sh: 1: welcome: not found

Last edited by huntaz556; 10-25-2011 at 10:10 AM.
 
  


Reply

Tags
bash, script, statement


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
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 07:00 AM
bash if statement jstu Programming 7 02-15-2008 08:48 PM
Bash if statement noir911 Programming 2 08-20-2006 06:43 AM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 06:20 PM
bash statement os2 Programming 2 03-20-2005 11:13 PM


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