LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-14-2011, 09:36 AM   #1
badlyconfused
LQ Newbie
 
Registered: Dec 2011
Posts: 5

Rep: Reputation: Disabled
Christmas exams study....menu driven backup script.


Hi everyone,

I am studying for christmas exams and we have been given a sample paper and been told it will be nearly the same as the real one..

I am having a problem with the last questions (5)

In question 4 it asks to create an archive of a directory I have already (archivetest) of all files with a .txt extension

I do this by running:

tar cvf archivetest.tar *.txt

and this is successful.


Then question 5 asks to add a date and timestamp to the files...

I have tried adding $(date '+%m%y%d') in different locations in the script above but just cant get it working!


this is also stopping me from making a script using the above two answers which gives menu options to
tar a specific file
or
browse to select a file you want to tar
or
a collection of files containing a certain filename
or
a directory

If anyone could let me know what they think about this as I have been trying it for over a week now and the lecturer is not very forthcoming with helpful info except "look over your notes" -_-

Thanks in advance for any help!!!
 
Old 12-14-2011, 09:46 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by badlyconfused View Post
Then question 5 asks to add a date and timestamp to the files...

I have tried adding $(date '+%m%y%d') in different locations in the script above but just cant get it working!
If you want to add the timestamp to the tar archive file name, this should do it:
Code:
tar cvf archivetest.(date '+%m%y%d').tar *.txt
but it's not a very friendly timestamp -- from the appearance of nnnnnn, it could be myd, dym, ymd (which is the most sortable) ...
 
1 members found this post helpful.
Old 12-14-2011, 09:49 AM   #3
badlyconfused
LQ Newbie
 
Registered: Dec 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks Catkin

Ok that is working well now.

I think it was the $ sign. I was convinced it had to be part of the command!!

Any idea of the script? To be honest the lecturer was a bit "all over the place" and nothing is clear enough from notes to be able to write anything tht works!!
 
Old 12-14-2011, 10:49 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by badlyconfused View Post
Ok that is working well now.

I think it was the $ sign. I was convinced it had to be part of the command!!

