LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-31-2008, 07:37 PM   #1
Shay
Member
 
Registered: Sep 2003
Posts: 77

Rep: Reputation: 15
Need help with a little bash script - generating unique folder names


I've got a few hundred of these. Want to throw them away, so I'm getting my niece to come over and take care of it. I want my script to do something like this:

mount -t vfat /dev/fd0 /mnt/floppy
mkdir /home/shay/uniquefoldername
cp /mnt/floppy/* /home/shay/uniquefoldername
umount /dev/floppy
echo "disc copied"

Doesn't need to automount or anything fancy. She can run the script via terminal. Generating the uniquefoldername is where I need help.

Thank you,
-Shay
 
Old 10-31-2008, 09:16 PM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Well, you can generate names in many ways.

If you know the number of disks beforehand, then you could do something like this:

Code:
#!/bin/bash

function die() {
  echo "[*] $1"
  exit 1 # Error
}

for disk in $(seq 0 99)
do
  i=$(printf "%02.0f" $disk)
  echo "Insert disk $i, and press ENTER to proceed."
  echo "(Abort at any time with Control+C)"
  read
  mount -t vfat /dev/fd0 /mnt/floppy >/dev/null 2>&1 || die "Error mounting the floppy device."
  mkdir "${HOME}/disk${i}" >/dev/null 2>&1 || die "Error creating the directory, maybe it already exists?"
  cp /mnt/floppy/* "${HOME}/disk${i}/"
  umount /dev/floppy >/dev/null 2>&1 || die "Error unmounting the device, maybe it's in use by another application."
  echo "Disc $i copied"
done

exit 0
I added some basic error handling stuff.

If what you wanted is some kind of random name, you can always use the $RANDOM var, which generates random strings, however if you do so you should first check that any randomly generated name doesn't exist (unlikely, but possible). This is a basic script that doesn't handle multiple sessions, it starts from 00 each time, so you might need to improve it a bit. Also, it's untested.

Last edited by i92guboj; 10-31-2008 at 10:02 PM. Reason: bolds are code I forgot the first time
 
Old 10-31-2008, 09:46 PM   #3
Shay
Member
 
Registered: Sep 2003
Posts: 77

Original Poster
Rep: Reputation: 15
That's almost it! I had to change umount /dev/floppy to umount /mnt/floppy. Only problem is that it doesn't wait for me to switch floppies. It copies the same floppy 100 times.

-Shay
 
Old 10-31-2008, 10:00 PM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by Shay View Post
That's almost it! I had to change umount /dev/floppy to umount /mnt/floppy. Only problem is that it doesn't wait for me to switch floppies. It copies the same floppy 100 times.

-Shay
Oh, yes hehe. I completely bypassed that.

You need to add some way to pause it. I will modify the script above in one minute.
 
Old 10-31-2008, 10:07 PM   #5
Shay
Member
 
Registered: Sep 2003
Posts: 77

Original Poster
Rep: Reputation: 15
Perfect. Thank you very much, i92guboj!

-Shay
 
  


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
bash - replace all spaces in file, folder names babag Programming 24 04-20-2008 12:17 AM
Shell Script to Get Folder Names firefoxlinux Programming 6 09-27-2007 02:56 PM
Generating 32-bit unique ID lucky6969b Programming 4 03-27-2006 08:59 AM
Bash Script- Finding/Generating unique UserIDs pheasand Linux - General 2 12-11-2004 09:44 AM
bash:output file names from shell script to vi sickboy Linux - Newbie 6 10-14-2004 03:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:20 PM.

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