var인 비밀번호를 해시하기 위해 bcrypt를 호출하는 Python을 사용할 때 올바른 bash 구문

var인 비밀번호를 해시하기 위해 bcrypt를 호출하는 Python을 사용할 때 올바른 bash 구문

우분투 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"

관련 정보