Sponsored Content
Top Forums Shell Programming and Scripting Batch write multiple usb in same time Post 302574792 by felipe.vinturin on Friday 18th of November 2011 10:21:43 AM
Old 11-18-2011
Try this:
Code:
checkRetCode ()
{
	lstRetCode=${?}
	lineNo="${1}"
	
	if [ ${lstRetCode} -ne 0 ]
	then
		echo "Last command returned and invalid code: [${lstRetCode}]. Line number: [${lineNo}]."
		exit 1
	fi
}

processUsbDev ()
{
	usbDevNamesCSV="${1}"
	baseMountPoint="${2}"
	numDirs="${3:-999}"
	numRetries="${4:-3}"

	usbDevBase="/dev"

	OIFS="${IFS}"
	IFS=","
	for usbDevName in ${usbDevNamesCSV}
	do
		usbDev="${usbDevBase}/${usbDevName}"
		
		echo "Formatting usb disk: [${usbDev}]"
		mkfs.vfat -F32 "${usbDev}"
		checkRetCode "${LINENO}"

		mountPoint="${baseMountPoint}/${usbDevName}"
		if [ ! -d "${mountPoint}" ]
		then
			mkdir "${mountPoint}"
			checkRetCode "${LINENO}"
		fi
		
		echo "Mounting disk: [${usbDev}] under: [${mountPoint}]"
		mount ${usbDev} ${mountPoint}
		checkRetCode "${LINENO}"
	done
	IFS="${OIFS}"

	countDirs=1
	countDevs=1
	
	numOfDevs=`echo "${usbDevNamesCSV}" | awk -F"," '{print NF}'`
	
	while [ ${countDirs} -le ${numDirs} ] 
	do
		[ ${countDevs} -gt ${numOfDevs} ] && countDevs=1

		countDirStr=`echo "${countDirs}" | awk '{printf("%03d\n", $0)}'`
		usbDevName=`echo "${usbDevNamesCSV}" | awk -F"," -v usbDev="${countDevs}" '{print $usbDev}'`
		
		mountPoint="${baseMountPoint}/${usbDevName}"
		createDir="${mountPoint}/${countDirStr}"
		
		echo "Creating directory under: [${mountPoint}] - [${countDirStr}][${usbDevName}]..."
		
		countRetries=0
		while true
		do
			mkdir "${createDir}"
			retCode=${?}
			if [ ${retCode} -ne 0 ]
			then
				if [ ${countRetries} -ge ${countRetries} ]
				then
					echo "Failed to create directory: [${createDir}]!"
					break
				fi
			else
				break
			fi
			countRetries=`expr ${countRetries} + 1`
		done
		
		countDevs=`expr ${countDevs} + 1`
		countDirs=`expr ${countDirs} + 1`
	done
	
	OIFS="${IFS}"
	IFS=","
	for usbDevName in ${usbDevNamesCSV}
	do
		usbDev="${usbDevBase}/${usbDevName}"
		umount ${usbDev}
		if [ ${?} -ne 0 ]
		then
			echo "Failed to umount: [${usbDev}]."
		fi
	done
	IFS="${OIFS}"
}

echo "Press any key to start!"
read

processUsbDev "sdc1,sdc2,sdc3,sdc4" "/media/usb" "999" "3"

And a sample output:
Code:
Press any key to start!

