urgent :S dont know why its not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting urgent :S dont know why its not working
# 1  
Old 05-31-2011
urgent :S dont know why its not working

hello, i am trying to put users2 information to file appraisalrecord, however it is not working and i tried everything. overall, i just want to show that when they enter the user number it will verify in the file to see does that user number exist, if yes they have to a new user number if no they continue on and the user number is put in the appraisalrecord. hope you understand what im saying. im using UNIX shell as well.
Code:
function newrecord () {
clear
read -p "please enter a user number: " users2

output=$(grep "$users2" appraisalrecord)
output1=$(echo $users2 | tr -dc '[:digit:]')
if [[ -n "$output" ]]; then
echo -e "The ID has already been taken\n"
sleep 2
newrecord
fi
if [[ $users2 = $output1 ]]; then
echo -e "$users2 has been accepted\n"
else
echo -e "The input must be a numerical number\n"
        sleep 2
        newrecord
echo $users2 >> appraisalrecord
fi

Moderator's Comments:
Mod Comment Please use code tags, and a more descriptive subject title for threads.

Last edited by Scott; 05-31-2011 at 03:11 PM.. Reason: Code tags
# 2  
Old 05-31-2011
In what manner does it "not work"? Be specific.

Quote:
Originally Posted by bassmasta1
hello, i am trying to put users2 information to file appraisalrecord, however it is not working and i tried everything. overall, i just want to show that when they enter the user number it will verify in the file to see does that user number exist, if yes they have to a new user number if no they continue on and the user number is put in the appraisalrecord. hope you understand what im saying. im using UNIX shell as well.
"what kind of shell are you using?" "unix"
"what kind of car are you driving?" "blue"

UNIX has many shells, "UNIX shell" tells us nothing.

Code:
function newrecord () {
clear
read -p "please enter a user number: " users2

If you're not using BASH, read -p won't work or won't do what you expect -- -p means read from a coprocess in ksh for instance, and other shells won't have it at all.


Code:
output=$(grep "$users2" appraisalrecord)

What's in appraisalrecord?
Code:
output1=$(echo $users2 | tr -dc '[:digit:]')

This is to remove everything except digits I take it?

Code:
if [[ -n "$output" ]]; then
echo -e "The ID has already been taken\n"
sleep 2
newrecord
fi

Why is newrecord calling itself? This will be a recursion. And the existing call to newrecord won't end, potentially making it an infinite recursion. You should have a loop outside newrecord that keeps calling it in a loop if you want it to happen more than once.

Instead of checking the output of grep, why not check grep's return value? it returns nonzero when nothing's found, and zero when it's found, so:

Code:
if grep -q "$users2" appraisalrecord
then
        echo "The ID has already been taken"
        echo
...

Code:
if [[ $users2 = $output1 ]]; then

This will blow up whenever users2 or output1 are blank, put quotes around them.
# 3  
Old 05-31-2011
Quote:
Originally Posted by Corona688
In what manner does it "not work"? Be specific.

"what kind of shell are you using?" "unix"
"what kind of car are you driving?" "blue"

UNIX has many shells, "UNIX shell" tells us nothing.

Unix has many shells, but there is only one "Unix shell", i.e. the POSIX shell.
Quote:
Code:
function newrecord () {


The function definition syntax (a mishmash of ksh and POSIX) indicates that the shell is bash.
Quote:
Code:
clear
read -p "please enter a user number: " users2

If you're not using BASH, read -p won't work or won't do what you expect -- -p means read from a coprocess in ksh for instance, and other shells won't have it at all.

Another indication that he is using bash.
Quote:

Code:
output=$(grep "$users2" appraisalrecord)

What's in appraisalrecord?
Code:
output1=$(echo $users2 | tr -dc '[:digit:]')

This is to remove everything except digits I take it?

In bash (or ksh93), this can be done without an external command:
Code:
output1=${users2//[![:digit:]]/}

But a better way would be to use a case statement to determine whether there are any non-digit characters:
Code:
case $users2 in
 *[![:digit:]]*) echo not number; ## do whatever
                  ;;
  *) ;; ## OK
esac

Quote:


Code:
if [[ $users2 = $output1 ]]; then

This will blow up whenever users2 or output1 are blank, put quotes around them.

Inside (non-standard) [[ ... ]], variables need not be quoted.
# 4  
Old 05-31-2011
After several posts, please let us know about your envirnment:
Please post:
Code:
echo $SHELL
uname -a

On one of your earlier posts I commented anout the contstruct of a function.
You post:
Quote:
function newrecord () {
This is not correct syntax.
Try:
Code:
newrecord () {

And don't forget the closing brace "}" to end the function. This closing brace is missing from your post.
After correction and taking into account cfajohnon post (if you have a Posix Shell) please post the amended version of your script complete with what you type to execute the script and all output (unedited).

---------- Post updated at 00:59 ---------- Previous update was at 00:54 ----------

@cfajohnson
Quote:
Unix has many shells, but there is only one "Unix shell", i.e. the POSIX shell.
You are joking LOL?
# 5  
Old 05-31-2011
Quote:
Originally Posted by methyl
After several posts, please let us know about your envirnment:
Please post:
Code:
echo $SHELL
uname -a


The SHELL variable will not necessarily tell you what shell is being used to execute the script, and the system is irrelevant to this script.
Quote:
On one of your earlier posts I commented anout the contstruct of a function.
You post:
Quote:
Code:
function newrecord () {

This is not correct syntax.

It is acceptable in bash, but it is neither standard nor ksh.

(Functions appeared first in ksh, with the syntax function funcname { ... }; then Bourne added functions to his shell using what is now the standard syntax, funcname() { ... }.
Quote:
Try:
Code:
newrecord () {

...
---------- Post updated at 00:59 ---------- Previous update was at 00:54 ----------


@cfajohnson
Quote:
Unix has many shells, but there is only one "Unix shell", i.e. the POSIX shell.
You are joking LOL?

No, I am perfectly serious. The "Unix shell" is, by definition, the POSIX shell. There are various implementations of the standard shell (bash, dash, ksh93), and some have a number of additions. There are also other shells (rc, etc.) that do not implement the standard Unix shell.
This User Gave Thanks to cfajohnson For This Post:
# 6  
Old 05-31-2011
@cfajohnson
No offence meant or implied. A high proportion of troublesome posts on this board prove to be non-Posix Shells. An empty response to "echo $SHELL" is a warning sign.


Code:
(Functions appeared first in ksh, with the syntax function funcname { ... } ; then Bourne added functions to his shell using what is now the standard syntax, funcname() { ... } .

Sorry but this is not correct as posted. Bourne (1977) preceded Korn (198x). If the O/P has posted "/bin/bash" in response to "echo $SHELL" then we know that normal unix Shell is irreleveant.
Btw. I like "ksh" because it is a consistent standard (yep I exclude ksh95), and I don't like the vague and inconsistent implementations of "Posix Shell" over the years.
# 7  
Old 06-01-2011
Quote:
Originally Posted by methyl
Code:
(Functions appeared first in ksh, with the syntax function funcname { ... } ; then Bourne added functions to his shell using what is now the standard syntax, funcname() { ... } .

Sorry but this is not correct as posted. Bourne (1977) preceded Korn (198x).

Functions were the last thing that Steve Bourne added to his shell; he did that in 1983: Interview with Steven Bourne - Computerworld

Ksh was originally written in the 1970s, and had functions very early on.
Quote:
If the O/P has posted "/bin/bash" in response to "echo $SHELL" then we know that normal unix Shell is irreleveant.

Why? Bash is a normal Unix shell.
Quote:
Btw. I like "ksh" because it is a consistent standard (yep I exclude ksh95), and I don't like the vague and inconsistent implementations of "Posix Shell" over the years.

While the original POSIX shell standard was largely based on ksh, ksh is not a standard; it has a well-defined specification, but that's not the same thing.

(And you mean ksh93, not ksh95.)
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

URGENT Reading a file and assessing the syntax shell script URGENT

I am trying to write a shell script which takes an input file as an arguement in the terminal e.g. bash shellscriptname.sh input.txt. I would like for the file to be read line by line each time checking if the .txt file contains certain words or letters(validating the syntax). If the line being... (1 Reply)
Discussion started by: Gurdza32
1 Replies

2. UNIX for Advanced & Expert Users

Urgent ssh -1 not working

Hi guys please help with the following. $ ssh -1 -v -l username -o "ForwardX11 yes" server.name netscape OpenSSH_5.8p1, OpenSSL 0.9.8r 8 Feb 2011 debug1: Connecting to proxy-bt-2 port 22. debug1: Connection established. debug1: identity file /home/username/.ssh/identity type -1 debug1:... (1 Reply)
Discussion started by: llcooljatt
1 Replies

3. Shell Programming and Scripting

i dont know where problem!!

okthanksi solve it :) (1 Reply)
Discussion started by: dream23
1 Replies

4. Shell Programming and Scripting

If not working...pls help:URGENT

Following is d code snipet #!/bin/ksh retVal=`sqlplus -s user/passwd\@oracle_sid <<EOF SET SERVEROUTPUT ON SIZE 100000 DECLARE STATUS_VALUE VARCHAR2(1); BEGIN SELECT temp1 INTO STATUS_VALUE FROM sai; DBMS_OUTPUT.PUT_LINE(STATUS_VALUE); END; / exit; EOF` echo "Return Value... (4 Replies)
Discussion started by: sainathdeg
4 Replies

5. UNIX for Dummies Questions & Answers

Urgent -Please help me 'mail' option not working

Hi , I have already posted a post regarding this, but i didn't get my problem solve, so some body help me as it was urgent for me, my mail option is not working, when i send a mail, it is strucking in /var/spool/mqueue folder. and i am getting struck there itself, i see below two files are... (1 Reply)
Discussion started by: mars_girish9
1 Replies

6. UNIX for Advanced & Expert Users

URGENT,URGENT- Need help tape drive installation

Hi, I am trying to attach tape drive to sun V890 running Solaris 9 on it. I have installed HBA(qlogic) in slot 1 of 0-8 slots and booted the system. I do not see HBAin prtdiag output. The tape drive is not attached to HBA. The tape drive I am going to attach is Sony AIT3. 1.How can I make... (3 Replies)
Discussion started by: sriny
3 Replies

7. What is on Your Mind?

dont understand

i'm trying to learn unix and i posted a question and what i was typing from school. i can't figure it out. how am i supposed to learn , when i get shutdown by an admin. for posting a homework question. doesn't make any sense. its a dumb rule. thanks for helping (4 Replies)
Discussion started by: AtomJ22
4 Replies

8. Programming

I dont want this

Im creating a sort of shell, for my cybercafe This will restrict my clients from accessing unwanted materials so im programming a similar bash to 1. to meet my goals 2. to learn new things. im creating it in C, please have a look at the attachement. i wish to avoid having a blank space... (6 Replies)
Discussion started by: C|[anti-trust]
6 Replies
Login or Register to Ask a Question