Programming :: Variable Is Gone At The End Of While Loop?

Mar 25, 2011

I have a directory file capturing script, the variable is fine with in the loop but gone after the loop is done:



View 9 Replies


Programming :: Bash - Read Content Of File To Variable And Use This Variable In For Loop ?

Aug 21, 2009

I'm trying to read content of file to variable and use this variable in for loop. The problem is, when I have c++ comment style in file - /*. Spaces in line are also interpreted as separated lines.

For example:


Changing $files to "$files" eliminate these problems but causes that whole content of variable is treated as one string (one execution of loop).

View 6 Replies View Related

Programming :: Variable Name Change In A Loop?

Apr 24, 2010

I am about to move my scripts to the next generation level, so I need some help I am stuck in varying my variable names in a loop. For example:

for user in ben dorothy mike pat
[ -r /home/$user ] && let "$user"check=1 || let "$user"check=0


View 1 Replies View Related

Programming :: Using A Variable To Control A BASH For Loop

Jul 3, 2009

I know of 4 different ways to use a for loop:

1. for I in {1..10}; do echo $I; done|

2. for I in 1 2 3 4 5 6 7 8 9 10; do echo $I; done|

3. for I in $(seq 1 10); do echo $I; done|

4. for ((I=1; I <= 10 ; I++)); do echo $I; done

I have a script which uses the 1st form of for loop. I'm trying to modify it to use a variable instead of a static hard-coded value in the section that controls the looping.of the for loop.

I've tried all different ways of quoting and escaping the variable, and the problem is that the quoting chars and escape char are being translated and passed into the loop along with the value stored in the variable.

For example, to change the start value of 1 to whatever value I want passed in through a variable:


I have tried: {{$a}..10} and {`$a`..10}, to have the variable evaluated first.

I have tried using the eval() function.

I have tried single and double quotes and the backslash escape character.

Nothing I've tried works. It's probably a syntax error.

View 14 Replies View Related

Programming :: Bash Variable Parsing / Extract And Put Into Variables Each Combination Of F1 And F2 In A Loop?

Oct 14, 2010

I have a bash variable where the content looks like this where ;f1; and ;f2; are delimiters:
;f1;field1value1;f2;field2 value1 ;f1;field1value2;f2;field2 value2 ;f1;field1value3;f2;field2 value3

So what I need is to extract and put into variables each combination of f1 and f2 in a loop to something like that:

#first pass of the loop I need:
f2=field2 value1

#second pass of the loop I need:
f2=field2 value2

# third pass of the loop I need:
f2=field2 value3

View 15 Replies View Related

Programming :: Perl's Foreach Loop Can't Use An Array Element As The Control Variable?

Jul 22, 2011

I'm reading "OReilly Learning Perl 5th Edition", and there are such words:Code:You can use an array element like $fred[2] in every place? where you could use any other scalavariable like $fred.At the bottom of the page, it explains the ? like this:Code:The most notable exception is that the control variable of a foreach loop, which you?ll see later in this chapter, must be a simple scalar.Since Perl has the save-and-restore mechanism for the control variable, why an array element can't be used as the control variable

View 1 Replies View Related

Programming :: SH: Passing A Variable Out Of A "while - Do - Done" Loop

Jul 3, 2011

"While ; do ; done" is very convenient for SH coding. However sometimes you may be annoyed by your computed variable within the "while do done" type loop. What to do how to pass it out of the loop to the outside of the bash code? A solution is to write it into the /tmp or on the disk... and to call it back after. - not elegant... really not... Anyone would know a trick another alternative that would look nicer?

# Count file total size
LISTOFFILES=`cat "$HOME/.fvwmoscfg/fvwmburnerlist.lst"`
echo "$LISTOFFILES" | while read i ; do
SIZE=`du -bs $i | cut -f 1`
echo "$TOTAL_SIZE" > "$HOME/.fvwmoscfg/fvwmburnerlisttotalsize.lst"
TOTAL_SIZE=`cat $HOME/.fvwmoscfg/fvwmburnerlisttotalsize.lst`

echo "The total size of all files and folders is : $TOTAL_SIZE"

View 8 Replies View Related

Ubuntu :: Lost Variable Value Out Of Loop?

Jan 27, 2011

