Sponsored Content
Top Forums Shell Programming and Scripting Issue with accessing value inside while loop, outside it Post 302802955 by Don Cragun on Sunday 5th of May 2013 02:11:23 PM
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).
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 09:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy