Issue with accessing value inside while loop, outside it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with accessing value inside while loop, outside it
# 1  
Old 05-05-2013
Issue with accessing value inside while loop, outside it

Hi,

Code:
GetName()
{
if [ $abc != "SO" ]
then
        echo " Please enter the name: "
        read Name
        tempvar=0
        while read line
        do
                if [ "$Name" = "$line" ]
                then
                        tempvar=`expr $tempvar + 1`
                        echo $tempvar
                        exit
                fi
        done < $dirUser/names.txt
        
        if [ $tempvar -eq 0 ]
        then
                echo "This is not a valid name... Exiting"
                exit
        fi
fi
}

Even though i have declared the variable outside the while loop, the changing value inside the while loop never reflects outside it Smilie

Always getting the error as : "This is not a valid market... Exiting", even if the name is proper & exists in the list. Inside the while loop it gets set to 1, but outside during the "if" check it takes up the 0 value.

Could someone please help

Thanks,
# 2  
Old 05-05-2013
What shell are you using?

If you are using bash, can you try using regexp comparison operator =~ instead?
Code:
if [[ "$line" =~ "$Name" ]]
then
        tempvar=`expr $tempvar + 1`
        echo $tempvar
        exit
fi

# 3  
Old 05-05-2013
Hi Yoda,

I would have to use Korn Shell, as am using some other symtax in rest of the code which is not compatible with bash
I tried the once you have mentioned above, but its not working (because of shell maybe)
Is there any equivalent in korn shell that I can use?
Thanks,
Ritu
# 4  
Old 05-05-2013
OK. In that case I would suggest to set xtrace / verbose and run your script to help understand what exactly is happening at that if statement:
Code:
#!/bin/ksh -xv

Another option is using grep command:
Code:
if grep "$Name" "$dirUser/names.txt" 1> /dev/null 2> /dev/null
then
        ...
else
        ...
fi

# 5  
Old 05-05-2013
Did that , but the values gets set properly inside the loop, but gets reset once its outside Smilie
Code:
+ echo  Please enter the name:
 Please enter the name:
+ read name
ritu
tempvar=0
+ read line
+ [ ritu = abc ]
+ read line
+ [ ritu = def ]
+ read line
+ [ ritu = ghi ]
+ read line
+ [ ritu = ritu ]
+ expr 0 + 1
tempvar=1
+ echo 1            #<-------------this is the value tat is getting set correctly inside the loop, as the entered name matches entry in the file
1
+ exit
+ echo 0            #<-------------this is where the value gets reset, coz its outside the loop
0
+ [ 0 -eq 0 ]
+ echo This is not a valid name... Exiting             #<-------------coz of incorrect value, it print "invalid name", instead of valid
This is not a valid name... Exiting
+ exit

# 6  
Old 05-05-2013
It is really strange! I don't see below echo statement anywhere in the code that you posted:
Code:
+ exit
+ echo 0            #<-------------this is where the value gets reset, coz its outside the loop
0

Also I don't understand why the script continues to run after exit statement! Are you calling this function from another script?
# 7  
Old 05-05-2013
Quote:
Originally Posted by rituparna_gupta
Hi,

Code:
GetName()
{
if [ $abc != "SO" ]
then
        echo " Please enter the name: "
        read Name
        tempvar=0
        while read line
        do
                if [ "$Name" = "$line" ]
                then
                        tempvar=`expr $tempvar + 1`
                        echo $tempvar
                        exit
                fi
        done < $dirUser/names.txt
        
        if [ $tempvar -eq 0 ]
        then
                echo "This is not a valid name... Exiting"
                exit
        fi
fi
}

Even though i have declared the variable outside the while loop, the changing value inside the while loop never reflects outside it Smilie

Always getting the error as : "This is not a valid market... Exiting", even if the name is proper & exists in the list. Inside the while loop it gets set to 1, but outside during the "if" check it takes up the 0 value.

Could someone please help

