LinuxQuestions.org
Visit Jeremy's Blog.
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-2010, 07:51 AM   #1
Dale255
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Rep: Reputation: 0
Archiving Bash Script


Hello People,

I'm relatively new to Linux in general but have learned to do the basics with the CLI.

Well my main problem is writing my first "real" script in VIM. I just have no idea where to start. I was hoping you guys could point me in the right direction.

Well this is what the script needs to do.


"As the IT administrator for a large manufacturing company you have been tasked with producing a script for archiving the daily log files. Each week the daily log files are placed in a directory named weekXX, where XX is the number of the week. Each week directory should be archived and compressed and stored in a folder called log_directories. When the script has completed the task it should display on the monitor exactly what files and directories it has archived.

The script should be started by a user with the required week numbers added as arguments (e.g prog 13 14 15 should start the program and archive the daily log files in week13, week14 and week15).

A basic manual is required, showing how to start the program, the hardware and / or software requirements, a hard copy of the script and a brief description of the test strategy and test data."
 
Old 12-14-2010, 07:57 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
LinuxQuestions.org has a policy not to do peoples homework problems. If you show what you have tried so far, and have particular questions on your code, we can help you with that.
 
Old 12-14-2010, 08:25 AM   #3
Dale255
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
How I wish it was homework, then I might have a teacher to help me out, Its an example from an old book I picked up at the library.
It explains ( very badly I may add ) what test conditions are and how to do them. But there is no reference to compression or archiving. So I was wondering how I would go about doing that?
 
Old 12-14-2010, 09:42 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,423

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
So a starting point would be to have a bash reference, see here.

Then you need top learn about one of your best friends in the linux world, the 'man' command. To start do a 'man man' and you will get the idea.

As for archiving, a simple search on google will show you that tar is a hot favourite.

Lastly, again with the help of google, compression comes in many forms, but may is suggest looking up - gzip, bzip2 and maybe lzma (or xz)

Let us know how you get on?
 
Old 12-14-2010, 09:54 AM   #5
Dale255
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
Really appreciated. The link will be very helpful thank you.

This is what i have so far.

~~~ begin of file
#!/bin/sh
week = $1
mv $LOGIFILES/week$week /dev/null
cp /dev/null $LOG_DIRECTORIES
echo "saved" | wc -l $LOG_DIRECTORIES

~~~ end of file

I really dont like the look for the /dev/null part. Can anyone tell me if its dodgy command?

Being 40 and learning linux is hard work!


This is with a little help from a friend. But it does not seem to be working at all I get is "bin/sh bad interpreter : no such file or directory"

Hope you can help.

Thanks Dalila.


Edit : I think the example my so called "friend" gave me was to do a diskdump. Dam some Linux people are unfriendly

Last edited by Dale255; 12-14-2010 at 04:34 PM.
 
Old 12-14-2010, 10:15 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
Quote:
Originally Posted by Dale255 View Post
#and just to be sure that everything is cleaned up:
rm -rf /
Please please please do not run that!!!
 
Old 12-14-2010, 10:17 AM   #7
Dale255
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
It's okay i might be a bit dim when it comes to BASH, But it was pretty clear what it was when I first seen it. Thanks for the warning anyway.
 
Old 12-14-2010, 10:39 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
Quote:
Originally Posted by Dale255 View Post
It's okay i might be a bit dim when it comes to BASH, But it was pretty clear what it was when I first seen it. Thanks for the warning anyway.
Phew!

Let's go by small steps. First that "bin/sh bad interpreter : no such file or directory" which should be "/bin/sh bad interpreter : no such file or directory". It does help if you can copy and paste from the terminal into the thread so we know exactly what you have seen with no risk of typos; do you know how to do that?

Please post the output of:
Code:
/bin/ls -l /bin/sh
cat /etc/shells

Last edited by catkin; 12-14-2010 at 10:40 AM. Reason: know -> no
 
Old 12-14-2010, 10:45 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,423

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Well my general first tip to a beginner is if you can do it on the command line then you can generally do it in a script.
So my question to you would be, what happens if you run the following on the command line:
Code:
mv $LOGIFILES/week$week /dev/null
So the hard part here might be that the directories don't exist yet so let us make it simple:
Code:
$ mkdir test_dir
$ mv test_dir /dev/null
Give that a go and then see what you think?

Also, as a personal thing, having a variable with the same name as something else you are dealing with and no capitalisation to show it is different can lead to headaches later.
Generally in about 2 - 3 weeks when you go back and look at this example and you have forgotten what is what.
 
Old 12-14-2010, 10:55 AM   #10
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,601
Blog Entries: 25

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Hi,

I like to recommend the following to a newbie or someone that is finally deciding to actually do something other than point & click.


These links will aid you to gaining some understanding. Sure some may seem beyond a newbie but you must start somewhere;



