[英]shell scripting and regular expression
#!bin/bash
echo enter your password :
read password
passlength=$(echo ${#password})
if [ $passlength -le 8 ];
then
echo you entered correct password
else
echo entered password is incorrect
fi
if [[$password == [a-z]*[0-9][a-z]*]];
then
echo match found
else
echo match not found
fi
I am not getting what's wrong with this code. 我没有得到这段代码有什么问题。 If I enter any string as a password, let's say
hello123
, it gives me an error: 如果我输入任何字符串作为密码,假设您输入
hello123
,它给我一个错误:
hello123 : command not found
hello123:找不到命令
What is wrong with my script? 我的脚本有什么问题?
You can do the following to make it work cross-platforms with any the bourne shell (/bin/sh) based shell, no bash specific primitives - 您可以执行以下操作,使其与任何基于bourne shell(/ bin / sh)的shell一起跨平台工作,而无需bash特定的原语-
echo "$password" | grep -q "[a-z]*[0-9][a-z]*"
if [ $? -eq 0 ] ;then
echo "match found"
else
echo "match not found"
fi
Also feel free to use quotes
around the variable names. 也可以在变量名周围使用
quotes
。 It will save you hours and hours worth of useless debugging. 这样可以节省您数小时的无用调试价值。 :)
:)
Technically it should give you an error like [[hello123 : command not found
. 从技术上讲,它应该给您类似
[[hello123 : command not found
的错误。
The issue is that [[$password
is not expanded how you think it is. 问题是
[[$password
不会扩展您的想法。 Bash will first resolve the $password
variable to what you entered (ie hello123
). Bash首先将
$password
变量解析为您输入的内容(即hello123
)。 This will yield the string [[hello123
which bash will then try to invoke (and fail, as there is nothing with that name). 这将产生字符串
[[hello123
,然后bash将尝试调用该字符串(并且失败,因为该名称没有内容)。
Simply add a space ( 只需添加一个空格(
) after
[[
and bash will recognise [[
as the command to run (although it is a builtin). )
[[
,bash会将[[
识别为要运行的命令(尽管它是内置命令)。
if [[ "$password" == [a-z]*[0-9][a-z]* ]]
then
...
The corrected script is below. 更正后的脚本如下。 The errors were:
错误是:
#!/bin/bash
, not #!bin/bash
#!/bin/bash
,而不是#!bin/bash
passlength=${#password}
, not passlength=$(echo ${#password})
passlength=${#password}
,而不是passlength=$(echo ${#password})
[
or [[
[
或[[
#!/bin/bash echo "enter your password :" read password passlength=${#password} if [[ $passlength -le 8 ]] then echo "you entered correct password" else echo "entered password is incorrect" fi if [[ $password == [az]*[0-9][az]* ]] then echo "match found" else echo "match not found" fi
#!/bin/bash
read -s -p "Enter Password: " password
password_length=${#password}
if [ $password_length -lt 8 -o $password_length -gt 20 ] ;then
echo -e "Invalid password - should be between 8 and 20 characters in length.";
echo ;
else
# Check for invalid characters
case $password in
*[^a-zA-Z0-9]* )
echo -e "Password contains invalid characters.";
echo ;
;;
* )
echo "Password accepted.";
echo ;
break;
;;
esac
fi
More tuned example.. 更多的例子。
In the bash [[
construct, the ==
operator will match glob-style patterns, and =~
will match regular expressions. 在bash
[[
构造中, ==
运算符将匹配glob样式的模式,而=~
将匹配正则表达式。 See the documentation . 请参阅文档 。
Try to replace line 尝试更换线
if [[$password == [a-z]*[0-9][a-z]*]];
with following 与以下
if echo "$password" | grep -qs '[a-z]*[0-9][a-z]*'
HTH 高温超导
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.