Unix Shell basic loop massive n00b


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix Shell basic loop massive n00b
# 1  
Old 09-01-2011
Error Unix Shell basic loop massive n00b

hey guys
I would really appreciate some help, i need to do a project for a job that requires minimal UNIX scripting and im REALLY stuck

basically Im stuck at what i believe is something really simple but i just dont have a clue how to do it efficiently and properly and i REALLY appreciate some help

Basically i have a loop at which someone has to enter a string of 3 numbers.

-I want the code to check that the string is numbers only
-check that the numbers have not been already signed to someone else in the users.db file
-restrict the number of numbers to only 3.

- each time the user inputs the number wrong i want it to be allowed to correct and tell why he has to re enter it.

This is the best i could came up with but IM REALLY suck and im at a short UNIX course (1 week) for a job....

Can anyone please tell how do i make the loop properly??

this is the best i could came up with... and i KNOW is crap... but i just dont know best........ I would really appreciate some guidance plz

Code:
#!/bin/bash
echo -e  "Create a new record\n"

fSID (){
read -p "Please enter staff ID: " cSID
}

fSID

gcSID=$(grep $cSID users.db | cut -d ":" -f1 | tr [":"] [" "])
wcSID=$(echo $cSID | wc -c) 

case "$cSID" in
      [!0-9]*         ) echo -e "The staff ID should contain only digits\n "
	  sleep 0.5
	  fSID;;
                  
esac  
    while [ $wcSID -gt 4 ]; do 

  read -p "Please enter a value: " cSID 
wcSID=$(echo $cSID | wc -c) 
echo $wcSID
done 
read -p "u passed to stage 2! gratz" st2


Last edited by pludi; 09-01-2011 at 05:09 PM..
# 2  
Old 09-01-2011
Check that the length is three. [ "${#STR}" -eq 3 ]

Check that it's all digits. [ -z "${STR/[0-9]*/}" ]

Check that it's not in the file. ! grep $ID user.db #??? not perfect but best I can do without knowing what user.db looks like

So

Code:
STR=""
until [ "${#STR}" -eq 3 ] && [ -z "${STR/[0-9]*/}" ] && ! grep "$STR" user.db > /dev/null
do
         read STR
done

# 3  
Old 09-01-2011
Quote:
Originally Posted by Corona688
Check that the length is three. [ "${#STR}" -eq 3 ]

Check that it's all digits. [ -z "${STR/[0-9]*/}" ]

Check that it's not in the file. ! grep $ID user.db #??? not perfect but best I can do without knowing what user.db looks like

So

Code:
STR=""
until [ "${#STR}" -eq 3 ] && [ -z "${STR/[0-9]*/}" ] && ! grep "$STR" user.db > /dev/null
do
         read STR
done

Thank you!!

I really appreciate that you took the time to help me out Smilie

However, how do I tell the user then why they are re inserting the information then?
specially if the entry is already in the user.db
coz that code will do it over and over again until is right but the person will not know why they have to re enter the info

Thank you again!
u r a life saver!
# 4  
Old 09-01-2011
I asked what the users.db looked like too, and noted that my grep solution probably doesn't do what you want because I don't know what users.db looks like...

Code:
while true
do
        read ID
        if [ "${#ID}" -ne 3]
        then
                echo "wrong number of digits"
                continue
        fi

        if ! [ -z "${ID/[0-9]*/}" ]
        then
                echo "Not all digits"
                continue
        fi

        if grep "$ID" users.db > /dev/null
        then
                echo "The string '$ID' exists anywhere at all in users.db"
                continue
        fi
        break # get out of loop
done

# 5  
Old 09-01-2011
Quote:
Originally Posted by Corona688
I asked what the users.db looked like too, and noted that my grep solution probably doesn't do what you want because I don't know what users.db looks like...

Code:
while true
do
        read ID
        if [ "${#ID}" -ne 3]
        then
                echo "wrong number of digits"
                continue
        fi

        if ! [ -z "${ID/[0-9]*/}" ]
        then
                echo "Not all digits"
                continue
        fi

        if grep "$ID" users.db > /dev/null
        then
                echo "The string '$ID' exists anywhere at all in users.db"
                continue
        fi
        break # get out of loop
done


the db looks like thi
Quote:
423:name:surname:18:14:5
666:Ferris:Lucy:15:6:17
069:Chong:Annabel:15:20:16
987:Marple:Mike:13:19:12
the ID is the first one

Thank you!!
# 6  
Old 09-01-2011
grep "^${ID}:" users.db > /dev/null then. ^ means beginning of the line.
# 7  
Old 09-01-2011
Quote:
Originally Posted by Corona688
grep "^${ID}:" users.db > /dev/null then. ^ means beginning of the line.
thank you!!

