LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 06-06-2004, 04:04 PM   #1
hamish
Member
 
Registered: Aug 2003
Location: Edinburgh
Distribution: Server: Gentoo2004; Desktop: Ubuntu
Posts: 720

Rep: Reputation: 30
help with a very small script


Hello!

I'm brand new to bash scripting and wrote (with some help) a script this afternoon which I used to move some of my flatmates AVIs (on CD) to my hard drive.

I have now moved all of them and won't be needing the script anymore, however, it struck me that I might use a very similar script again, only slightly modified.

What my script does at the moment is just grab ALL the files from one location and copy them to another. I would like to modify it slightly so that there is an option to ask the user what files he would like to move.

Here is my current script. It is very simple and self explanitory. Feel free to run it if you want, just remember to change the source directory and the target directory.

HERE IS THE SCRIPT

Code:
#Currently, it copys from /mnt/cdrom to /mnt/trek/TNG_Season1
                                                                                                                              
echo "Please enter CD into drive."
#TODO: MOUNT FAILED. EJECT CD BAY. "PLEASE INSERT CD."
                                                                                                                              
                                                                                                                              
echo "I am now mounting the CD drive."
mount /mnt/cdrom
echo "CD drive mounted successfully."
                                                                                                                              
                                                                                                                              
echo "Are you ready to copy the tracks to the hard drive? (y/n)"
read answer
                                                                                                                              
#TODO: WHICH FILES WOULD YOU LIKE TO MOVE?
#TODO: WHERE WOULD YOU LIKE THE FILES?
                                                                                                                              
case "$answer" in
[yY]) echo "The files are copying. This may take some time."
     cp -v /mnt/cdrom/* /mnt/4/TNG_Season1
     echo "All files have been copied from the CD to the hard drive."
     echo "I will now eject the CD."
     eject /mnt/cdrom
     ;;
                                                                                                                              
*) echo "The files were *NOT* copied.";;
                                                                                                                              
esac
                                                                                                                              
#echo "All files have been copied from the CD to the hard drive."
                                                                                                                              
#echo "I will now eject the CD."
                                                                                                                              
eject /mnt/cdrom

echo "Thank you for using my script!"
Thanks for your time and I hope someone can point me in the right direction.

Hamish
 
Old 06-06-2004, 04:17 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
It depends how you want to specify the files - I can see 2 ways:
1) Prompt for y/n on each file
2) Select then in one go using a regular expression (the easiest way to do this would be in conjunction with the find command and a -exec statement)

Just as a hint for the other things you want to do like checking if the mounts fail etc, check the exit code of the mount command. It will be stored in the variable "$?". In almost all circumstances a 0 will show success and any other number will be a failure of some sort.
 
Old 06-06-2004, 04:18 PM   #3
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 47
Command line arguments can be read by using $1. So to copy a certan files and check for a destination directory ( using -dest as an option ) you could have

Code:
# check for command line arguments
if [ $# -ne 0 ]; then
   # loop through arguments
   until [ -z $1 ]; do
      if [ $1 = -dest ]; then
            shift
            DESTDIR=$1
      else
            files[numfiles++]=$1
      fi
   done
   if [ files -eq 0 ]; then
      ALLFILES=true
else
   ALLFILES=true
fi
The reading ${files[i]} for files, will give you the list of files. ALLFILES will be true if you need to copy all the files.

Theres lost of other ways as well. ( The example code might not work I haven't checked it ).

Last edited by leonscape; 06-06-2004 at 04:19 PM.
 
Old 06-09-2004, 02:10 AM   #4
hamish
Member
 
Registered: Aug 2003
Location: Edinburgh
Distribution: Server: Gentoo2004; Desktop: Ubuntu
Posts: 720

Original Poster
Rep: Reputation: 30
Hey. Thanks for both your suggestions.

I was thinking about just the same idea for the CDrom check, but I didn't know where to find the exit code would be.

Hamish
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
small script Kassel Programming 2 11-19-2004 04:22 AM
Need help with very small script. Franklin Programming 8 10-14-2003 12:37 PM
Please Help with a small script ? juglugs Programming 2 11-14-2001 02:39 AM
Small Script msj Linux - Newbie 2 08-27-2001 06:59 AM
small script msj Programming 1 08-26-2001 11:42 AM


All times are GMT -5. The time now is 03:53 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration