The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
comparing 2 files using nested for loop vadharah Shell Programming and Scripting 0 03-01-2008 04:32 AM
Nested loop not running using cronjob bihani4u Shell Programming and Scripting 9 09-26-2007 11:19 AM
Nested Loop to Echo Multiple Arrays yongho Shell Programming and Scripting 1 07-12-2005 11:35 AM
Nested while read line loop Rakker Shell Programming and Scripting 7 06-24-2005 07:42 AM
nested loop chinog Shell Programming and Scripting 5 04-20-2005 10:45 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-26-2007
geass geass is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 2
Variable in While Loop Nested If

Hi all,

I'm having problems with the setting a variable in a nested if statement. It doesn't seem to change even if it mets the 'if' condition.
My script essentially looks for a user name from the output from a kerberos command.
When I find the user name, I tried to change a variable and exit the script.

I'm using sh under Fedora Core 5.

Here is part of my script:

#!/bin/sh

#Constants and Globals
USER_EXISTS=1
USER_DOESNT_EXIST=0
user_name=
g_exitcode=

#note: I parse the $user_name from the command line arguments

#Main
g_exitcode=$USER_DOESNT_EXIST

kas list | while read i
do
echo "$i : $user_name"
#compare the user_name with each entry in the kas database
if [ "$user_name" = "$i" ]; then
#Match found
g_exitcode $USER_EXISTS
break
fi
done

exit $g_exitcode

So, even if a match is found, and it enters the nested if statement and sets the g_exitcode to USER_EXISTS at that point, I always exit the script with g_exitcode=USER_DOESNT_EXIST.

Sorry, I'm a little new to scripting. Any help would be appreciated

0
  #2 (permalink)  
Old 03-26-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
g_exitcode $USER_EXISTS

You need to add an equals sign.

g_exitcode=$USER_EXISTS
  #3 (permalink)  
Old 03-26-2007
Dhruva's Avatar
Dhruva Dhruva is offline
Registered User
  
 

Join Date: Mar 2006
Location: India
Posts: 255
Hi perdarabo,I ran the script with small modification but it gives desired result with ksh shell and not in sh shell
please suggest why it is happening


Code:
#Constants and Globals
USER_EXISTS=1
USER_DOESNT_EXIST=0
user_name=$1

#g_exitcode
#export g_exitcode

#note: I parse the $user_name from the command line arguments

#Main
g_exitcode=$USER_DOESNT_EXIST
export g_exitcode

cat kas|{ while read i
 do
echo "$i : $user_name"
#compare the user_name with each entry in the kas database
if [ "$user_name" = "$i" ]; then
#Match found
echo "match found"
echo "$g_exitcode"
g_exitcode=$USER_EXISTS
echo "$g_exitcode"
echo "$g_exitcode $USER_EXISTS"


#break
fi

done }
echo "exit code $g_exitcode"
exit $g_exitcode
output with sh shell
Code:
> sh u102.sh sunil
sunil : sunil
match found
0
1
1 1
exit code 0
output with ksh
Code:
> ksh u102.sh sunil
sunil : sunil
match found
0
1
1 1
exit code 1
  #4 (permalink)  
Old 03-26-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
You probably used Solaris where sh is the bourne shell rather than bash.
  #5 (permalink)  
Old 03-26-2007
Dhruva's Avatar
Dhruva Dhruva is offline
Registered User
  
 

Join Date: Mar 2006
Location: India
Posts: 255
yes exactly i used solaris.So in solaris bourne shell when we assign value in while loop it gets lost out of that while loop.
Is there any way to retain that value.
  #6 (permalink)  
Old 03-26-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Switch to ksh or bash.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:55 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0