Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 !
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
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
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.
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:
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
}
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.
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.