LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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-23-2011, 10:46 PM   #1
huntaz556
LQ Newbie
 
Registered: Oct 2011
Posts: 22

Rep: Reputation: Disabled
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 !
 
Old 10-23-2011, 10:56 PM   #2
countach74
Member
 
Registered: Feb 2011
Distribution: Ubuntu 10.04, Debian Squeeze
Posts: 46

Rep: Reputation: 8
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
 
Old 10-23-2011, 10:58 PM   #3
huntaz556
LQ Newbie
 
Registered: Oct 2011
Posts: 22

Original Poster
Rep: Reputation: Disabled
hmm that didnt really help.. because there is a certain way the two replys need to be seperated i just dont know how.
 
Old 10-23-2011, 11:20 PM   #4
countach74
Member
 
Registered: Feb 2011
Distribution: Ubuntu 10.04, Debian Squeeze
Posts: 46

Rep: Reputation: 8
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

Last edited by countach74; 10-23-2011 at 11:22 PM. Reason: All kinds of fail... fixed problems
 
1 members found this post helpful.
Old 10-23-2011, 11:32 PM   #5
huntaz556
LQ Newbie
 
Registered: Oct 2011
Posts: 22

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by countach74 View Post
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 !
 
Old 10-24-2011, 12:05 AM   #6
countach74
Member
 
Registered: Feb 2011
Distribution: Ubuntu 10.04, Debian Squeeze
Posts: 46

Rep: Reputation: 8
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.
 
Old 10-24-2011, 12:34 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
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 .
 
Old 10-24-2011, 12:41 AM   #8
countach74
Member
 
Registered: Feb 2011
Distribution: Ubuntu 10.04, Debian Squeeze
Posts: 46

Rep: Reputation: 8
Ahh, I didn't realize REPLY was the default variable name. Thanks for the input, grail.
 
Old 10-24-2011, 06:01 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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
}
 
Old 10-24-2011, 09:20 AM   #10
huntaz556
LQ Newbie
 
Registered: Oct 2011
Posts: 22

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-24-2011, 09:42 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
/dev/sdb is the device. You need to go to where it is mounted, generally /mnt or /media
 
Old 10-24-2011, 10:07 AM   #12
huntaz556
LQ Newbie
 
Registered: Oct 2011
Posts: 22

Original Poster
Rep: Reputation: Disabled
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
 
Old 10-24-2011, 10:51 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Do an ls -l and make sure you have access to the item.
 
Old 10-24-2011, 10:55 AM   #14
huntaz556
LQ Newbie
 
Registered: Oct 2011
Posts: 22

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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
 
Old 10-24-2011, 11:13 AM   #15
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
You might get more answers if you start a new thread for the new topic ...
 
  


Reply



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
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM
Getting asynchronous user input in BASH jakeo25 Linux - Software 1 05-08-2008 06:22 PM
Bash Y/N user input zcrxsir88 Programming 11 04-16-2008 11:35 AM
Bash scripting and user input Woodsman Slackware 13 11-02-2005 02:20 PM
User input using a BASH script... causticmtl Programming 5 07-13-2003 09:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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