/ 3.sh
3.sh
  1  #!/usr/bin/bash
  2  function is_nonempty(){
  3  	[ `ls "$1" | wc -l` -ne 0 ]
  4  }
  5  
  6  dialog="dialog --stdout"
  7  
  8  function show_error(){
  9  	$dialog --title "error" --msgbox "$1" 5 $[ ${#1} + 4 ]
 10  }
 11  
 12  function errorize(){
 13  	status=`"$@" 2>&1`
 14  	if [ $? -ne 0 ]; then
 15  		show_error "$status"
 16  		return 1
 17  	fi
 18  	return 0
 19  }
 20  
 21  function wait_key(){
 22  	read -p "Press any key to continue " -n 1
 23  }
 24  
 25  loc="$PWD/"
 26  
 27  while
 28  	loc=`$dialog --title "File manager" \
 29  		--ok-label                   "create" \
 30  		--extra-button --extra-label "copy/move" \
 31  		--nocancel \
 32  		--help-button --help-label   "remove" \
 33  		--fselect "$loc" -1 -1`
 34  
 35  	case $? in
 36  		0) # OK – create
 37  			if [ -e "$loc" ]; then
 38  				show_error "Enter object name first"
 39  				continue
 40  			fi
 41  
 42  			errorize mkdir -p "`dirname "$loc"`" || continue
 43  
 44  			$dialog --title "$loc" \
 45  				--yes-label "directory" \
 46  				--extra-button --extra-label "file" \
 47  				--no-label "cancel" \
 48  				--yesno "" 0 0
 49  
 50  			case $? in
 51  				0) errorize mkdir "$loc" ;;
 52  				3) errorize touch "$loc" ;;
 53  			esac
 54  			;;
 55  
 56  		3) # extra – copy/move
 57  			if [ ! -e "$loc" ]; then
 58  				show_error "Selected object does not exist"
 59  				continue
 60  			fi
 61  
 62  			if [[ -d "$loc" && ! -L "$loc" ]]; then dst="`dirname "$loc"`/"
 63  			else dst="$loc"
 64  			fi
 65  
 66  			dst=$($dialog --backtitle "Destination selector" --title "$loc" \
 67  				--ok-label "copy" \
 68  				--extra-button --extra-label "move" \
 69  				--cancel-label "cancel" \
 70  				--fselect "$dst" 0 0)
 71  			
 72  			case $? in
 73  				0)
 74  					clear
 75  					echo "copying in progress..."
 76  					echo "from: $loc"
 77  					echo "to:   $dst"
 78  					cp --interactive --recursive --reflink=auto "$loc" "$dst" || wait_key
 79  					[ -e "$dst" ] && loc="$dst"
 80  					;;
 81  				3)
 82  					clear
 83  					echo "moving in progress..."
 84  					echo "from: $loc"
 85  					echo "to:   $dst"
 86  					mv --interactive "$loc" "$dst" || wait_key
 87  					[ -e "$dst" ] && loc="$dst"
 88  					;;
 89  			esac
 90  			;;
 91  
 92  		2) # help – remove
 93  			if [ ! -e "$loc" ]; then
 94  				show_error "Selected object does not exist"
 95  				continue
 96  			fi
 97  
 98  			if [ -d "$loc" ]; then
 99  				if is_nonempty "$loc"; then
100  					$dialog --title "$loc" \
101  						--yesno "Warning: non-empty directory. Continue?" 0 0
102  					if [ $? -eq 0 ]; then
103  						clear
104  						echo "removing in progress..."
105  						echo "from: $loc"
106  						rm -fr "$loc" || wait_key
107  					fi
108  				else
109  					rmdir "$loc"
110  				fi
111  			else
112  				if [[ ! -L "$loc" && -f "$loc" && `stat --format=%s "$loc"` -ne 0 ]]; then
113  					$dialog --title "$loc" \
114  						--yesno "Continue to remove this file?" 0 0 || continue
115  				fi
116  				clear
117  				echo "removing in progress..."
118  				echo "from: $loc"
119  				rm "$loc" || wait_key
120  			fi
121  			;;
122  
123  		255) # ESC – exit
124  			clear
125  			break ;;
126  	esac
127  
128  	true
129  do true; done