New to UNIX like 2 weeks of study only


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting New to UNIX like 2 weeks of study only
# 8  
Old 10-28-2014
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
# 9  
Old 10-28-2014
Quote:
Originally Posted by Aia
There has to be a space after [ and before ], like so:

Code:
Code:
 [ $NAME != $x ]

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
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. SCO

Study UNIX Kernel

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)
Discussion started by: Abdo_8008
20 Replies

2. UNIX for Dummies Questions & Answers

UNIX Study Material

Hi , Can anyone suggest me any UNIX Study material and UNIX Certification specific for TELECOM-DOMAIN. Best Regards, Om Prakash. (14 Replies)
Discussion started by: omprakash1986
14 Replies

3. Programming

UNIX- -Case study - Library management.

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)
Discussion started by: viji_jeya
2 Replies

4. What is on Your Mind?

UNIX Admin (Papers and study material)

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)
Discussion started by: raman1605
11 Replies

5. UNIX for Dummies Questions & Answers

Unix study help

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)
Discussion started by: Heyo
2 Replies

6. UNIX for Dummies Questions & Answers

Any study material for begineers for UNIX please??

Any study material for begineers for UNIX please?? (2 Replies)
Discussion started by: niranjany
2 Replies

7. Programming

Good Unix Online Study Material

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)
Discussion started by: rahul3894singh
2 Replies

8. UNIX for Dummies Questions & Answers

help for unix study

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)
Discussion started by: vi77_surat
2 Replies

9. UNIX for Dummies Questions & Answers

i want to study unix,but it is very dificult.

14 (2 Replies)
Discussion started by: zbweh5280
2 Replies

10. UNIX for Dummies Questions & Answers

I want to start study Unix!

hello .. I am college student and a new guy to unix. I have a simple question: what does "unix" stand for ,or just a meaningless name ? (4 Replies)
Discussion started by: nanuo
4 Replies
Login or Register to Ask a Question