05-22-2008
Quote:
Originally Posted by
dinjo_jo
Hi ,
I'm trying to change the variable value in a while loop , however its not working it seems that the problem with subshells while reading the file.
#!/bin/sh
FLAG=0;
cat filename | while read data
do
FLAG=1;
done
echo $FLAG
Should display 1 instead displays 0
It will only display the value of FLAG as 1 when the condition is satisfied,that means there is some file you are trying to cat and reading lines. So check for the file first.
Thanks..
10 More Discussions You Might Find Interesting
1. Programming
I'm having a problem getting this to work..
I got 3 files,
start.C - Where i got my main() function
Menu.C & Menu.h - Where I'm trying to use hash_map
start.C
#include <iostream>
#include "Menu.h"
using namespace std;
int main() { /* test code here */ return 0; }
Menu.h ... (1 Reply)
Discussion started by: J.P
1 Replies
2. Programming
Each thread has a copy of auto variables within a function, but variables
declared as static within a function are common to all threads. To circumvent
this can static variables be placed outside the function. If so, will the
scope of the variable be file only or will it be extern, and will each... (7 Replies)
Discussion started by: sundaresh
7 Replies
3. UNIX for Advanced & Expert Users
Sorry in the wrong forum. Moving this to right forum. (2 Replies)
Discussion started by: Amruta Pitkar
2 Replies
4. Shell Programming and Scripting
My awk script searches for specified patterns in a text file and stores these values into mem variables.
Once this is done I want to Insert these values into a table.
How can I avail of the variable values outside the scope of awk script....
One method that I have tried is to write the... (7 Replies)
Discussion started by: Amruta Pitkar
7 Replies
5. Shell Programming and Scripting
Heres an example.....
<~/abc>$ cat textfile
line 1
line 2
line 3
line 4
line 5
<~/abc>$ cat try.sh
#/bin/ksh
for runs in 1 2 3
do
A=$runs
echo "Inside A : $A"
done
echo "Outside A : $A" <- works fine (1 Reply)
Discussion started by: qzv2jm
1 Replies
6. AIX
What is the scope of AIX as I am starting my career as a fresher in AIX administration?? (4 Replies)
Discussion started by: abhishek27
4 Replies
7. Shell Programming and Scripting
I call my script with two parameters
myscript.sh aaa bbb
What is the way to access $1 and $2 values inside a function? I call the function like this
myfuntion $1 $1
but inside of the function, $1 and $2 are empty. Any suggestions? thank you in advanced. (1 Reply)
Discussion started by: aristegui
1 Replies
8. Shell Programming and Scripting
If I set a variable within a while-read loop, sometimes it's local to the loop, sometimes it's global, depending on how the loop is set up. I'm testing this on a Debian Lenny system using both bash and dash with the same results.
For example:
# Pipe command into while-read loop
count=
ls -1... (2 Replies)
Discussion started by: mjd_tech
2 Replies
9. Shell Programming and Scripting
Hi,
I want to know about the variable scope in shell script.
How can we use the script argument inside the function?
fn () {
echo $1 ## I want this argument should be the main script argument and not the funtion argument.
}
also are there any local,global types in shell script?
if... (3 Replies)
Discussion started by: shellwell
3 Replies
10. Shell Programming and Scripting
Friends,
I am using ksh under SunoS. This is what I have
In file1.sh
NOW=$(date +"%b-%d-%y")
LOGFILE="./log-$NOW.log"
I will be using this file through file1.sh as log file.
I have another script file2.sh which is being called inside my file1.sh. I would like to use the same log... (6 Replies)
Discussion started by: dahlia84
6 Replies
LEARN ABOUT CENTOS
perl::critic::policy::variables::prohibitreusednames
Perl::Critic::Policy::Variables::ProhibitReusedNames(3) User Contributed Perl DocumentationPerl::Critic::Policy::Variables::ProhibitReusedNames(3)
NAME
Perl::Critic::Policy::Variables::ProhibitReusedNames - Do not reuse a variable name in a lexical scope
AFFILIATION
This Policy is part of the core Perl::Critic distribution.
DESCRIPTION
It's really hard on future maintenance programmers if you reuse a variable name in a lexical scope. The programmer is at risk of confusing
which variable is which. And, worse, the programmer could accidentally remove the inner declaration, thus silently changing the meaning of
the inner code to use the outer variable.
my $x = 1;
for my $i (0 .. 10) {
my $x = $i+1; # not OK, "$x" reused
}
With "use warnings" in effect, Perl will warn you if you reuse a variable name at the same scope level but not within nested scopes. Like
so:
% perl -we 'my $x; my $x'
"my" variable $x masks earlier declaration in same scope at -e line 1.
This policy takes that warning to a stricter level.
CAVEATS
Crossing subroutines
This policy looks across subroutine boundaries. So, the following may be a false positive for you:
sub make_accessor {
my ($self, $fieldname) = @_;
return sub {
my ($self) = @_; # false positive, $self declared as reused
return $self->{$fieldname};
}
}
This is intentional, though, because it catches bugs like this:
my $debug_mode = 0;
sub set_debug {
my $debug_mode = 1; # accidental redeclaration
}
I've done this myself several times -- it's a strong habit to put that "my" in front of variables at the start of subroutines.
Performance
The current implementation walks the tree over and over. For a big file, this can be a huge time sink. I'm considering rewriting to
search the document just once for variable declarations and cache the tree walking on that single analysis.
CONFIGURATION
This policy has a single option, "allow", which is a list of names to never count as duplicates. It defaults to containing $self and
$class. You add to this by adding something like this to your .perlcriticrc:
[Variables::ProhibitReusedNames]
allow = $self $class @blah
AUTHOR
Chris Dolan <cdolan@cpan.org>
This policy is inspired by <http://use.perl.org/~jdavidb/journal/37548>. Java does not allow you to reuse variable names declared in outer
scopes, which I think is a nice feature.
COPYRIGHT
Copyright (c) 2008-2011 Chris Dolan
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license
can be found in the LICENSE file included with this module.
perl v5.16.3 2014-06-09 Perl::Critic::Policy::Variables::ProhibitReusedNames(3)