I really don't get this!! I must be doing something really foolish but i can't figure out what it is.



View 4 Replies View Related

General :: Recalculate Variable For Each Pass Of An Until Loop?

Feb 11, 2010

I am trying to write a script that will find any user on the server running more than one instance of the epiphany web browser process, then kill all their epiphany pids. (This is necessary sometimes to cure lockups, and repeat processes are the telltale sign in this case). I have tried implementing the until loop:

USER=`ps aux | grep epiphany | grep -v grep | sort | uniq -d -w 5 | awk '{print $1}'`
until [[ "$USER" == "" ]]
ps aux | grep epiphany | grep -v grep | grep $USER | awk '{print $2}' | sudo xargs kill

Unfortunately, the script only calculates the USER variable once at the beginning of the script. Fair enough, that's how it's written. My question is, how would I get the USER variable to update for each pass of the until loop so that it could cure multiple users of this ailment if necessary? That is, calculate $USER, kill their epiphany pids, repeat those two steps until the $USER variable comes up blank, then exit.

View 5 Replies View Related

Server :: Bash Mayhem With For Loop And Variable Range

Mar 1, 2011

As you can see on the output of the script, the two 'testing echoes' I do at the end don't print anything.That's the point, I do NEED this array further in my script.I'd understand my "param" var is local to the for, but is the other one too ? I tried to use "declare -a file" before the for, but i get same exact result !

View 2 Replies View Related

General :: Shell Script - Use Variable In A For Loop With Directory Path?

Jan 19, 2011

I modified files in several directories, and need to run a diff on the backup I created before modifying the file.

I'm trying to compose a simple shell script to speed up the task, but getting a syntax error.

Here is what I have:

for i in DIR1 DIR2 DIR3 DIR4 DIR5 do;
diff /maindir/subdir/subdir/$i/filename.txt.old /maindir/subdir/subdir/$i/filename.txt;

I know the paths are valid, and if I run just the diff command with the actual DIR1 instead of $i it works.

View 2 Replies View Related

Programming :: Make A New Variable With The String From The Old Variable Btut Without Any Plus Sign?

Apr 7, 2010

my script has a variable which comes in the form +00.00 +0.00 -00.00 or -0.00 (the numbers can be any in that form) for any that have a + symbol I need to remove the +, but if it has a - symbol it needs to stay.

i need to make a new variable with the string from the old variable btut without any plus sign. I have tried a lot of different ways with no success, each thing I tried either left the + or removed the entire string. I think this should work but doesn't


View 4 Replies View Related

Programming :: Search Within A Variable And Assign The Results To A New Variable?

Apr 25, 2011

how I can search within a variable and assign the results to a new variable. I'll use the following as an example -

cars="Audi BMW Cadillac Chevy Dodge Ferrari Ford Mercedes"
list=`echo ${cars} | egrep -o '<A?+|<C+'`

with the echo command I get the following output assigned to list -


What I'd like to get for output is -


how I could do this regardless of upper/lower case letters?

View 5 Replies View Related

Programming :: Assign Value Of C Variable To Shell Variable?

Apr 28, 2010

included shell script inside c program, and i wanted to assign the value of c variable to shell variable..Can any one please suggest me how to do it?

View 8 Replies View Related

General :: Written A Script In Which, Changes A Variable Using A "while" Loop?

Feb 8, 2010

i have written a script in which, i changes a variable using a "while" loop. then i feed that variable to an input file. After that i run that file in parallal using mpi library.Script is as:>>>>

sta=mpirun -np 6 $STA


View 14 Replies View Related

Programming :: Assignment To A Variable Variable?

Mar 17, 2011

This loop is part of a bash script which takes multiple arguments.

for ((i=1;i<=$number;++i)) ; do


View 3 Replies View Related

Programming :: Bash Programming - Rename Files In A Loop?

Mar 31, 2011

I need to rename the resulted searched files from a loopI have the following code:

find . -name DOC* | while read i
find $i -type f -name '*.txt'

basically, I am searching for all txt files inside any folder starting with DOC name.this code is working fine with me.I need to rename those .txt files to .txtOLDOS: Ubuntu 10.4Bash shell

View 10 Replies View Related

Programming :: Broken Do While Loop?

Feb 9, 2011

