Sfoglia il codice sorgente

Creating function to retry commands

Vinicius Teshima 10 mesi fa
parent
commit
2e31037dd7
1 ha cambiato i file con 16 aggiunte e 4 eliminazioni
  1. 16 4
      luks.sh

+ 16 - 4
luks.sh

@@ -5,6 +5,18 @@ ERR () {
 	test -n "$2" && exit "$2"
 }
 
+TRY_AND_RETRY () {
+	while ! $1
+	do
+		ERR "$2"
+		echo -n 'Want to try again? (Y/n) '
+		read choose
+		case "$choose" in
+			N|n) exit 1 ;;
+		esac
+	done
+}
+
 file_path="$1"
 test -n "$file_path" || { echo 'No file given'; exit 1; }
 file="$(basename "$file_path")"
@@ -72,19 +84,19 @@ else
 	echo "Succesfully Allocated file: ${file_path}"
 
 	echo "Encrypting file: ${file_path}"
-	cryptsetup luksFormat -c aes-xts-plain64 -s 512 -y "$file_path" || ERR "Failed to format file: ${file_path}" 1
+	TRY_AND_RETRY "cryptsetup luksFormat -c aes-xts-plain64 -s 512 -y ${file_path}" "Failed to format file: ${file_path}"
 	echo "Succesfully Encrypted file: ${file_path}"
 
 	echo "Opening Encrypted file: ${file_path} as ${_name}"
-	$sudo_prog cryptsetup luksOpen "$file_path" "$_name" || ERR "Failed to open luks file: ${file_path}." 1
+	TRY_AND_RETRY "${sudo_prog} cryptsetup luksOpen ${file_path} ${_name}" "Failed to open luks file: ${file_path}."
 	echo "Succesfully Opened Encrypted file: ${file_path} as ${_name}"
 
 	echo "Formating ${_name} as ext4"
-	$sudo_prog mkfs.ext4 -m0 "$block_dev" || ERR "Failed to format block dev: ${block_dev}" 1
+	TRY_AND_RETRY "${sudo_prog} mkfs.ext4 -m0 ${block_dev}" "Failed to format block dev: ${block_dev}"
 	echo "Succesfully Formated ${_name} as ext4"
 
 	echo "Mounting ${block_dev} into ./${name}"
-	$sudo_prog mount "$block_dev" ./"$name" || ERR "Failed to mound ${block_dev} on ./${name}." 1
+	TRY_AND_RETRY "${sudo_prog} mount ${block_dev} ./${name}" "Failed to mound ${block_dev} on ./${name}."
 	echo "Succesfully Mounted ${block_dev} into ./${name}"
 
 	dir_perm="$(stat -c '%u:%g' "$file_path")"