烏班圖16.04
shellcheck 聲明“表達式不會用單引號擴展,請使用雙引號”,但我的密碼是一個 var。當我使用單引號匯入 bcrypt 時,該腳本運作良好。
這是我的腳本:
#!/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;
當我運行腳本時,我收到零錯誤:
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
當我詢問 shellcheck 它的想法時,它說:
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.
如何修復雙引號以滿足 shellcheck 的要求?
答案1
我正在模仿您的腳本,無需設定 ampPass 變數:
$ python2 -c 'print("$ampPass");'
$ampPass
單引號內的 $ampPass 不會被替換,僅將其放在雙引號之間:
python2 -c 'import bcrypt; print(bcrypt.hashpw("'"$ampPass"'", bcrypt.gensalt(10)))' > "$ampPassHashtxt"