LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash user input (https://www.linuxquestions.org/questions/linux-newbie-8/bash-user-input-909728/)

huntaz556 10-23-2011 10:46 PM

Bash user input
 
Okay i have a script thats doing a simple backup but i need user input and im baffled on how im doing this..

heres what i have right now

Code:

#/bin/bash

clear

read -p "Hello, "$USER". would you like to restore or backup your home directory? Enter restore or backup and press [ENTER] : "

[ "$REPLY" == "backup" ] ||  cd /home/$USER/ &&  tar -cf - /home/$USER/ | gzip -c > /home/$USER/backup.tar.gz && exit 

[ "$REPLY" == "restore" ] || cd /home/$USER/ && tar xvpfz HOME_BACKUP.tar.gz -C /home/$USER/ && exit

but the only thing it executes either way is the backup. even if i type restore what can i do to seperate them? your help is greatly appreciated thank you so much !

countach74 10-23-2011 10:56 PM

Check your syntax for read. You need to specify a variable to save the user input to. Try something like this:

read -p "your message to user here: " REPLY

huntaz556 10-23-2011 10:58 PM

hmm that didnt really help.. because there is a certain way the two replys need to be seperated i just dont know how.

countach74 10-23-2011 11:20 PM

It appears you are using || instead of && after the short hand if statement.

I would rewrite things a bit. Rather than chaining commands together, it would be easier to read and write, in my opinion, to do it more like this:
Code:

#!/bin/bash

function welcome {
  clear
  read -p "Hello, $USER. would you like to restore or backup your home directory? Enter restore or backup and press [ENTER] : " REPLY

  if [ "$REPLY" = "backup" ]; then
    cd /home/$USER
    tar czf /home/$USER/backup.tar.gz .
    exit
  elif [ "$REPLY" = "restore" ]; then
    cd /home/$USER
    tar xvpfz HOME_BACKUP.tar.gz
    exit
  else
    echo "Your selection was invalid. Please try again. Valid responses are: 'backup' and 'restore'."
    read -n 1
    welcome
  fi
}

welcome


huntaz556 10-23-2011 11:32 PM

Quote:

Originally Posted by countach74 (Post 4506203)
It appears you are using || instead of && after the short hand if statement.

I would rewrite things a bit. Rather than chaining commands together, it would be easier to read and write, in my opinion, to do it more like this:
Code:

#!/bin/bash

function welcome {
  clear
  read -p "Hello, $USER. would you like to restore or backup your home directory? Enter restore or backup and press [ENTER] : " REPLY

  if [ "$REPLY" = "backup" ]; then
    cd /home/$USER
    tar czf /home/$USER/backup.tar.gz .
    exit
  elif [ "$REPLY" = "restore" ]; then
    cd /home/$USER
    tar xvpfz HOME_BACKUP.tar.gz
    exit
  else
    echo "Your selection was invalid. Please try again. Valid responses are: 'backup' and 'restore'."
    read -n 1
    welcome
  fi
}

welcome


You helped me so much thank you !!! but i have one more question sorry .

so what happens is i do the backup option and what i get is this
Code:

Hello, huntaz. would you like to restore or backup your home directory? Enter restore or backup and press [ENTER] : backup
tar: Removing leading `/' from member names
tar: /home/huntaz/backup.tar.gz: file changed as we read it

and it dosent end it just hangs on that but the backup is successfuly completed

thanks again !

countach74 10-24-2011 12:05 AM

Yeah that's because the tar file is being written to the same path that you are backing up. A better idea would be to temporarily store the backup somewhere else like in /home or /var/backup or something like that. Once the tar is done then you can move the .tar.gz file back over to the wanted directory.

grail 10-24-2011 12:34 AM

I would like to add a few observations:

1. REPLY is the default variable used by read when no variable is supplied. So there is no need to include it (I would use an alternate variable name if you do wish
to add one)

2. You need to be careful when using multiple || and && tests. Sometimes you can get unexpected results if you think it will just exit after getting first error.

3. When performing multiple tasks it is often clearer to use an if construct as provided by countach74

4. Newer versions of tar allow you to simply use the 'a' switch which will select the correct compression based on the file name:
Code:

tar acf /home/$USER/backup.tar.gz .

countach74 10-24-2011 12:41 AM

Ahh, I didn't realize REPLY was the default variable name. Thanks for the input, grail.

catkin 10-24-2011 06:01 AM

Personally I find the case command less cluttered and more transparent for this type of thing
Code:

function welcome {
  clear
  read -p "Hello, $USER. would you like to restore or backup your home directory? Enter restore or backup and press [ENTER] : " REPLY

  case $REPLY in
    'backup' )
      cd /home/$USER
      tar czf /home/$USER/backup.tar.gz .
      exit
      ;;
    'restore' )
      cd /home/$USER
      tar xvpfz HOME_BACKUP.tar.gz
      exit
      ;;
    * )
      echo "Your selection was invalid. Please try again. Valid responses are: 'backup' and 'restore'."
      read -n 1
      welcome
  esac
}


huntaz556 10-24-2011 09:20 AM

Thank you guys so much you have helped me figure this out and clean it up ! you guys are awesome. What is the best way to access my flashdrive through the terminal like cd'ing into it? because a simple cd /dev/sdb dosent work.

grail 10-24-2011 09:42 AM

/dev/sdb is the device. You need to go to where it is mounted, generally /mnt or /media

huntaz556 10-24-2011 10:07 AM

Haha yes i formated it with the name Backup and then have it CD into /media/Backup and write it to that. but when i do that it says it cannot backup the file .gvfs but i still have about 1 GB of space left on my flashdrive but in the folder .gvfs there is nothing there even when i show hidden files

grail 10-24-2011 10:51 AM

Do an ls -l and make sure you have access to the item.

huntaz556 10-24-2011 10:55 AM

Quote:

Originally Posted by grail (Post 4506659)
Do an ls -l and make sure you have access to the item.

it is owned by me.

and when i try to mount my flashdrive after formatting it in the terminal and this is part of my script

Code:

sudo mount /dev/sdb
it says that it cant find /dev/sdb in fstab or mtab

i tried mounting it to a folder i made called usbflash in the mnt directory which worked but now i cant copy the backup to /mnt/usbflash it says i dont have permission even when using sudo

catkin 10-24-2011 11:13 AM

You might get more answers if you start a new thread for the new topic ...


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