why the script is not terminating with proper results


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users why the script is not terminating with proper results
# 1  
Old 11-18-2011
why the script is not terminating with proper results

Hi everyone,
I am new to the linux.I wrote a small script and assigning two values to fname and lname and I want if the fname or lname are not given proper name like Toys or Gun the script should terminate and if they are given proper name it should execute.please help thanksSmilie
Code:
#!/bin/bash
fname=Toys
lname=Gun
y=VeryGood
x=Goodbye
echo "your first name please"
read fname
echo "Hello $fname, Lets be friend!"
echo "your last name Please"
read lname
echo "$lname,Lets work together!"
if
[ $fname=Toys ]
read fname
then
echo "$y"
 if
[ $lname=Gun ]
read lname
then
echo "$y"
else
echo "lets move next"
Exit
fi
else
Exit
echo "$x"
 fi

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 11-18-2011 at 04:36 PM.. Reason: code tags, please!
# 2  
Old 11-18-2011
Statements in [ ] need to be spaced and quoted properly.
The 'if' has to be on the same line. The 'then' has to be on the next line or immediately after a ;

case is important. Don't capitalize "exit".

There's no point adding a command after exit. It won't ever be executed.

Code:
if [ "$fname" = something ]
then

I'm pretty sure you don't need to nest your if-statements 9-deep like that, either. If you find something wrong, just exit instead of slapping on another 'if'.
Code:
#!/bin/bash

echo "your first name please"
read fname

echo "your last name Please"
read lname

if [ "$lname" != "toys" -o "$fname" != "Gun" ]
then
        echo "Wrong name.  Goodbye!"
        exit 1
fi

echo "User $lname, $fname accepted"

# 3  
Old 11-18-2011
thanks for the guidance- Appreciate for the help

---------- Post updated at 04:30 PM ---------- Previous update was at 04:08 PM ----------

one error i see after corona 688 wrote some comment and I executed the file at the end of the file it says "too many arguments" any clue
# 4  
Old 11-18-2011
Please post your code, I can't tell what you did without it. The code I gave you works.
# 5  
Old 11-18-2011
or with ksh:
Code:
#!/bin/ksh

read fname?'your first name please '

read lname?'your last name Please '

if [ "$lname" != toys -o "$fname" != Gun ]
then
        echo 'Wrong name.  Goodbye!'
        exit 1
fi

echo "User $lname, $fname accepted"

This User Gave Thanks to vgersh99 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

tcsh: how to prevent a foreach from terminating a script when the result is null?

Sorry if this has been answered. I did search both Google and this site and did find this post: unix.com/unix-dummies-questions-answers/152992-how-ignore-errors-script.html However, it wasn't answered. I have the same question - how do you prevent a tcsh script from terminating when the... (4 Replies)
Discussion started by: deepstructure
4 Replies

2. Shell Programming and Scripting

Getting Proper Date Format in SH Script

There's a small SH script I'm trying to write where it will get the current month and find a log file that is based on the date. Example: Today is February, so the log file is going to be 201102.log (2011 + 02) An additional thing is that if today is the 1st of a month, it will also find the log... (3 Replies)
Discussion started by: kooshi
3 Replies

3. Shell Programming and Scripting

issue invoking shell script using cron, even with proper file permission

I am using tcsh what could possibly be a problem, when using crontab to invoke a shell script. ? The script has the read, write and execute permission to all users. And the script works as expected while executing it in stand-alone mode. Is there a way to trace (like log) what error... (9 Replies)
Discussion started by: vikram3.r
9 Replies

4. Shell Programming and Scripting

How can i terminating expect script without terminating SSH connection.

Hi all , i know i ask a lot of question but these are really hard to solve and important question. I send two scripts: expect.sh: #!/usr/local/bin/expect spawn ssh root@172.30.64.163 expect "login:" send "root\n" expect "password:" send "root\n^M" interact and son.sh: ... (2 Replies)
Discussion started by: fozay
2 Replies

5. Shell Programming and Scripting

how to terminate ssh connection without terminating script

Hi all, I connect with SSH connection to remote machine in the script and ı want to logout at half of the script then continue to script. If ı write exit in the script it terminates script not SSH connection. How can i do that please help me (1 Reply)
Discussion started by: fozay
1 Replies

6. Shell Programming and Scripting

getting proper o/p from a procedure in a script

foll. is my code snippet. #!/bin/ksh retVal=`sqlplus -s user/passwd@oracle_sid <<EOF SET SERVEROUTPUT ON SIZE 100000 DECLARE STATUS_VALUE CHAR; BEGIN SELECT temp1 INTO STATUS_VALUE FROM sai; DBMS_OUTPUT.PUT_LINE(STATUS_VALUE); END; / SET... (1 Reply)
Discussion started by: sainathdeg
1 Replies

7. Shell Programming and Scripting

script unexpectedly terminating

I now that this isnt the greatest code around. Im a network guy by trade not a programer .. but needed something to compare config files ... Anyway ... intermittently, the program terminates. Ive been looking at the code for a week trying to figure it out and Im stumped. Can anyone provide... (0 Replies)
Discussion started by: popeye
0 Replies

8. AIX

The shell script is not returning proper result

Can anybody pls look into this script and tell me where I went wrong. After running this script, it is showing like "Trying to overlay current working directory ABORT!!!" :-( ARGCNT=$# if then echo "Two parameters are needed for this shell " echo "Please try again with... (1 Reply)
Discussion started by: clnsharma123
1 Replies

9. UNIX for Dummies Questions & Answers

Terminating child script with terminating the parent script

Hi I was working on a shell script with randomly shows a page of text from a randomly selected topic .As soon as the page is displayed it callers a timer script which keeps on running indefinitely until the timer script is killed by the user. This is where I have the problem,if I press... (2 Replies)
Discussion started by: mervin2006
2 Replies

10. Shell Programming and Scripting

terminating script with CTRL+D

Hi, I'm trying to make a script that reads the console input and terminates with CTRL+D. It's absolutely basic but I don't know how to "read" the CTRL+D. I've tried a bunch of things like EOT=^D while //with & without quotations do read input echo $input done while while ] ... (12 Replies)
Discussion started by: sanchopansa
12 Replies
Login or Register to Ask a Question