Tuesday, November 25, 2014

Advanced bash scripting : essence zenity : Interactive file converter


#!/bin/sh -u 
zenity --info --ok-label="START" --width="500" --height="300" --title="Horopter Inc." --text "`basename $0` is loaded. Click START to continue."
current=`pwd`
zenity --question --width="500" --height="300" --title="Welcome to Horopter fileconverter." --text "You need to put all the required files together in a directory, did you do that?"
if [ $? -eq 0 ]
then 
	zenity --info --width="500" --height="300" --title="Horopter Inc." --text "Example: If the files are in a directory called 'apple',\n /folder1/folder2/apple is location into the directory apple."
	path=$(zenity --entry --width="500" --height="300" --title="Directory selection" --text "Enter the location into the directory as absolute path, . for current directory: ")
	if [ -n "$path" ]
	then 		
		cd $path
		zenity --info --width="500" --height="300" --title="Horopter Inc." --text "Example: If the file to be converted is of type document, it has extension .doc, in that case enter *.doc\nExample: If you have multiple file types, seperate them with spaces. If you have *.doc and *.html enter\n*.doc *.html"
		from=$(zenity --entry --width="500" --height="300" --title="Filetype insertion" --text "Enter the file types to be converted as illustrated above : ")
		declare -a var
		mkdir converted_files
		var=($from)
		count=0
		for i in "${var[@]}"
		do
			let count=count+1
		done
		zenity --info --title="Horopter Inc." --width="500" --height="300" --text "Total $count files were found."
		zenity --info --title="Horopter Inc." --width="500" --height="300" --text "Example: If the file is to be converted into type document, it should have extension .doc, in that case enter .doc"
		to=$(zenity --entry --title="Filetype selection" --width="500" --height="300" --text "Enter the file type into which these files have to be converted.")
		ten=10/$count;
		twenty=20/$count;
		forty=40/$count;
		ff=55/$count;
		ss=67/$count;
		sn=69/$count;
		(
		for i in "${var[@]}"
		do
			echo $ten*i; sleep 1
			echo "# $i is being converted"; sleep 1
			newt[$count]="`echo $i | cut -d '.' -f1`"
			v="`echo $i | cut -d '.' -f2`"
			echo $twenty*i ; sleep 1
			echo "# converting the files"; sleep 1
			if [ "$v" = "$to" ]
			then
				newt[$count]="${newt[$count]}[converted]"
			fi
			echo $forty*i ; sleep 3
			echo "# Extension removed : ${newt[$count]}"; sleep 1  
			newt[$count]="${newt[$count]}$to"
			echo $ff*i; sleep 2
			echo "# Created a $to file of name : ${newt[$count]}"; sleep 1
			echo $ss*i; sleep 1
			touch ${newt[$count]}
			echo -e "*************************** ${newt[$count]} **********************\n" >> ${newt[$count]}
			echo -e "----------Converted into a $to file from a shell script\n ------------designed by SANTOSH KUMAR DESAI. :-) \n\n\n" >> ${newt[$count]}
			echo -e "<< This is preformatted text >>" >> ${newt[$count]} #If this were not there, the system goes into a corrupt state.
			cat $i >temp.txt ; cat temp.txt >> ${newt[$count]}
			echo -e "\n\n\n-----End of the document. Conversion courtesy : SANTOSH KUMAR DESAI-----" >> ${newt[$count]}
			echo $sn*i; sleep 2
			echo "# Copied content into the $to file : ${newt[$count]}";sleep 1
			mv -n ${newt[$count]} ./converted_files
			echo "# Moved ${newt[$count]} to the folder converted_files."; sleep 1
		done
		echo "# Finished copying."
		echo "100"; sleep 1
		) |
			zenity --progress --width="500" --height="300" \
			--title="Converting files..." \
			--text="Starting progress now..." \
			--percentage=0
		cd ./converted_files
		numfiles=`ls -l | wc -l`
		let numfiles=numfiles-1
		zenity --info --width="500" --height="300" --title="Horopter Inc." --text "Total $numfiles files were copied."
		if [ ! $count -eq $numfiles ]
		then 
			zenity --error --width="500" --height="300" --title="Horopter Inc." --text "You must have files with same names or same names but different extensions. Please check them out and try again."
		fi
		zenity --info --title="Horopter Inc." --width="500" --height="300" --text "Converting all the files into $to files is complete.\n The files are in converted_files directory and it will be available in the same directory as other source files. \n Thanks for using this shell script \n\n\n---- Horopter."
	fi
fi	
cd $current
zenity --info --title="Horopter Inc." --ok-label="FINISH" --width="500" --height="300" --text "Click FINISH to finish."
exit 0


No comments:

Post a Comment