Linux Shell: how to check a string whether meets some conditions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Linux Shell: how to check a string whether meets some conditions
# 1  
Old 06-11-2013
Question Linux Shell: how to check a string whether meets some conditions

Hi, guys.
In Linux Shell script, how can I check a string whether meets some conditions.
e.g.:
If a string str must start with a underscore or a alphabet, and it must contains at least one lowercase, one uppercase, one numeric and one punctuation, and its length must be more than 8 characters long and less than 16 characters long, how should I check it?
I can just use the command grep and my command like this:
Code:
a='aBc3o!9203'
echo "$a" | grep  '^[_a-Z]' | grep  '[A-Z]' | grep  '[a-z]' | grep  '[0-9]' | grep  '\W'

It just works when determining whether the correct characters included in the string, and it doesn't include the length check. However, it's so wired and so complex and lengthy.
Does anyone have better ideas?
Thanks!
# 2  
Old 06-11-2013
The english language stating the problem is already longer than your source code, I don't know if it can be made truly short. Smilie But you're right, grep | grep | grep | grep isn't terribly efficient. You could do it in several case statements and use ${#string} to get its length, or pile it all into an awk statement, which isn't terribly efficient itself, but better than five greps, and easier to see what you're doing.

Code:
if echo "$PASS" | awk '/^[_a-zA-Z]/ && /[a-z]/ && /[A-Z]/ && /[0-9]/ && (length($0) > 8) && (length($0) < 16) { X=1 } END { exit(!X) }'
then
        echo "Password is OK"
else
        echo "Password is not OK"
fi

# 3  
Old 06-11-2013
Quote:
Originally Posted by Corona688
The english language stating the problem is already longer than your source code, I don't know if it can be made truly short. Smilie But you're right, grep | grep | grep | grep isn't terribly efficient. You could do it in several case statements and use ${#string} to get its length, or pile it all into an awk statement, which isn't terribly efficient itself, but better than five greps, and easier to see what you're doing.

Code:
if echo "$PASS" | awk '/^[_a-zA-Z]/ && /[a-z]/ && /[A-Z]/ && /[0-9]/ && (length($0) > 8) && (length($0) < 16) { X=1 } END { exit(!X) }'
then
        echo "Password is OK"
else
        echo "Password is not OK"
fi

Yes!
You're right!
Thx!
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux find command : how to use multiple conditions

Hello. I need to find all files but excluding some because I need to exclude some sub-folders I need to exclude some filenames Files must be within two dates. The result is sent to a function I cannot achieved to put together the date conditions, the folder conditions and the... (4 Replies)
Discussion started by: jcdole
4 Replies

2. Shell Programming and Scripting

Linux shell script, search by an input string

So, there is a large file where I have to conduct several search using bash shell scripting. The file is like this: TITLE and AUTHOR ETEXT NO. Aspects of plant life; with special reference to the British flora, 56900 by Robert Lloyd... (1 Reply)
Discussion started by: Philia
1 Replies

3. Shell Programming and Scripting

How to Check Multiple conditions in IF statement?

I wish to check two conditions inside the if statement Condition 1: The two file contents should be identical // using cmp command for this. Condition 2: The two filenames should NOT be the same. This is what i did in vain. if ]; then where entry1 and entry2 are ls *.txt | while... (7 Replies)
Discussion started by: mohtashims
7 Replies

4. Shell Programming and Scripting

Linux Mount Points usage check with shell script

Hi Everyone, Just in need of your help again, I have managed to get a script to check the linux disk usage stuff. But wanted to tweak it little more to get the desired output. Requirement: Now its querying only one mount point, As its not saving in array instead calling it as variables. So... (1 Reply)
Discussion started by: thiyagoo
1 Replies

5. Shell Programming and Scripting

Script to check for few conditions and respond with outputs

All, Here is what I am trying to accomplish: 1. Check for a file of specific name recursively and when I find it, a. if the file is more than 1hr old, no need to email or send notification, so just exit out from the shell script b. if the file is less than 1hr old, send an email to me saying... (3 Replies)
Discussion started by: pnara2
3 Replies

6. Shell Programming and Scripting

korn shell: check the content of a string of a variable

hello, i have a variable which should have following content : var="value1" or var="value2" or var="value2:*" # example: value2:22 how can i check : - if the content is ok (value1 / value2* ) - the two options of "value2" when content is example "value2:22" , i want to split... (3 Replies)
Discussion started by: bora99
3 Replies

7. Shell Programming and Scripting

Linux Shell Script to check for string inside a file?

This is what I have so far grep -lir "some text" * I need to check all files on the system for this text, if the text is found I need to change the file permissions so only ROOT has read and write access. How can this be done? (3 Replies)
Discussion started by: mapleleafs89
3 Replies

8. Shell Programming and Scripting

How to Check given string is number in shell script?

Hi, Can anyone help me out to check whether the input argument is number? Example: REQUEST_ID="123456" I need to check the REQUEST_ID value is number or string. Thanks in Advance Regards BS (6 Replies)
Discussion started by: balajiora
6 Replies
Login or Register to Ask a Question