Problem with Looping


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with Looping
# 15  
Old 06-12-2013
Quote:
Originally Posted by franksunnn
You are right! Loginname doesn't look so complex. I just took a little practice.
I have a question: what if I want the loginname to start with an underscore or an alphabet and not to contain any spaces? What should I do?
Write down your exact set of requirements as a list of tests that need to be performed. You have the first two done:
  1. Is the 1st character is _ or alpha class?
  2. Are there any spaces? Although I imagine you might want: Are there any characters in class space (rather than just space characters to exclude tabs, newlines, vertical tabs, etc. in addition to the space character itself).
Do you have any other requirements about what characters are allowed or forbidden, minimum or maximum length restrictions, etc?

With your list of requirements and the code I supplied as a sample, try creating your own script. If it doesn't work, show us the complete set of requirements and show us what you tried. Then we'll try to help you fix the script to meet your requirements.

As you get more experience programming, you'll quickly learn it is MUCH easier to write a program if you write a complete set of requirements first.

PS Note that the script I provided doesn't allow any space characters in the login name.

Last edited by Don Cragun; 06-12-2013 at 02:36 PM.. Reason: Add PS.
# 16  
Old 06-12-2013
Quote:
Originally Posted by Don Cragun
Write down your exact set of requirements as a list of tests that need to be performed. You have the first two done:
  1. Is the 1st character is _ or alpha class?
  2. Are there any spaces? Although I imagine you might want: Are there any characters in class space (rather than just space characters to exclude tabs, newlines, vertical tabs, etc. in addition to the space character itself).
Do you have any other requirements about what characters are allowed or forbidden, minimum or maximum length restrictions, etc?

With your list of requirements and the code I supplied as a sample, try creating your own script. If it doesn't work, show us the complete set of requirements and show us what you tried. Then we'll try to help you fix the script to meet your requirements.

As you get more experience programming, you'll quickly learn it is MUCH easier to write a program if you write a complete set of requirements first.

PS Note that the script I provided doesn't allow any space characters in the login name.
OK!
My requirement is below:
1. the first and last character must be an underscore or an alphabet;
2. the length of the loginname should be between 8 and 16 characters long;
3. Except for first and last character, the digit or alphabet or 4 kinds of punctuation ( @ . - _ ) is only allowable.
# 17  
Old 06-12-2013
Quote:
Originally Posted by franksunnn
OK!
My requirement is below:
1. the first and last character must be an underscore or an alphabet;
2. the length of the loginname should be between 8 and 16 characters long;
3. Except for first and last character, the digit or alphabet or 4 kinds of punctuation ( @ . - _ ) is only allowable.
You are off to a great start.

I repeat:
Quote:
With your list of requirements and the code I supplied as a sample, try creating your own script. If it doesn't work, show us the complete set of requirements and show us what you tried. Then we'll try to help you fix the script to meet your requirements.
# 18  
Old 06-12-2013
Quote:
Originally Posted by Don Cragun
You are off to a great start.

I repeat:
I think I did it.
Code:
grep -n '^[_a-Z][a-Z0-9_@.-]\{6,14\}[_a-Z]$' abc

What confused me just now was why I couldn't replace [a-Z0-9_@.-] with [\w@.-].

---------- Post updated at 03:59 PM ---------- Previous update was at 03:27 PM ----------

Quote:
Originally Posted by Corona688
You can avoid printing the first time.

Code:
ERR=0
until [ ... ]
do
        [ "$ERR" -eq 1 ] && echo "error"
        ...
        ERR=1
done

The && is a short-form if/then. If the first command succeeds, the second command will be executed.
I have another question:Why did I get an error with the first code? And it worked well with the second code
first code:
Code:
...
if grep -q "^${loginname}:" /etc/passwd && echo "The loginname already exists." && return 1
elif [ -z "${loginname}" ] && echo "You should enter a nonempty loginname." && return 1
...

Error:
Quote:
./loginname_validate1: line 11: syntax error near unexpected token `elif'
./loginname_validate1: line 11: ` elif [ -z "${loginname}" ] && echo "You should enter a nonempty loginname." && return 1'
second code:
Code:
...
if grep -q "^${loginname}:" /etc/passwd
then
echo "The loginname already exists."
return 1
elif [ -z "${loginname}" ]
then
echo "You should enter a nonempty loginname." 
return 1
...

# 19  
Old 06-12-2013
Quote:
Originally Posted by franksunnn
I think I did it.
Code:
grep -n '^[_a-Z][a-Z0-9_@.-]\{6,14\}[_a-Z]$' abc

What confused me just now was why I couldn't replace [a-Z0-9_@.-] with [\w@.-].
According to the standards, \w in a bracket expression in a Basic Regular Expression (as used by grep when the -E and -F options are not active) matches the two characters \ and w.

What I found surprising is that I hadn't seen anything indicating that the login names you wanted to verify were:
  1. stored in a file,
  2. that your wanted to include the line number in the file where they were stored as part of your list of valid names,
  3. nor that the only character you wanted for the first and last character was "_".
Note that the a-Z in a range expression is treated as an empty set or as an error on most systems I've seen because "a" comes after "Z" in collating order in the C/POSIX locale. Looking more closely at the standards, it is ambiguous as to whether a backwards range should be accepted. If it is accepted it would contain the characters Z, [, \, ], ^, _, `, and a; not lowercase and uppercase letters.
Quote:
Originally Posted by franksunnn

---------- Post updated at 03:59 PM ---------- Previous update was at 03:27 PM ----------


I have another question:Why did I get an error with the first code? And it worked well with the second code
first code:
Code:
...
if grep -q "^${loginname}:" /etc/passwd && echo "The loginname already exists." && return 1
elif [ -z "${loginname}" ] && echo "You should enter a nonempty loginname." && return 1
...

