Tuesday, November 25, 2014

Advanced bash scripting : essence zenity : Interactive filemerger

#!/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
  flname=$(zenity --entry --width="500" --height="300" --title="Directory selection" --text "Enter the name of the output file with extension.")
  echo -e "*******************************   $flname  ******************************\n" > $flname
  echo -e "------------ Compiled by a software from SANTOSH KUMAR DESAI ------------\n" >> $flname
  echo -e "<< This is preformatted text >>" >> $flname
  declare -a var
  var=(*.html *.css *.txt)
  count=0
  for i in "${var[@]}"
  do
   input=`echo $i | cut -d '.' -f1`
   if [ "$input" != "*" ]
   then 
   let count=count+1
   fi
  done
  zenity --info --title="Horopter Inc." --width="500" --height="300" --text "Total $count files were found."
  cnt=1
  ( for i in "${var[@]}"
  do
   input=`echo $i | cut -d '.' -f1`
   if [ "$input" != "*" ]
   then
   echo -e "# copying $i ($cnt of $count)"; sleep 1
   echo -e "--------DOCUMENT $i STARTS HERE--------\n" >> $flname
   cat $i >> $flname
   echo -e "\n--------DOCUMENT $i ENDS HERE-----------\n\n\n" >> $flname
   echo -e "# copying $i complete. ($cnt of $count)" ; sleep 1
   sed '/^$/d' $flname
   echo `expr $cnt \* 100 / $count - 10`; sleep 1
   let cnt=cnt+1
   fi
  done
  echo -e "# Finishing the job"; sleep 1
  echo -e "\n**************************** END OF DOCUMENT ****************************" >> $flname
  echo -e "\nAll files were merged through a shell script from SANTOSH KUMAR DESAI " >> $flname
  echo -e "# Total $count files were copied."; sleep 1
  echo -e "# Compiling all the files into one file is complete.\n"; sleep 1
  echo "100" ; sleep 1
  )| zenity --progress --width="500" --height="300" \
   --title="Merging the files..." \
   --text="Starting progress now..." \
   --percentage=0
  zenity --info --title="Horopter Inc." --width="500" --height="300" --text  "The file is named $flname and it will be available in the same directory as other merged files. \n Thanks for using this shell script ---- Horopter."
 fi
fi 
cd $current
zenity --info --title="Horopter Inc." --ok-label="Finish" --width="500" --height="300" --text "Click finish to exit."
exit 0

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


fileconverter.sh


This is a script to convert files in batch to given format, kind of like pandoc.


#!/bin/sh -u 
echo -e "$0 is loaded."
echo -e "You need to put all the required files together in a directory, press Y if yes, N if no"
read choice
if [ "$choice" = "y" -o "$choice" == "Y" ]
then 
 echo -e "Example: If the files are in a directory called 'apple', /folder1/folder2/apple is location into the directory apple."
 echo -e "Enter the location into the directory as absolute path, . for current directory: "
 read path
 if [ -n "$path" ]
 then 
  current = pwd  
  cd $path
  echo "Example: If the file to be converted is of type document, it has extension .doc, in that case enter *.doc"
  echo "Example: If you have multiple file types, seperate them with spaces. If you have *.doc and *.html enter"
  echo "*.doc *.html"
  echo "Enter the file types to be converted as illustrated above : "
  read from
  declare -a var
  mkdir converted_files
  var=($from)
  count=0
  for i in "${var[@]}"
  do
   echo $i
   let count=count+1
  done
  echo "Total $count files were found."
  echo "Example: If the file is to be converted into type document, it should have extension .doc, in that case enter .doc"
  echo "Enter the file type into which these files have to be converted."
  read to
  for i in "${var[@]}"
  do
   echo "$i is being converted"
   newt[$count]="`echo $i | cut -d '.' -f1`"
   v="`echo $i | cut -d '.' -f2`"
   if [ "$v" = "$to" ]
   then
    newt[$count]="${newt[$count]}[converted]"
   fi
   echo "Extension removed : ${newt[$count]}" 
   newt[$count]="${newt[$count]}$to"
   echo "Created a $to file of name : ${newt[$count]}"
   echo -e "Converted into a $to file from a shell script designed by SANTOSH KUMAR DESAI. \n\n\n" > ${newt[$count]}
   cat $i >> ${newt[$count]}
   echo -e "\n\n\nEnd of the document. Conversion courtesy : SANTOSH KUMAR DESAI" >> ${newt[$count]}
   echo "Copied content into the $to file : ${newt[$count]}"
   mv ${newt[$count]} ./converted_files
   echo "Moved ${newt[$count]} to the folder converted_files."
  done
  cd ./converted_files
  numfiles = ls -l | wc -l
  echo -e "Total $numfiles files were copied."
  if [ ! $count -eq $numfiles ]
  then 
   echo "You must have files with same names or same names but different extensions. Please check them out and try again."
  fi
  echo -e "Converting all the files into $to files is complete.\n"
  echo -e "The files are in converted_files directory and it will be available in the same directory as other source files."
  echo -e "Thanks for using this shell script ---- Horopter."
 fi
fi 
echo "Exiting the script."
cd $current
exit 0

filemerger.sh


This is a software I developed, merges several files into one.

#!/bin/sh -u 
echo -e "$0 is loaded."
echo -e "You need to put all the required files together in a directory, press Y if yes, N if no"
read choice
if [ "$choice" = "y" -o "$choice" == "Y" ]
then 
 echo -e "Example: If the files are in a directory called 'apple', /folder1/folder2/apple is location into the directory apple."
 echo -e "Enter the location into the directory as absolute path, . for current directory: "
 read path
 if [ -n "$path" ]
 then 
  cd $path
  echo -e "What do you want the file to be named?"
  read flname
  echo -e "*******************************   $flname  ******************************" > $flname
  echo -e "------------ Compiled by a software from SANTOSH KUMAR DESAI ------------" >> $flname
  echo "The files in this folder to merge are:"
  declare -a var
  var=(*.php *.html *.css *.txt)
  $count=0
  for i in "${var[@]}"
  do
   echo $i
   let count=count+1
  done
  echo "Total : $count files" 
  echo -e "Starting to merge files into one now...."
  for i in "${var[@]}"
  do
   echo -e "copying $i"
   echo -e "--------DOCUMENT $i STARTS HERE--------\n" >> $flname
   cat $i >> $flname
   echo -e "\n--------DOCUMENT $i ENDS HERE-----------\n\n\n" >> $flname
   echo -e "copying $i complete."
  done
  echo -e "**************************** END OF DOCUMENT ****************************" >> $flname
  echo -e "All files were merged through a shell script from SANTOSH KUMAR DESAI " >> $flname
  echo -e "Total $count files were copied."
  echo -e "Compiling all the files into one file is complete.\n"
  echo -e "The file is named $flname and it will be available in the same directory as other merged files."
  echo -e "Thanks for using this shell script ---- Horopter."
 fi
fi 
echo "Exiting the script."
exit 0