【问题标题】:Printing special characters in a Bash scipt在 Bash 脚本中打印特殊字符
【发布时间】:2016-04-08 04:05:23
【问题描述】:

我有一个 bash 脚本,它的功能之一是检查字符串(密码)的长度。密码中必须包含特殊字符。我正在使用 ${#VAR} 检查字符串的长度是否合适,但正在解释特殊字符,因此计数已关闭。以下是有问题的代码:

userpass="$1"

if [[ "${#userpass}" -lt 8 ]]; then
     echo 'Short Password'
fi

像 Q7cS$ZpR8eSi 和 pNbTb$IhkyUouDcZ 这样的字符串会使它出错(任何带有美元符号或 & 符号的字符串)。我也尝试过使用wc -c,但我遇到了同样的问题

【问题讨论】:

  • 你必须单引号引用字符串。不清楚你是如何喂 $1 的,但假设 cmd 线,你必须做 ./myPswdChkr.sh 'Q7cS$ZpR8eSi' 。用于模型外壳 Q 的 Pluse-uno。简洁并包括示例输入!祝你好运。
  • 感谢您的建议,我的问题是密码是可变的。该脚本用于检查密码的复杂性以及它是否符合组织的密码策略。用户将输入 ./myPswdChkr.sh 他们的密码,脚本会告诉他们密码是否合适。
  • 我无法重现该问题。 userpass='pNbTb$IhkyUouDcZ'; echo ${#userpass} 输出:16
  • 嗯...也许这是我的 Bash 版本或其他问题。 pw A2ZH$HoyL Short Password, please make it longer than 8 characters Please create a password with a lowercase letter Plese insert one of @ # $ % & * + - = into your password Please try again, your password did not meet the requirements 它在美元符号之前被切断。
  • @Cyrus 我的$userpass 等于$1。该脚本要求用户输入密码,因此不会用引号引起来。也许我会尝试在sed 中一起破解一些东西,以将其封装在单引号中。

标签: bash shell


【解决方案1】:

这不起作用:

./myPswdChkr.sh theirpa$sword

因为变量插值发生在参数传递给脚本之前。 myPswdChkr.sh 只会收到 theirpa

正如@shellter 所说,如果用户在使用脚本时在密码周围加上单引号,它会起作用:

./myPswdChkr.sh 'theirpa$sword'

。 . .但实际上,这是一个糟糕的主意,因为这样他们的明文密码就会保存在命令历史记录中。

相反,运行不带参数的脚本,只需 ./myPswdChkr.sh,然后在脚本中提示用户输入密码:

echo -n 'PlsEntrYrPswd: '
read userpass
if [[ "${#userpass}" -lt 8 ]]; then
    echo 'Short Password'
fi

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 2015-11-30
    • 1970-01-01
    相关资源
    最近更新 更多