Thanks,
Sorry, but I don't believe your statement:
Quote:
Always getting the error as : "This is not a valid market... Exiting", even if the name is proper & exists in the list.
With your code, if the name entered exactly matches a line in names.txt, the function you had exits before it gets to the point that echoes:
Quote:
This is not a valid name... Exiting
When you find a match, you call exit which terminates the while loop, the function, and the shell script that called the function. When you don't find a match, you call exit which terminates the function and the script that called the function. I would think you would want something more like the following:
Code:
GetName() {
if [ $abc != "SO" ]
then    printf "Please enter the name: "
        read Name
        while read line
        do
                if [ "$Name" = "$line" ]
                then    return
                fi
        done < $dirUser/names.txt

        echo "$Name is not a valid name... Exiting"
        exit 1
fi
}

Without knowing the environment in which this function will be used, I would have expected the exit at the end of this function to be either:
Code:
exit 1

if you really want to exit the script calling this function indicating unsuccessful completion, or:
Code:
return 1

indicating that the function failed, but letting the rest of the script continue if it wants to based on the return value from the function. I used exit 1 in my rewrite to follow the statement in your last echo.

Furthermore, I didn't see any need for tempvar, since Name is always valid if $abc is not "SO" and this function returns to the calling program (after my changes) and can never be used if the name is not found (since your program exits).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Accessing multiple directories in loop

Hi Guys, I need to access multiple directories whcih is following similar structure and need to copy those files in desitination path. for eg : if ] then cd ${DIR}/Mon/loaded echo "copying files to $GRS_DIR" cp * ${DIR}/Mon/ echo "Files of Monday are Copied" fi if ] then... (5 Replies)
Discussion started by: rohit_shinez
5 Replies

2. Shell Programming and Scripting

Issue with copying files into dir inside for loop

Hi , I'm trying to move/copy the files inside the loop into a directory . I tried the below code and the issue is the data is not copying into the created directory but the files are copying into another file file_path="/home/etc" Last_Day=20130930 mkdir $file_path/ARC_${Last_Day} ... (3 Replies)
Discussion started by: smile689
3 Replies

3. Shell Programming and Scripting

Perl: accessing reference to variable inside hash.

Below is hash which contains reference to variables: my %mandatoryFields = ( 1 => \$msgtype, 2 => \$switchtype, 3 => \$card_nbr, 4 => \$natv_tran_type_code, 5 => \$amt_1 ); This... (0 Replies)
Discussion started by: som.nitk
0 Replies

4. Shell Programming and Scripting

accessing variable from while loop

Hi all, Here is an outline of the problem: #variable declared at start of script x=0; #a function that increments x by 1 every 10 seconds incrementX(){ increments x every 10 seconds; } #i want this to output the value of x every second. The problem is that x is always reported... (3 Replies)
Discussion started by: free2rhyme2k
3 Replies

5. Shell Programming and Scripting

Error during Accessing Global variable inside function

emailid=myemail@xyz.com taskName="DB-Backup" starttime=`date` email() { subject="$taskName" ": " $* " at `date` " mutt -s "$subject" $emailid < /dev/null } email "Starting" #do my stuff email "Finished" The above code gives following error ./dbbackup.sh: line 6: :... (5 Replies)
Discussion started by: nitiraj.rathore
5 Replies

6. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

7. Shell Programming and Scripting

Accessing Multiple files using for loop

Hi All, I have some files in my directory, and i want to pull all data using for loop....I am using following code but getting error..! for file in {file1, file2, file3, ..... filen} do L="$(tail -1 $file)";NUM=${L%%|*};DAT=${L##*|} echo $NUM>>filedata.txt done Error: tail:... (3 Replies)
Discussion started by: fidelis
3 Replies

8. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Shell Programming and Scripting

Sed inside bash script - accessing multiple files

I have a file (email) containing email addresses. I have a second file (terms) that contains simple regular expressions and words/characters. Here are some examples: \.trainee \.group \.web I want to go through email and delete lines containing the expressions/words from terms and write... (1 Reply)
Discussion started by: manouche
1 Replies

10. UNIX for Dummies Questions & Answers

Accessing redirected file inside script

hi, Is there a way to access the redirected file inside the script. Here is what the command line looks like: $ shar * > archive_file.arc I know I can't access the name of archive_file.arc with positional parameters like $1, $2.. Is there any way to figure out what file the output of the... (3 Replies)
Discussion started by: milhan
3 Replies
Login or Register to Ask a Question