Ubuntu 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"