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
# 1  
Old 10-27-2014
New to UNIX like 2 weeks of study only

why does this not work, confused

Code:
#!/bin/ksh
x="x"
echo "Enter a name: "
read NAME
while [$NAME != $x]
do
echo $NAME
echo
if [$NAME == $NAME]
then
echo ":"
else
echo $NAME
fi
echo
echo "Enter a Name: "
read NAME
done
de

# 2  
Old 10-27-2014
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?
# 3  
Old 10-27-2014
Im trying to understand how to do something I'm working on at work

I'm trying to read a users name from input store it in name, then enter a while loop as long as name is not equal to x, then if name is equal to name do nothing, but if name is != name do something, exit if statement, check name variable if not x continue looping, if x end program. Does that make sense?

---------- Post updated at 09:50 PM ---------- Previous update was at 09:35 PM ----------

so now code is
Code:
#!/bin/ksh
x="x"
echo "Enter a name: "
read NAME
while [ "$NAME" != "$x" ]
do
echo $NAME
echo
if [ $NAME == $NAME ]
then
echo ":"
else
echo $NAME
fi
echo
echo "Enter a Name: "
read NAME
done

When I view the output it keeps asking for a name, and is ignoring the x if name is x, and looks like it is infinite looping. Why?
# 4  
Old 10-27-2014
> Does that make sense?
Mostly, until you get here:
>then if name is equal to name do nothing, but if name is != name do something

I am trying to let you know that name will never be anything else but name. Even if you change the content of variable $NAME, it will always be true, therefore, it does not make any logical sense (in the technical sense).

Now:
[ $NAME != $x ] That makes sense, $NAME can contain something different that $x, but $NAME would never contain something different to compare.
You need to create another variable containing another name you want to compare.

Hope it helps.

Last edited by Aia; 10-27-2014 at 11:05 PM..
# 5  
Old 10-27-2014
Quote:
Originally Posted by Gmoney93079
then if name is equal to name do nothing, but if name is != name do something
We have undestood that, but: "if name is equal to name" is true regardless of the value of "name", so the condition is moot. You could have written "if 1 equals 1" for the same effect.

For any if-else-decision to make sense there must be a condition which is sometimes true, sometimes not true. If it is always true or always not true, then you do not need a condition at all. If i say to you "if the time is in the evening then go to sleep, otherwise get up" there is a condition (what the time is) and sometimes this will be "in the evening", sometimes not, resulting in "get up" sometimes and in "go to sleep" otherwise.

What you do is "if the time is any time then get up" - but you do not need to refer to the time at all in such a case, because its value would be irrelevant anyways.

I hope this helps.

bakunin
# 6  
Old 10-27-2014
ok, let me ask you this then, lets say I enter a users name, I read in the value, while name is not equal to x I do something, I then grep a file to search if the value of name exists in there, if so I don't want it to do anything, if not, then I want it to do something, then exit the if block go back where I left off in while loop, check value of name variable, if name is not x continue looping, once name is an x end program. How would I do that then? In other words how do you compare the value of name to value of name that I grep from another file?
# 7  
Old 10-27-2014
Just as an idea, not meant to be your personal solution.

Code:
#!/bin/ksh

x="x"
echo "Enter a name: "
read NAME

while [ "$NAME" != "$x" ]; do
    
    if [ ! $(grep "$NAME"  filename) ]; then
        echo "$NAME is not found in filename"
    fi

    echo "Enter a name: "
    read NAME
done

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