i can not figure out why my program does not terminate if i type n when i read loop unless it is the last if statement in my program ?

while [ "$loop" = y ] ; do
echo "what does baket mean: " ; read word
if [ "$word" = why ]


View 3 Replies View Related

Programming :: C++ Loop Windows ?

Oct 3, 2010

Here is my code


I just cant seem to understand loops. i havent put the loop in yet but what i would like is the user only to have 3 tries at guessing number then exit if wrong or it says "correct if right" tbh i dont even know if the above code is correct. i have tried several ways and position of the loops but each time is comes out wrong

View 7 Replies View Related

Programming :: Pass Variable In Mysql Qyery In C Programming?

Dec 4, 2010

i want to pass variable in mysql qyery in c programming

View 1 Replies View Related

General :: Shell Programming Using While Loop

Jun 28, 2010

I am trying to do some shell programs. I tried some sites regarding the while loop, they give the structure as:
while [ n1 -lt 500 ]
echo $((n1+100))

But the below code also worked for me:
while ((n1 > 500))
echo $((n1+100))
By using (( )) I could use while, for. But the documentations didnt follow this way. I mainly use this for datastructure programming.

View 4 Replies View Related

Programming :: C Language - For Loop That Just Keeps On Running?

Aug 1, 2010

In the code segment below is a for loop I am having some considerable difficulty with. It just keeps iterating endlessly and totally ignores the 70 times limit specified. I can't ever remember having this problem before and am absolutely Clueless.

for ( x = 0; x < 70; x++ )
fputs ("CSN00", target);
fprintf (target, "%d", userid);


View 14 Replies View Related

Programming :: Error Codes In A For Loop

Nov 3, 2010

I've got a 'nested' for loop which has a grep in it, if the grep fails there's no output - however the error code is still $0 and the second for loop is still entered, there's also a grep in the second for loop.I guess ultimately what i need to know is whether there's a way of making grep generate an error code. when no results are found?

View 14 Replies View Related

Programming :: Loop Through Records In A File?

Apr 23, 2011

I want to loop through the records in the below file (homedir.temp)

I want to do the following activities with each record1. du -s - to get the total usage for that directory (my variable name is SIZE)2. divide SIZE by du -c for /home to get the percentage of usage. (my variable name is PER)3. write the directory, SIZE, PER to a filePROBLEMI am using the below for loop: for record in homedir.tempthe mentioned activitiesdonehe above is not looping through the records. It does the first record perfectly and exits the loop.

View 14 Replies View Related

Programming :: Read A File With A While Loop?

Apr 25, 2010

ITT and am just lurning about scripting and need to read a file and output the file to the screen. We need to do this with a while loop.

View 5 Replies View Related

Programming :: Can't Access To Content Of Array In For Loop

Mar 7, 2010

When I deal with an array in a function I con not access to the content of array in a for loop, but out of a for loop I can access to them! for example


In a function when I send as parameter, in a for loop it prints the content of array and out of a for loop it prints the address of arr[i]

View 5 Replies View Related

Programming :: Bash For Loop - All Responses On One Line

Nov 18, 2010

I'm writing a mass snmp toner check which polls any toners available to be snmp polled, however when using a loop statement I get the results on different lines; which sounds good, however the tool I use to check with (nagios) ignores the new lines.

Is there any way I can get the output on one line? Also, I need to raise a fault if any of the toners are below a specific level (with nagios you raise faults with the exit code) - any way I can do this without exiting the loop. Code below with bits and bobs commented out.

check_ink_levels ()
for ID in $INKS


View 10 Replies View Related

Programming :: Conditional Foreach Loop In Perl?

Jun 1, 2010

#!/usr/bin/perl -w
use strict;
my @files = `ls -1`;


View 2 Replies View Related

Programming :: Fortran How To Open A File In Loop

Jun 29, 2011

I have a 50 file name NSSAVE0001.vtk to NSSAVE0050.vtk.Do I have to manually type individually command to open each file or can I have a loop to open file?

View 3 Replies View Related

Programming :: Take A List From A File And Use It In A For Loop In Python?

May 11, 2009

so far I have this

list = open('list.txt')
for x in list:

View 3 Replies View Related

Copyrights 2005-15, All rights reserved