LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash If statement (http://www.linuxquestions.org/questions/linux-newbie-8/bash-if-statement-909927/)

huntaz556 10-24-2011 07:05 PM

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 !

roreilly 10-24-2011 07:14 PM

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.

huntaz556 10-24-2011 07:16 PM

Quote:

Originally Posted by roreilly (Post 4507110)
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..

colucix 10-25-2011 02:12 AM

You missed the closing quotes in the echo statement at line 50.

huntaz556 10-25-2011 08:00 AM

Quote:

Originally Posted by colucix (Post 4507419)
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

roreilly 10-25-2011 08:26 AM

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'

colucix 10-25-2011 08:27 AM

Nope.
Code:

echo "Your backup is complete

huntaz556 10-25-2011 08:33 AM

now im getting this error.

Code:

./practice.sh: line 66: syntax error near unexpected token `}'
./practice.sh: line 66: `  }'


huntaz556 10-25-2011 08:37 AM

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

huntaz556 10-25-2011 09:07 AM

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



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