Reputation: 148
I cant get the following script to work whenever the path($1) contains one or more spaces. What should i change to make it work?
#!/bin/bash
ws=/tmp/$RANDOM && mkdir $ws && cd $ws
TRG=$1 && SRC=$1
if [ "$1" != "" ];then
if [ "$3" != "" ];then
if [ "$2" == "enc" ];then
if [[ -d $1 ]];then
mv $1 ./
zip -rq "$(basename $1).zip" "$(basename $1)"
SRC="$ws/$(basename $1).zip"
fi
openssl aes-256-cbc -a -salt -in $SRC -out $TRG -pass file:<( echo -n "$3" )
elif [ "$2" == "dec" ];then
mv $1 ./
SRC="$ws/$(basename $1)"
openssl aes-256-cbc -d -a -salt -in $SRC -out $TRG -pass file:<( echo -n "$3" )
if grep -q "PK" <<<"$(awk 'FNR == 1 { print; exit }' $TRG)"; then
rm "$ws/$(basename $TRG)"
mv "$TRG" "$ws/$(basename $TRG)"
TRG="$ws/$TRG"
mkdir "$1"
unzip -o -qq "$(basename $TRG)" -d "$(cd $1 && cd .. && pwd)"
fi
else
echo "Incorrect operation"
fi
elif [ "$2" == "" ];then
echo "Password and mode is required"
else
echo "Password is required"
fi
else
echo "No path specificated"
fi
rm -rf $ws
I know that i need to add quotes but if i do that i think i will screw up some existent commands in the code...
Upvotes: 1
Views: 45
Reputation: 8613
You also need to quote variables inside $(...)
too. If not this can lead to unexpected behaviour. So replace all "$(basename $1)"
with "$(basename "$1")"
.
And also quote assignments TRG="$1"
.
Then it should look like this.
#!/bin/bash
ws=/tmp/$RANDOM && mkdir "$ws" && cd "$ws" || exit
TRG="$1" && SRC="$1"
if [ "$1" != "" ];then
if [ "$3" != "" ];then
if [ "$2" == "enc" ];then
if [[ -d $1 ]];then
mv "$1" ./
zip -rq "$(basename "$1").zip" "$(basename "$1")"
SRC="$ws/$(basename "$1").zip"
fi
openssl aes-256-cbc -a -salt -in "$SRC" -out "$TRG" -pass file:<( echo -n "$3" )
elif [ "$2" == "dec" ];then
mv "$1" ./
SRC="$ws/$(basename "$1")"
openssl aes-256-cbc -d -a -salt -in "$SRC" -out "$TRG" -pass file:<( echo -n "$3" )
if grep -q "PK" <<<"$(awk 'FNR == 1 { print; exit }' "$TRG")"; then
rm "$ws/$(basename "$TRG")"
mv "$TRG" "$ws/$(basename "$TRG")"
TRG="$ws/$TRG"
mkdir "$1"
unzip -o -qq "$(basename "$TRG")" -d "$(cd "$1" && cd .. && pwd)"
fi
else
echo "Incorrect operation"
fi
elif [ "$2" == "" ];then
echo "Password and mode is required"
else
echo "Password is required"
fi
else
echo "No path specificated"
fi
rm -rf "$ws"
Upvotes: 1