Another question:

how would i be a really cool way (the way u do it) to retrive the one b4 the last ":".
Shall i use "cut" and "tr" or there is an easier way?

also how could i save it to an array? ( you will prob know how to do it in 1 step and i would do it in 300)

Thank you so much!!... you prob saved me 4 hrs trying to figure it out and it would never look as neat!

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

Btw i ran the program and it returns an error after i written down the string
Quote:
1: line 5 p: [missing ']'
and it doesnt show any message
why is that?

---------- Post updated at 03:50 PM ---------- Previous update was at 03:41 PM ----------

Quote:
#!/bin/bash
while true
do
read -p "Please enter staff ID: " cSID
if [ "${#cSID}" -ne 3]
then
echo "wrong number of digits"
continue
fi

if ! [ -z "${cSID/[0-9]*/}" ]
then
echo "Not all digits"
continue
fi

if grep "^${cSID}:" users.db > /dev/null
then
echo "The string '$cSID' exists anywhere at all in users.db"
continue
fi
break # get out of loop
done

Last edited by thurft; 09-01-2011 at 05:50 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Basic FOR loop with break

Oracle Linux : 6.4/bash shell In the below I want to break out of the loop when it enters the 5th iteration. #!/bin/bash for i in 1 2 3 4 5 6 do echo "$i" if echo "Oh Nooo... i = $i. I need to stop the iteration and jump out of the loop" then break fi done But, it only... (3 Replies)
Discussion started by: John K
3 Replies

2. UNIX for Dummies Questions & Answers

Basic loop awk/shell script question..

Hi, Sorry if this is a newbie question. I guess you can use either awk or shell script for this sequence of operations, but knowing very little about either of them I'm not sure how I should try to write this. The basic objective is to copy certain files that are scattered all over my... (10 Replies)
Discussion started by: pc2001
10 Replies

3. Shell Programming and Scripting

Shell pipeline help for a n00b

I need to read input from a file, and make sure nothing prints after column 72. basically, ignore input after character 72 until the next newline character. Any help is appreciated. I have been searching forever! (10 Replies)
Discussion started by: Gbear
10 Replies

4. Shell Programming and Scripting

which is the Very basic certification on unix shell scripting?

Hi, I am very new to this forum, can any one tell me which is the very basic certification on unix shell scripting? please give me an advice on this. (1 Reply)
Discussion started by: Manjesh
1 Replies

5. Shell Programming and Scripting

Help required on basic Unix Bourne Shell Script

Howdy People :), I'm a newbie & its my first question here. I've started learning Unix Bourne Shell scripting recently and struggling already :p Can someone PLEASE help me with the following problem. Somehow my script is not working. Display an initial prompt of the form: Welcome to... (1 Reply)
Discussion started by: methopoth
1 Replies

6. Shell Programming and Scripting

Shell Scripting n00b

Hey Guys! I was hoping for some help with a simple script I'm trying to use. I have the script set up to pull some simple information out of a database .txt file and sed it into a preexisting template for assignment cover letters. The problem seems to be someplace in the sed statement but I... (5 Replies)
Discussion started by: BFeicht
5 Replies

7. Programming

Creating a basic UNIX shell script for chatting

Hey guys, This is quite simply what I'm trying to make: A program that runs in a UNIX terminal that you can output text messages to from another machine. These text messages would be prepended with a customized prompt. I'd also like to have the window spew out random dumps of flavor text not... (1 Reply)
Discussion started by: AcerAspirant
1 Replies

8. Shell Programming and Scripting

unix shell basic

need some help badly. if i had a file with content of few lines like the followings. 1183724770.651 0.049 137.157.56.169 200 415 GET http://venus/client/clients.xml "text/xml" 1183724770.651 0.049 141.183.101.250 200 415 GET http://venus/client/clients.xml "text/xml" using what command... (1 Reply)
Discussion started by: akagi07
1 Replies

9. UNIX for Dummies Questions & Answers

A basic question of FOR loop

Hi, have a basic query. Please see the below code: list="one two three" for var in $list ; do echo $var list="nolist" Done Wht if I want to print only first/ last line in the list Eg one & three Regards er_ashu (3 Replies)
Discussion started by: er_ashu
3 Replies

10. UNIX for Dummies Questions & Answers

Absolute n00b questions about Unix / Linux

Hi All, I have absolutely no experince with either one, and would LOVE to start from somewhere! So please guide me to some web sites (beside these great forums of course!) that I can obtain n00b information. (Books, links, resources, etc.) What software OS? should I begin with? I have heard... (2 Replies)
Discussion started by: CodeHunter
2 Replies
Login or Register to Ask a Question