[pass] [PATCH] Use bash's inbuilt arithmetic comparison instead of [[.

Chris Down chris at chrisdown.name
Mon Sep 16 11:13:29 CEST 2013


This is the recommended way to do arithmetic comparisons, I also find this a
lot more readable.
---
 src/password-store.sh | 42 +++++++++++++++++++++++-------------------
 1 file changed, 23 insertions(+), 19 deletions(-)

diff --git a/src/password-store.sh b/src/password-store.sh
index 2500253..202ddd4 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -146,7 +146,7 @@ case "$command" in
 			--) shift; break ;;
 		esac done

-		if [[ $# -ne 1 ]]; then
+		if (( $# != 1 )); then
 			echo "Usage: $program $command [--reencrypt,-e] gpg-id"
 			exit 1
 		fi
@@ -157,7 +157,7 @@ case "$command" in
 		echo "Password store initialized for $gpg_id."
 		git_add_file "$ID" "Set GPG id to $gpg_id."

-		if [[ $reencrypt -eq 1 ]]; then
+		if (( reencrypt )); then
 			find "$PREFIX/" -iname '*.gpg' | while read passfile; do
 				gpg2 -d $GPG_OPTS "$passfile" | gpg2 -e -r "$gpg_id" -o "$passfile.new" $GPG_OPTS &&
 				mv -v "$passfile.new" "$passfile"
@@ -201,7 +201,7 @@ case "$command" in
 			--) shift; break ;;
 		esac done

-		if [[ $err -ne 0 ]]; then
+		if (( err )); then
 			echo "Usage: $program $command [--clip,-c] [pass-name]"
 			exit 1
 		fi
@@ -209,12 +209,12 @@ case "$command" in
 		path="$1"
 		passfile="$PREFIX/$path.gpg"
 		if [[ -f $passfile ]]; then
-			if [[ $clip -eq 0 ]]; then
-				exec gpg2 -d $GPG_OPTS "$passfile"
-			else
+			if (( clip )); then
 				pass="$(gpg2 -d $GPG_OPTS "$passfile" | head -n 1)"
 				[[ -n $pass ]] || exit 1
 				clip "$pass" "$path"
+			else
+				exec gpg2 -d $GPG_OPTS "$passfile"
 			fi
 		elif [[ -d $PREFIX/$path ]]; then
 			if [[ -z $path ]]; then
@@ -243,22 +243,24 @@ case "$command" in
 			--) shift; break ;;
 		esac done

-		if [[ $err -ne 0 || ( $multiline -eq 1 && $noecho -eq 0 ) || $# -ne 1 ]]; then
+		if (( err )) || { (( multiline )) && ! (( noecho )); } || (( $# != 1 )); then
 			echo "Usage: $program $command [--echo,-e | --multiline,-m] [--force,-f] pass-name"
 			exit 1
 		fi
 		path="$1"
 		passfile="$PREFIX/$path.gpg"

-		[[ $force -eq 0 && -e $passfile ]] && yesno "An entry already exists for $path. Overwrite it?"
+		if ! (( force )) && [[ -e $passfile ]]; then
+			yesno "An entry already exists for $path. Overwrite it?"
+		fi

 		mkdir -p -v "$PREFIX/$(dirname "$path")"

-		if [[ $multiline -eq 1 ]]; then
+		if (( multiline )); then
 			echo "Enter contents of $path and press Ctrl+D when finished:"
 			echo
 			gpg2 -e -r "$ID" -o "$passfile" $GPG_OPTS
-		elif [[ $noecho -eq 1 ]]; then
+		elif (( noecho )); then
 			while true; do
 				read -r -p "Enter password for $path: " -s password
 				echo
@@ -278,7 +280,7 @@ case "$command" in
 		git_add_file "$passfile" "Added given password for $path to store."
 		;;
 	edit)
-		if [[ $# -ne 1 ]]; then
+		if (( $# != 1 )); then
 			echo "Usage: $program $command pass-name"
 			exit 1
 		fi
@@ -320,7 +322,7 @@ case "$command" in
 			--) shift; break ;;
 		esac done

-		if [[ $err -ne 0 || $# -ne 2 ]]; then
+		if (( err )) || (( $# != 2 )); then
 			echo "Usage: $program $command [--no-symbols,-n] [--clip,-c] [--force,-f] pass-name pass-length"
 			exit 1
 		fi
@@ -333,18 +335,20 @@ case "$command" in
 		mkdir -p -v "$PREFIX/$(dirname "$path")"
 		passfile="$PREFIX/$path.gpg"

-		[[ $force -eq 0 && -e $passfile ]] && yesno "An entry already exists for $path. Overwrite it?"
+		if ! (( force )) && [[ -e $passfile ]]; then
+			yesno "An entry already exists for $path. Overwrite it?"
+		fi

 		pass="$(pwgen -s $symbols $length 1)"
 		[[ -n $pass ]] || exit 1
 		gpg2 -e -r "$ID" -o "$passfile" $GPG_OPTS <<<"$pass"
 		git_add_file "$passfile" "Added generated password for $path to store."
-
-		if [[ $clip -eq 0 ]]; then
+
+		if (( clip )); then
+			clip "$pass" "$path"
+		else
 			echo "The generated password to $path is:"
 			echo "$pass"
-		else
-			clip "$pass" "$path"
 		fi
 		;;
 	delete|rm|remove)
@@ -359,7 +363,7 @@ case "$command" in
 			-f|--force) force=1; shift ;;
 			--) shift; break ;;
 		esac done
-		if [[ $# -ne 1 ]]; then
+		if (( $# != 1 )); then
 			echo "Usage: $program $command [--recursive,-r] [--force,-f] pass-name"
 			exit 1
 		fi
@@ -374,7 +378,7 @@ case "$command" in
 			fi
 		fi

-		[[ $force -eq 1 ]] || yesno "Are you sure you would like to delete $path?"
+		(( force )) || yesno "Are you sure you would like to delete $path?"

 		rm $recursive -f -v "$passfile"
 		if [[ -d $GIT_DIR && ! -e $passfile ]]; then
--
1.8.4



More information about the Password-Store mailing list