Error:


second code:
Code:
...
if grep -q "^${loginname}:" /etc/passwd
then
echo "The loginname already exists."
return 1
elif [ -z "${loginname}" ]
then
echo "You should enter a nonempty loginname." 
return 1
...

You also did not mention that your code checking valid login names was going to be included in a function. But, the return statement is only vaiid inside a function.

Whether it is in a function or not, there are two ways to code the equivalent of an if statement (ignoring elif clauses):
Code:
if compound-list
then
compound-list of then clause commands
else
compound-list of else clause commands
fi

which matches what you have shown above as second code, and equivalently
Code:
[B]compound-list &&
compound-list of then clause commands ||
compound-list of else clause commands

What you are showing above as first code is a cross between the two that is a syntax error; the if keyword requires matching then and fi keywords.

I had expected that you were going to try to pattern your updated code based on the code I had supplied earlier. I thought it was working for you, but you had changed the requirements concerning what was to be considered a valid login name.

What you have here is a completely different concept. And, with the short snippets of code you have shown, I no longer have any idea what you're trying to do.
This User Gave Thanks to Don Cragun For This Post:
# 20  
Old 06-13-2013
@franksunnn
Quote:
grep -n '^[_a-Z][a-Z0-9_@.-]\{6,14\}[_a-Z]$' abc
Is problematic because it assumes ASCII order.
Better is
Code:
grep -n '^[_a-zA-Z][a-zA-Z0-9_@.-]\{6,14\}[_a-zA-Z]$' abc

or
Code:
grep -n '^[_[:alpha:]][[:alnum:]_@.-]\{6,14\}[_[:alpha:]]$' abc

# 21  
Old 06-13-2013
Quote:
Originally Posted by MadeInGermany
@franksunnn
Is problematic because it assumes ASCII order.
Better is
Code:
grep -n '^[_a-zA-Z][a-zA-Z0-9_@.-]\{6,14\}[_a-zA-Z]$' abc

or
Code:
grep -n '^[_[:alpha:]][[:alnum:]_@.-]\{6,14\}[_[:alpha:]]$' abc

Thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Looping problem

I need help. I am trying to get this script to send out only one email not multiple emails of the abend. Currently it will send me one then and ther with the first and second one then another email with the first second and third abend and so on. I only want one email sent. ... (2 Replies)
Discussion started by: bbcarosi
2 Replies

2. Shell Programming and Scripting

perl: problem in looping! how to get rid

Hi i just want to open 2 files and find difference between cond1 and cond2 and if the difference is greater than or equal to some number say 2 print the lines again in 2 different files. file 1 (1.txt) aqw dfr 34 poilo ggg 98 file 2 (2.txt) qww asd 28 poilo ggg 97 open FILE1,"1.txt" or... (2 Replies)
Discussion started by: anurupa777
2 Replies

3. Shell Programming and Scripting

looping problem

I have been trying to come up with a program that can do this: Say I have a file named "sir" with a single field; 10 229 288 35 83 47 3 I want to create a file "gen" with three fields with the data in file "sire" listed in field 1 while field 2 and 3 are just 1 each like this: SPARSE... (1 Reply)
Discussion started by: iconig
1 Replies

4. Solaris

SVM Solaris 8 Problem. Metastat output looping

Hi friends, I'm newbie to SVM. Just wanna try installed it on one of our server (to do mirroring for disk0 and disk1) but i think im lost until now. :( the steps i've taken is as below:- 1.prtvtoc /dev/rdsk/c1t0d0s2 | fmthard -s - /dev/rdsk/c1t1d0s2 2.metadb -a -c 3 -f c1t0d0s7... (3 Replies)
Discussion started by: kronenose
3 Replies

5. Shell Programming and Scripting

Problem with looping the directories

Hi all, I have a directory which has many sub-directories. Now, I want to check the space of each dir and redirect the output in a file if space exceeds the limit. I have already done it, but the way I did is not very good. I just listed the directories and awked the last column to get the... (5 Replies)
Discussion started by: naw_deepak
5 Replies

6. Shell Programming and Scripting

help with looping

vesselNames values: xxx yyy zzz vesselPlanned values: xxx zzz zzz zzz OIFS="" OIFS=$IFS IFS="\n" (2 Replies)
Discussion started by: finalight
2 Replies

7. Shell Programming and Scripting

for looping

I run into a issue when I try to do sorting of the following with ascending order, one round of for looping seems not working, anyone knows how to use shell or perl? $array = (5,0,3,2,7,9,8) (2 Replies)
Discussion started by: ccp
2 Replies

8. Shell Programming and Scripting

Problem with looping construct

Hi all I have tried to search for this, but keep getting a MySQL db connect error, so am posing the question here, and taking a risk of incurring the wrath of the mods with my first post... I have the following test script: #!/bin/bash HTTPD=`/bin/ps -axcu | /usr/bin/grep httpd... (6 Replies)
Discussion started by: mikie
6 Replies

9. Shell Programming and Scripting

Awk: looping problem!

I am having a problem with awk when I run it with a loop. It works perfectly when I echo a single line from the commandline. For example: echo 'MFG009 9153852832' | awk '$2 ~ /^0-9]$/{print $2}' The Awk command above will print field 2 if field 2 matches 10 digits, but when I run the loop... (5 Replies)
Discussion started by: cstovall
5 Replies

10. UNIX for Dummies Questions & Answers

Help with looping

Hi, Actually I have a file which consists data . for eg names. Then I want my sql query to read this file and produce the output. Currently I am using this FOR EG : FILENAME is NAMES for i in `cat NAMES` { sqlplus -s $CONNECTID << EOF spool rooh set heading off select... (1 Reply)
Discussion started by: rooh
1 Replies
Login or Register to Ask a Question