Any idea of the script? To be honest the lecturer was a bit "all over the place" and nothing is clear enough from notes to be able to write anything tht works!!
It doesn't work for me; sorry, it should have been
Code:
tar cvf archivetest.$(date '+%m%y%d').tar *.txt
A useful technique to see what bash creates from a command like that is to echo it, here shown demonstrating that the $ is required:
Code:
c@CW8:~$ echo tar cvf archivetest.(date '+%m%y%d').tar *.txt
bash: syntax error near unexpected token `('
c@CW8:~$ echo tar cvf archivetest.$(date '+%m%y%d').tar *.txt
tar cvf archivetest.121114.tar *.txt
Regards the script, is this the specification:
Give menu options to tar:
  1. a specific file
    or
  2. browse to select a file you want to tar
    or
  3. a collection of files containing a certain filename
    or
  4. a directory
If so:
  1. OK -- just a fixed name.
  2. What sort of browse? Are you supposed to start a GUI and somehow get back the path of a file or is it a small number of files somewhere so you can generate a subsidiary menu listing them all to choose one by typing its number?
  3. Doesn't make sense. Does it mean a collection of files with the last path component including a fixed/specifiable string ("certain" is lousy English used that way)? If so under which directory?
  4. OK -- just a fixed name.
First rule of programming is to have a clear specification of what the program is required to do!
 
1 members found this post helpful.
Old 12-14-2011, 11:03 AM   #5
badlyconfused
LQ Newbie
 
Registered: Dec 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks Catkin

Sorry this is getting to me now I have been trying it for so long,

it is:

"a script to give menu options to choose to tar:

1:a specific file
2:browse to select a specific file
3:a collection of files containing requested text in their name
4:a driectory"

that is the actual questions.

He worder it badly but basically the script needs to tar whichever option is choosen and give it a date and time aswell....

This is just too confusing!!!!!!! >_<
 
Old 12-14-2011, 11:18 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
The tarring is easy now, you just need to script the menus. Given an ambiguous specification and no practicable opportunity to clarify it, you can interpret it as you want

How about this:

A script to give menu options to choose to tar:
  1. a specific file
  2. a specific file from a list of files in a specific directory
  3. several files in a specific directory with names containing a string entered by the user
  4. a specific directory's contents
You OK with that?
Assuming you are, here are commands to create a directory tree to test with:
Code:
mkdir ~/test_dir
cd ~/test_dir
touch foo.txt bar.txt foobar.txt
Now we come to the tricky part; I am willing to help you write the script but not to write it for you. First off, what do you know about how to script a menu?
 
Old 12-14-2011, 11:26 AM   #7
badlyconfused
LQ Newbie
 
Registered: Dec 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
HEre is what he has given us as a sample...........


your own menu driven backup script
Apart from being a reasonably practical exercise in learning bash scripting, it might also be
vaguely useful, and reasonable guidelines are given, so it should not take too long to do – a
few hours at most.
BUT THINK, PLAN AND EVEN CODE BEFORE GOING TO THE LAB.
THEN CHECK IT OUT A PART AT A TIME, BEFORE JAMMING IT ALL TOGETHER.
Later, (not as an assignment, but as ongoing demonstrations) we can look at modifications
and extensions to it, which would make it more useful, such as incremental backup, (backing
up only changed files etc.) and varying the underlying backup command, from tar to
something else.
The assignment is designed in relatively independent component parts, so I donʼt expect you
all to get it all worked out, and you get marks for what is accomplished. So it is not an ʻall or
nothingʼ assignment.
The numeric orderings below indicate the order in which things (c/sh)ould be done, which is
effectively
from the inside :-
1) the basic backup command : tar as youʼve used in your last assignment
to the outside : basically naming the archive.
2) with a filetype and timestamp in the name
(I know that files have system times intrinsically associated with them,
but these are often changed in copying and backing up, so it is handy to have the real
original time as part of the name : besides you can see at a glance the timestamp from
the archive name!)
3) with a menu to allow the user to select the options: file, directory etc.
GUIDELINES
Tar basically works as: tar flags $source $destination
So you have to modify the strings for source and destination
Remember strings are concatenated simply by
$c=$a + $b + “whatever you like yourself”
Part 3 is basically choosing and setting the source string, from the source files
Part 2 is basically setting the destination from string for the archive name,
Which includes some filetype info from source + the timestamp when the script ran.
Part 1 is basically issuing the archive command : tar flags $source $destination
FORMAL SPECIFICATIONS … APPLY ONLY TO FILES IN THE CURRENT DIRECTORY.
(You can assume there are no directories within; so the scriptester directory will do fine!)
So here is the formal spec…insofar as formality is useful…
3) Give menu options for user to choose which files etc. to archive…
a) a specific file [10%]
b) browse to select a specific file [10%]
(merely an ls and then repeat menu for option a) is easiest) [20%]
c) a collection of files containing requested text in their name [10%]
d) the current directory [20%]
the archive_name is to be set according to the name of the
2) Include some source information in the archive name
a) filetype / file extension to the archive file [10%]
b) along with date and time of archive [10%]
(Here's a code snippet along those lines)
# the timestamp
this_run=`/bin/date +%F+%T` # e.g. 2011-05-06+10:56:59
# to clean up date strings for appending to filenames, replace all non-numeric symbols with '_'
# e.g. from 2011-05-06+10:56:59 to 2011_05_06_10_56_59
# (other chars may confuse filesystem)
this_run="`echo $this_run | sed -e s/[^0-9]/_/g `"
and just include $this_run as part of the tar archive filename...
tar sourcepath tarred_road/$archive_name_$thisrun [10%]
1) tar whatever is asked in menu 3...
setting the archive_name according to selections from menu input..
Please do not try to implement an untar or dearchive option, as youʼll have enough to do…it
could be done by generating a toc from the archive, listing it like the browse option 3b) and
then allowing an extraction as in 3a) – but it would only complicate issues needlessly.



I cant make senes of this to be honest. He had shown us quickly how to script menus but not given concise notes.....would you have any suggestions as to what to do etc?

Im not asking for you to write the actual script just give me a general idea or any helpful links or websites?.....
 
Old 12-14-2011, 11:37 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Thanks -- that's a bit clearer now. The easiest way to generate a menu is the select command. Example here (scroll down ~80%) and reference here (scroll down to select).

Post back when you get stuck or when you have a basic menu working. The commands to run when the user chooses a menu option can be added later. When posting the script or its output, used CODE tags -- easiest in "Advanced Mode" using the # button.
 
Old 12-14-2011, 02:21 PM   #9
badlyconfused
LQ Newbie
 
Registered: Dec 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Ok thanks...Ill try again in the morning and see how far I can get!

Thanks again.
 
Old 12-14-2011, 02:39 PM   #10
asimba
Member
 
Registered: Mar 2005
Location: 127.0.0.0
Distribution: Red Hat / Fedora
Posts: 355

Rep: Reputation: 42
I would have modelled mine on Amanda pattern.

I would have take following basic options

1. Create backup
a. Specify Source file/directory
*verify if source directory already exists - verify file version/date/time etc - option to overwrite existing file or rename existing destination file or use new destination name

2. Delete Backup
a. Specify backup location
3. Verify Backup
b. specify backup location to verify
4. Exit

I also would have used a configuration file to save last location/last backup done and so forth so as to make life easy + not to miss out logging. So forth
I apologize if I missed context
 
Old 01-07-2013, 05:35 AM   #11
joepeger
LQ Newbie
 
Registered: Jan 2013
Posts: 4

Rep: Reputation: Disabled
I'm doing the same question at the moment. I know this is > 12 months late but was a solution found to this problem?

Last edited by colucix; 01-11-2013 at 10:42 AM. Reason: Partially restored original content.
 
Old 01-07-2013, 09:40 AM   #12
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,921

Rep: Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690
Quote:
Originally Posted by joepeger View Post
I'm doing the same question at the moment. I know this is > 12 months late but was a solution found to this problem?
Post #8 has two links that essentially solve your problem. It tells you how to write a menu-driven script, how to do input verification, and has full examples. Modify them to suit your needs.

Last edited by colucix; 01-11-2013 at 10:42 AM.
 
Old 01-07-2013, 02:34 PM   #13
joepeger
LQ Newbie
 
Registered: Jan 2013
Posts: 4

Rep: Reputation: Disabled
Thanks TB0ne!

Last edited by colucix; 01-11-2013 at 10:43 AM.
 
Old 01-07-2013, 02:52 PM   #14
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,159
Blog Entries: 10

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
I hope this doesn't get me kicked off the LQ Christmas Card List...

Basic Linux Menu « Bash recipes
ActiveState Code A menu box - Linux Shell Scripting Tutorial - A Beginner's handbook
bash select menu example
Menu Driven Shell Script
 
Old 01-08-2013, 11:00 AM   #15
joepeger
LQ Newbie
 
Registered: Jan 2013
Posts: 4

Rep: Reputation: Disabled
Clueless

Thanks for your help thus far:

I think I have completed part a) and part b) but I'm having trouble with parts c) and d). Part c) I really don't have a clue about, part d) I'm not sure what's wrong because it feels like what I have should work. Any help would be greatly appreciated.

Cheers,
Joe.



QUESTIONS:

3) Give menu options for user to choose which files etc. to archive…
a) a specific file [10%]
b) browse to select a specific file (merely an ls and then repeat menu for option a) is easiest) [30%]
c) a collection of files containing requested text in their name [10%]
d) the current directory [20%]

WHAT I HAVE COMPlETED:

#!/bin/bash

selection=
until [ "$selection" = "0"]; do

echo ""
echo "PROGRAM MENU"
echo "1 - Archive specific files"
echo "2 - Browse to select a specific file"
echo "3 - Archive files containing specific characters"
echo "4 - Archive current directory"
echo ""
echo "0 - Exit program"
echo -n "Enter Selection: "
read selection
echo ""
case $selection in

1 ) echo "This archives files"
echo "Please type name of file to archive"
read archivefiles
tar cvf archivetest.$(date '+%m%y%d').tar $archivefiles;;

2 ) echo "Browse and select a file to archive"
ls -l
read archivefiles
tar cvf archivetest.$(date '+%m%y%d').tar $archivefiles;;

3 ) echo "This archives files containing specific characters"
read ???
tar cvf archivetest.$(date '+%m%y%d').tar $???;;

4 ) echo "Select Current Directory:"
read cd
tar cvf archivetest.$(date '+%m%y%d').tar $cd;;

0 ) exit;;

* ) echo "Please enter 1,2,3,4 or 0"

esac
done
 
  


Reply


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
Appropriate Hardware to study for the RHCSA/RHCE exams mike_rhce Linux - Certification 8 12-05-2011 07:10 PM
Menu driven backup script? ali0123 Programming 2 08-20-2011 10:34 AM
Needed study material for RHCE exams nithinkumaar Linux - Newbie 1 12-21-2005 03:48 AM
dselect menu-driven GUI mullog Programming 1 06-14-2004 04:44 PM


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