Richtige Bash-Syntax bei Verwendung von Python, das bcrypt aufruft, um ein Passwort zu hashen, das eine Variable ist

Richtige Bash-Syntax bei Verwendung von Python, das bcrypt aufruft, um ein Passwort zu hashen, das eine Variable ist

Ubuntu 16.04

Shellcheck gibt an: „Ausdrücke werden nicht in einfachen Anführungszeichen erweitert, verwenden Sie dafür doppelte Anführungszeichen“, aber mein Passwort ist eine Variable. Das Skript funktioniert einwandfrei, wenn ich einfache Anführungszeichen zum Importieren von bcrypt verwende.

Hier ist mein Skript:

#!/bin/bash

wDir="/home/work/amp/"
ampDir="${wDir}.pass_and_hash/"
ampPass="0192734654837948787098"
ampAdminPass="0192734654837948787098"
ampPassHashtxt="${ampDir}.ampPassHash.txt"
ampAdminPassHashtxt="${ampDir}.ampAdminPassHash.txt"

#-- create the .pass_and_hash folder
mkdir -p "$ampDir"

#-- echo both $ampPass and $ampAdminPass to files at .pass_and_hash
echo "${ampPass}" > "${ampDir}".ampPass.txt
echo "${ampAdminPass}" > "${ampDir}".ampAdminPass.txt

#-- generate hashes for $ampPass and $ampAdminPass and record output to files at .pass_and_hash
python2 -c 'import bcrypt; print(bcrypt.hashpw("$ampPass", bcrypt.gensalt(10)))' > "$ampPassHashtxt"
python2 -c 'import bcrypt; print(bcrypt.hashpw("$ampAdminPass", bcrypt.gensalt(10)))' > "$ampAdminPassHashtxt"

#-- Echo the values of the hash to /home/work/amp/Logs/console.log
echo "";
echo "*** After Created - Generate + Record Hashes for SuperAdmin + Administrator ****"
echo "SuperUser - generated password = $ampPass and hash = $(cat $ampPassHashtxt)"
echo "Administrator User - generated password = $ampAdminPass and hash = $(cat $ampAdminPassHashtxt)"
exit 0;

Wenn ich das Skript ausführe, erhalte ich null Fehler:

root@pl /home/work/amp # ./run.sh

*** After Created - Generate + Record Hashes for SuperAdmin + Administrator ****
SuperUser - generated password = 0192734654837948787098 and hash = $2b$10$7UuG0NfTYZ8Ritgj3nhQt.7Fqa7RTYlN97WyoTt1EGrrXmA85pVc6
Administrator User - generated password = 0192734654837948787098 and hash = $2b$10$H3Gr4hrDL/6CAaCgSf2f7eEvqdbM9DUese1cQpyn/muBdQdmiFNgS

Wenn ich Shellcheck frage, was es denkt, sagt es:

root@pl /home/work/amp # shellcheck run.sh

In run.sh line 18:
python2 -c 'import bcrypt; print(bcrypt.hashpw("$ampPass", bcrypt.gensalt(10)))' > "$ampPassHashtxt"
           ^-- SC2016: Expressions don't expand in single quotes, use double quotes for that.


    In run.sh line 19:
    python2 -c 'import bcrypt; print(bcrypt.hashpw("$ampAdminPass", bcrypt.gensalt(10)))' > "$ampAdminPassHashtxt"
               ^-- SC2016: Expressions don't expand in single quotes, use double quotes for that.

Wie korrigiere ich die Anführungszeichen, um die Shellcheck-Prüfung zu erfüllen?

Antwort1

Ich ahme Ihr Skript nach, die Variable ampPass muss nicht festgelegt werden:

$ python2 -c 'print("$ampPass");'
$ampPass

Innerhalb der einfachen Anführungszeichen wird „$ampPass“ nicht ersetzt, sondern nur zwischen doppelte gesetzt:

python2 -c 'import bcrypt; print(bcrypt.hashpw("'"$ampPass"'", bcrypt.gensalt(10)))' > "$ampPassHashtxt"

verwandte Informationen