Let me ask, cause I think I understand what your saying. In a program I wrote I am entering user information, while name is not equal to x It will echo $NAME, it will then ask for something, and then it uses the something in a function I wrote, it then outputs this information to a file, sorts that file and outputs the sorted file to a new file. Works fine, other than it will accept if the users name was entered twice, this I do not want. So I thought that if I did grep -i '$NAME" filename, I could then check if the value of $NAME exists do nothing, if it doesn't do something. So, is there no way to store a value in a variable and compare it to a variable value with the same name? Or is their a way to take what is in the $NAME = $PREVNAME. Sorry to ask so many questions, but I could sware in C++ I am able to take a variables value and send it to a new variable. which is what I'm trying to understand how to do in UNIX
There has to be a space after [ and before ], like so:
Code:
if [ $NAME == $NAME ]
Now, this is confusing to me, when would $NAME not be equal to $NAME?
What are you confused about it?
Depending on what shell you're using there are lots of cases where:
Code:
if [ $NAME == $NAME ]
then echo true
fi
will give you a syntax error rather than print "true". In fact, the only case where that if statement will work correctly with any shell based on Bourne shell syntax is if NAME has been set to an empty string. In all other cases, the operator should be = instead of ==. If you happen to be using a shell that accepts ==, you still have a syntax error if $NAME expands to more than one word, if it expands to !, or if the expansion contains any of several other characters that are special to the shell).
With the requirements given in post #8 in this thread, the following might be closer to what the OP wants (while using the conventional control-d rather than x to end input):
Code:
#!/bin/ksh
printf 'Enter name (CTL-d to exit): '
while read -r NAME
do printf 'Processing name "%s"...\n' "$NAME"
if [ "$NAME" = '' ]
then printf 'Empty name ignored...\n'
else if grep -Fiq "$NAME" file
then printf 'Name "%s" is already in file.\n' "$NAME"
else printf 'Name "%s" not found.\n' "$NAME"
# Do whatever you want. In this case, add name to file...
printf "%s\n" "$NAME" >> file
fi
fi
printf 'Enter name (CTL-d to exit): '
done
printf 'Done...\n'
If you save this in a file named tester, make it executable by using:
Code:
chmod +x tester
create an empty database of entered names by using:
Code:
touch name
execute it using the command (where user input is shown in bold):
Code:
./tester
Enter name (CTL-d to exit): John Doe
Processing name "John Doe"...
Name "John Doe" not found.
Enter name (CTL-d to exit): Jane Doe
Processing name "Jane Doe"...
Name "Jane Doe" not found.
Enter name (CTL-d to exit): JOHN doe
Processing name "JOHN doe"...
Name "JOHN doe" is already in file.
Enter name (CTL-d to exit):
Processing name ""...
Empty name ignored...
Enter name (CTL-d to exit): Sam Smith
Processing name "Sam Smith"...
Name "Sam Smith" not found.
Enter name (CTL-d to exit): Done...
$ cat file
John Doe
Jane Doe
Sam Smith
$
Hi all,
I hope you are fine,
I'd like study Os I tried a book like Silberschatz it's a good book but like
other books it talks about the concepts abstractly and that's due to it try
to encompass many concepts from many operating systems in GENERAL.
i am not too much comfortable from these... (20 Replies)
Hi..
I am a new joinee to this foram.I need to submit a case study in UNIX .Please help me to submit the case study by giving your valuable ideas.It will be very helpful for me.
Topic: Unix File Management
A university wants to computerize its Library operations because of... (2 Replies)
Hi,
I want to prepare and then appear for Unix admin certification. Please guide me for the study material and Exams that are required to be taken for UNIX certifcation.
Thanks in advance. (11 Replies)
Hi, I need some help with the follow questions :(. Any help would be great!
Answer with the necessary commands
1. In your login directory, make a directory called week4/revision
2. Without changing directories, make another directory week4_revision/data
3. Change to week4_revision/data... (2 Replies)
HI Friends,
I wanted to start this thread inorder to keep all the Unix starters to easily find useful material through this thread...I request you all to provide with the URL address of any gud material you know...
thanks and regards... (2 Replies)
hi I am vijay
how r the unix gurus?
I want to install unix for our sites.So pls tell me abt unix installation user guide & also all the unix commands.Pls help me.I am waiting for yr reply.
bye..........vijay :D (2 Replies)