expression - bash variable evaluating with quote for special character -
below simple bash script test few password in rar file. when password contains special character (!), see option (-p) parsed quote (').
#!/bin/bash -x pwd=`echo sei{0..9}{0..9}b{0..9}axq!zx` #pwd=sei03b4axq!zx file="test.rar" eachpwd in $pwd eval unrar x "$file" -p"$eachpwd" id 2>/dev/null 1>/dev/null c=$? if [ $c = 0 ] echo "success" exit fi echo $eachpwd $c done
output
+ eachpwd in '$pwd' + eval unrar x test.rar '-psei49b4axq!zx' id + c=10 + '[' 10 = 0 ']' + echo 'sei49b4axq!zx' 10
question
how code such that, during eval not unrar x test.rar '-psei49b4axq!zx' id
instead unrar x test.rar -psei49b4axq!zx id
evaluated expression
update 1
tried below code, still quote(!) on -p option refuses budge.
passwords=( sei{0..9}{0..9}b{0..9}axq!zx ) file=test.rar password in "${passwords[@]}"; if unrar x "$file" -p"$password" id 2>/dev/null 1>/dev/null; echo "success" exit else echo $password failed. fi done
result
+ password in '"${passwords[@]}"' + unrar x test.rar '-psei21b0axq!zx' id + echo 'sei21b0axq!zx' failed. sei21b0axq!zx failed. + password in '"${passwords[@]}"' + unrar x test.rar '-psei21b1axq!zx' id
please note using bash shell execution.
there couple of possible explanations happening, none of worth exploring because don't need use eval
. also, it's better use array hold passwords, or put brace expression directly in for
loop, rather creating single string.
# use single quotes on small chance history expansion # enabled, protect !. can drop them. passwords=( sei{0..9}{0..9}b{0..9}'axq!zx' ) password in "${passwords[@]}"; if unrar x "$file" -p "$eachpwd" id 2>/dev/null 1>/dev/null; echo "success" exit fi done
Comments
Post a Comment