Linux Documentation Project
Rute Tutorial & Exposition
Linux Command Guide
Utimate Linux Newbie Guide
LinuxSelfHelp
Getting Started with Linux
Bash Beginners Guide
Bash Reference Manual
Advanced Bash-Scripting Guide
Linux Home Networking



The above links and others can be found at 'Slackware-Links'. More than just SlackwareŽ links!
 
Old 12-14-2010, 10:56 AM   #11
Dale255
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
Phew!

Let's go by small steps. First that "bin/sh bad interpreter : no such file or directory" which should be "/bin/sh bad interpreter : no such file or directory". It does help if you can copy and paste from the terminal into the thread so we know exactly what you have seen with no risk of typos; do you know how to do that?

Please post the output of:
Code:
/bin/ls -l /bin/sh
cat /etc/shells
lrwxrwxrwx 1 root root 4 2010-11-22 17:59 /bin/sh -> dash



# /etc/shells: valid login shells
/bin/csh
/bin/sh
/usr/bin/es
/usr/bin/ksh
/bin/ksh
/usr/bin/rc
/usr/bin/tcsh
/bin/tcsh
/usr/bin/esh
/bin/dash
/bin/bash
/bin/rbash
/usr/bin/screen




I think i should have said the files i want to archive are in /home/macq/assessment3
 
Old 12-14-2010, 11:17 AM   #12
Dale255
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by onebuck View Post
Hi,

I like to recommend the following to a newbie or someone that is finally deciding to actually do something other than point & click.


These links will aid you to gaining some understanding. Sure some may seem beyond a newbie but you must start somewhere;



Linux Documentation Project
Rute Tutorial & Exposition
Linux Command Guide
Utimate Linux Newbie Guide
LinuxSelfHelp
Getting Started with Linux
Bash Beginners Guide
Bash Reference Manual
Advanced Bash-Scripting Guide
Linux Home Networking



The above links and others can be found at 'Slackware-Links'. More than just SlackwareŽ links!

Thank you very much.

I have the information I need to make this script. But it is just putting it together in to one that I don't understand.
Is there any one of those links that would be specificity good for that?
 
Old 12-14-2010, 11:23 AM   #13
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
First I'll say that I've been using Linux for over 15 years and making scripts still gives me fits. I suppose that I just don't "get it" or something, but I've written backup scripts in bash several times.

Your specifications indicate that your backups should be contained in archive files, just like in WinZip archive files.

You have done what catkin asked. Now what does it mean? If you look at the output of the ls -l /bin/sh command you can see that it has an arrow next to /bin/sh and that arrow points to dash. Now if you look in the listing of /etc/shells you can see that /bin/dash is one of the items listed. That's good. The /etc/shells file simply contains a list of the allowed command line interpreters. If you performed an ls -l /bin/dash it would look like a regular file, which is to say that it would not have an arrow pointing to anything else.

So the first line of your bash script looks like a comment and it tells Linux which command line interpreter to use when executing your script. The first line of your bash script should look like this.
Code:
#!/bin/sh
You should decide where you want to put the archives. I recommend putting them on a different disk drive. This disk drive would have to be mounted. Linux mounts disk drives on directories. Windows can do this as well but normally Windows makes disk drives (disk drive partitions) look like separate devices. This is not the case in Linux. Mounted disk drive partitions just look like any other directory. Before you can put files onto a disk partition you have to mount it somewhere. You can do this using the mount command.
Example: a normal user wants to mount disk partition /dev/sdb3 on /mnt/sdb3.
Code:
sudo mount /dev/sdb3 /mnt/sdb3
Note that the directory /mnt/sdb3 must exist prior to entering this command.

As catkin said we should take baby steps so get comfortable with what I have written and then come back.

Last edited by stress_junkie; 12-14-2010 at 11:30 AM.
 
Old 12-14-2010, 12:41 PM   #14
Dale255
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
Thank you very much Stress_junkie. I think I will keep it simple and store the files in /home/log_directories.

I understand that it starts with #!/bin/sh

Do I need to check if the file exists by using "if [ -f $filename ]"?

then if I does move it to "log_directories" then compress it?
 
Old 12-14-2010, 01:36 PM   #15
barriehie
Member
 
Registered: Nov 2010
Distribution: Debian Lenny
Posts: 136
Blog Entries: 1

Rep: Reputation: 23
Quote:
Originally Posted by Dale255 View Post
Thank you very much Stress_junkie. I think I will keep it simple and store the files in /home/log_directories.

I understand that it starts with #!/bin/sh

Do I need to check if the file exists by using "if [ -f $filename ]"?

then if I does move it to "log_directories" then compress it?
That would be a good idea. If you try to execute a command on a file that doesn't exist your script could produce failures/weird results. Note that the if/then construct also comes with an else clause; it can make life easier.
 
  


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
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 09:37 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 09:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 07:00 AM
Is this possible (file archiving/tidying script) mrgreaper Linux - Newbie 3 07-25-2008 01:25 PM


All times are GMT -5. The time now is 05:41 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