LINUX.ORG.RU

История изменений

Исправление Desmond_Hume, (текущая версия) :

#!/bin/bash

#set -x
for a in /home/user/Documents/Test\ Files/*;
	do
		filetype_is1 () {
			echo $(file "$a" |  cut -d ":" -f 2 | cut -d " " -f 2)	
		}		

		if [ $(filetype_is1) = "directory" ]; then
			:
		else			
			if [ $(filetype_is1) = "Unicode" ]; then
				mv "$a" /home/user/Documents/Test\ Files/test/${a%}.converted
			else
				:
			fi
		fi
	done

for b in /home/user/Documents/Test\ Files/*;
	do
		filetype_is2 () {
			echo $(file "$b" |  cut -d ":" -f 2 | cut -d " " -f 2)	
		}	
		if [ $(filetype_is2) = "directory" ]; then
			:
		else
			iconv -f WINDOWS-1251 -t UTF-8 "$b" -o "${b%}.converted" && mv "${b%}.converted" /home/user/Documents/Test\ Files/test/
		
		fi
	done 

for c in /home/user/Documents/Test\ Files/*;
	do 
		filetype_is3 () {
			echo $(file "$c" |  cut -d ":" -f 2 | cut -d " " -f 2)	
		}
		if [ $(filetype_is3) = "directory" ]; then
			:
		elif [ $(filetype_is3) = "ISO-8859" ]; then
			rm "$c"
		else
			:
		fi	
	done
			
#set +x

Вот именно так - работает, как надо. Стоит только function … сделать - всё, сыплет ошибками:

$ ./conv_test.sh 
mv: target '10).txt.converted.converted': No such file or directory
mv: target '11).txt.converted.converted': No such file or directory
mv: target '3).txt.converted.converted': No such file or directory
mv: target '4).txt.converted.converted': No such file or directory
mv: target '5).txt.converted.converted': No such file or directory
mv: target '6).txt.converted.converted': No such file or directory
mv: target '7).txt.converted.converted': No such file or directory
mv: target '8).txt.converted.converted': No such file or directory
mv: target '9).txt.converted.converted': No such file or directory
mv: target '(Copy).txt.converted.converted': No such file or directory
iconv: illegal input sequence at position 388
iconv: illegal input sequence at position 388
iconv: illegal input sequence at position 388
iconv: illegal input sequence at position 388
iconv: illegal input sequence at position 388
iconv: illegal input sequence at position 388
iconv: illegal input sequence at position 388
iconv: illegal input sequence at position 388
iconv: illegal input sequence at position 388
iconv: illegal input sequence at position 388
$ 

Исходная версия Desmond_Hume, :

#!/bin/bash

#set -x
for a in /home/user/Documents/Test\ Files/*;
	do
		filetype_is1 () {
			echo $(file "$a" |  cut -d ":" -f 2 | cut -d " " -f 2)	
		}		

		if [ $(filetype_is1) = "directory" ]; then
			:
		else			
			if [ $(filetype_is1) = "Unicode" ]; then
				mv "$a" /home/user/Documents/Test\ Files/test/${a%}.converted
			else
				:
			fi
		fi
	done

for b in /home/user/Documents/Test\ Files/*;
	do
		filetype_is2 () {
			echo $(file "$b" |  cut -d ":" -f 2 | cut -d " " -f 2)	
		}	
		if [ $(filetype_is2) = "directory" ]; then
			:
		else
			iconv -f WINDOWS-1251 -t UTF-8 "$b" -o "${b%}.converted" && mv "${b%}.converted" /home/user/Documents/Test\ Files/test/
		
		fi
	done 

for c in /home/user/Documents/Test\ Files/*;
	do 
		filetype_is3 () {
			echo $(file "$c" |  cut -d ":" -f 2 | cut -d " " -f 2)	
		}
		if [ $(filetype_is3) = "directory" ]; then
			:
		elif [ $(filetype_is3) = "ISO-8859" ]; then
			rm "$c"
		else
			:
		fi	
	done
			
#set +x