Formatting usb disk: [/dev/sdc1]
Mounting disk: [/dev/sdc1] under: [/media/usb/sdc1]
Formatting usb disk: [/dev/sdc2]
Mounting disk: [/dev/sdc2] under: [/media/usb/sdc2]
Formatting usb disk: [/dev/sdc3]
Mounting disk: [/dev/sdc3] under: [/media/usb/sdc3]
Formatting usb disk: [/dev/sdc4]
Mounting disk: [/dev/sdc4] under: [/media/usb/sdc4]
Creating directory under: [/media/usb/sdc1] - [001][sdc1]...
Creating directory under: [/media/usb/sdc2] - [002][sdc2]...
Creating directory under: [/media/usb/sdc3] - [003][sdc3]...
Creating directory under: [/media/usb/sdc4] - [004][sdc4]...
Creating directory under: [/media/usb/sdc1] - [005][sdc1]...
Creating directory under: [/media/usb/sdc2] - [006][sdc2]...
Creating directory under: [/media/usb/sdc3] - [007][sdc3]...
Creating directory under: [/media/usb/sdc4] - [008][sdc4]...
Creating directory under: [/media/usb/sdc1] - [009][sdc1]...
Creating directory under: [/media/usb/sdc2] - [010][sdc2]...
Creating directory under: [/media/usb/sdc3] - [011][sdc3]...
Creating directory under: [/media/usb/sdc4] - [012][sdc4]...
Creating directory under: [/media/usb/sdc1] - [013][sdc1]...
Creating directory under: [/media/usb/sdc2] - [014][sdc2]...
Creating directory under: [/media/usb/sdc3] - [015][sdc3]...
Creating directory under: [/media/usb/sdc4] - [016][sdc4]...
Creating directory under: [/media/usb/sdc1] - [017][sdc1]...
Creating directory under: [/media/usb/sdc2] - [018][sdc2]...
Creating directory under: [/media/usb/sdc3] - [019][sdc3]...
Creating directory under: [/media/usb/sdc4] - [020][sdc4]...

If you want to know all devices that are USB, check this link: http://stackoverflow.com/questions/6...s-a-usb-device

I hope it helps!
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to write a Script to run series of batch jobs on unix platform

Im new to unix shell scripting, I have to run batch jobs on unix. for example i have 5 jobs. first 2 can kickoff parallely. after completely finishing the 2 previous jobs the 3 job should kick off..once 3rd is over 4 th and 5th can kick off parallely. Each jobs run for 1 or 2 hours each. How to... (2 Replies)
Discussion started by: venki311
2 Replies

2. Red Hat

formating a USB which is throwing error write delayed

Hello, while i was saving a web file directing on to usb location there was some network problem which result in error WRITE DELAYED on windows xp. so pen drive is not getting completely formated(show as Windows was unable to complete format).From then onwords it is not allowing to copy... (0 Replies)
Discussion started by: amarnathbasis8
0 Replies

3. UNIX for Advanced & Expert Users

Multiple processes write to log file at the same time

If we have 3 process to write to same log file at the same time like below. will it cause the data outdated because the multiple process writing same time? It this a safe way to keep the log for multiple process? p1 >> test.log &; p2 >> test.log &; p3 >> test.log & Thanks, (1 Reply)
Discussion started by: casttree
1 Replies

4. Shell Programming and Scripting

Multiple process write to same log file at the same time

If we have 3 process to write to same log file at the same time like below. will it cause the data outdated because the multiple process writing same time? It this a safe way to keep the log for multiple process? p1 >> test.log &; p2 >> test.log &; p3 >> test.log & Thanks, (5 Replies)
Discussion started by: casttree
5 Replies

5. Debian

Write permission for USB device

Hello, I need to run an application in wine that requires write permission to a USB device. Wine users must not have root privileges. On FreeBSD this could be accomplished by adding the user to the wheel group but I am using Debian 6.0. From looking at the passwd file it is not obvious what... (6 Replies)
Discussion started by: snorkack59
6 Replies

6. Shell Programming and Scripting

Xentop Batch Time Display Help

I would like to be able to display the local time (or anytime for that matter) when I run Xentop in batch mode. Is that possible? (In other words, when I look back at the data, I want to be able to tell what time that the output was displayed). (2 Replies)
Discussion started by: Coyote2012
2 Replies

7. AIX

How to write a script to run without password on a batch of servers?

I need run a command such as ps -ef |grep xxx on a batch of servers, how to write a script to run it without password? don't need go in each server to check? Thanks (7 Replies)
Discussion started by: rainbow_bean
7 Replies

8. Shell Programming and Scripting

How to write BTEQ batch scripts in UNIX?

Hi All, I need to write Unix shell script. To star with : I need to do some file checking on unix file system, then based on file existance, I need to run diff SQL in Teradata Bteq. After that, depending on Results of SQL, I need to code other shell scripting like moving file, within same... (4 Replies)
Discussion started by: Shilpi Gupta
4 Replies

9. Shell Programming and Scripting

Is it possible to write write multiple cronjobs in shellscript??

Hi All, I need the answer of below question? 1) How to write multiple cronjobs in shellscript? Is there any way or we cant write in shellscript... Regards, Priyanka (2 Replies)
Discussion started by: pspriyanka
2 Replies
All times are GMT -4. The time now is 04:09 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy