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
# 8  
Old 05-05-2013
Hi Yoda,

I have finally been able to solve it Smilie, by using below code...
And yes I was calling this function from the mail script. Regarding the echo 0 part, yes I had added it later on, just to check whats the value chaging from & to.
Thanks for you help though....
Code:
while read line
        do
                if [ "$Name" = "$line" ]
                then
                        tempvar=`expr $tempvar + 1`
                        echo $tempvar
                        exit
                fi
        done < $dirUser/names.txt > file.txt
        
newvar=`cat file.txt | head -1`

        if [ $newvar -eq 0 ]
        then
                echo "This is not a valid name... Exiting"
                exit
else
echo "valid"
        fi

& I am getting valid this time finally Smilie
# 9  
Old 05-05-2013
Even though it is working, what you did is not a good programming practice. I highly recommend implementing the suggestion posted by Don Cragun in his previous post.
# 10  
Old 05-05-2013
Quote:
Originally Posted by rituparna_gupta
Hi Yoda,

I have finally been able to solve it Smilie, by using below code...
And yes I was calling this function from the mail script. Regarding the echo 0 part, yes I had added it later on, just to check whats the value chaging from & to.
Thanks for you help though....
Code:
while read line
        do
                if [ "$Name" = "$line" ]
                then
                        tempvar=`expr $tempvar + 1`
                        echo $tempvar
                        exit
                fi
        done < $dirUser/names.txt > file.txt
        
newvar=`cat file.txt | head -1`

        if [ $newvar -eq 0 ]
        then
                echo "This is not a valid name... Exiting"
                exit
else
echo "valid"
        fi

& I am getting valid this time finally Smilie
No. No. No. If you find a matching name in names.txt, this script exits before it gets to the echo "valid".

Something else is going on that you are not showing us.
# 11  
Old 05-05-2013
This one works:
Code:
 while read line
        do
                if [ "$Name" = "$line" ]
                then
                        tempvar=`expr $tempvar + 1`
                        echo $tempvar
                        exit
                fi
        done < $dirUser/names.txt > file.txt
        
newvar=`cat file.txt | head -1`
        if [ $newvar -eq 0 ]
        then
                echo "This is not a valid name... Exiting"
                exit
else
echo "valid value"
        fi

& the value is finally valid outside the loop...
Thanks everyone for your help, though Smilie

---------- Post updated at 12:46 AM ---------- Previous update was at 12:43 AM ----------

sorry for the re-post above... pls ignore
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