Programming :: Variable Is Gone At The End Of While Loop?
Mar 25, 2011I have a directory file capturing script, the variable is fine with in the loop but gone after the loop is done:
Code:
DIR="/usb/sdb1/media/music/"
i=1
[code]...
I have a directory file capturing script, the variable is fine with in the loop but gone after the loop is done:
Code:
DIR="/usb/sdb1/media/music/"
i=1
[code]...
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:
Code:
Changing $files to "$files" eliminate these problems but causes that whole content of variable is treated as one string (one execution of loop).
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
do
[ -r /home/$user ] && let "$user"check=1 || let "$user"check=0
[code]...
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:
Change:
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.
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:
f1=field1value1
f2=field2 value1
#second pass of the loop I need:
f1=field1value2
f2=field2 value2
# third pass of the loop I need:
f1=field1value3
f2=field2 value3
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"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?
Code:
# Count file total size
TOTAL_SIZE=0
LISTOFFILES=`cat "$HOME/.fvwmoscfg/fvwmburnerlist.lst"`
echo "$LISTOFFILES" | while read i ; do
SIZE=`du -bs $i | cut -f 1`
TOTAL_SIZE=`expr $SIZE + $TOTAL_SIZE`
echo "$TOTAL_SIZE" > "$HOME/.fvwmoscfg/fvwmburnerlisttotalsize.lst"
done
TOTAL_SIZE=`cat $HOME/.fvwmoscfg/fvwmburnerlisttotalsize.lst`
echo "The total size of all files and folders is : $TOTAL_SIZE"
I really don't get this!! I must be doing something really foolish but i can't figure out what it is.
Code:
#!/bin/bash
pro_num=0
[code]...
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:
Code:
USER=`ps aux | grep epiphany | grep -v grep | sort | uniq -d -w 5 | awk '{print $1}'`
until [[ "$USER" == "" ]]
do
ps aux | grep epiphany | grep -v grep | grep $USER | awk '{print $2}' | sudo xargs kill
done
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.
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 RelatedI 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;
done
I know the paths are valid, and if I run just the diff command with the actual DIR1 instead of $i it works.
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
foo=+12.40
bar=${foo#+}
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 -
A
C
C
What I'd like to get for output is -
Audi
Cadillac
Chevy
how I could do this regardless of upper/lower case letters?
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 Relatedi 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:>>>>
#!/bin/bash
STA=/home/sk/Desktop/exefile
sta=mpirun -np 6 $STA
[code]....
This loop is part of a bash script which takes multiple arguments.
Code:
for ((i=1;i<=$number;++i)) ; do
offset=$(($i+5))
[code]...
I need to rename the resulted searched files from a loopI have the following code:
find . -name DOC* | while read i
do
find $i -type f -name '*.txt'
done
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
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 ]
[code]....
Here is my code
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
i want to pass variable in mysql qyery in c programming
View 1 Replies View RelatedI am trying to do some shell programs. I tried some sites regarding the while loop, they give the structure as:
Code:
while [ n1 -lt 500 ]
do
echo $((n1+100))
done
But the below code also worked for me:
Code:
while ((n1 > 500))
do
echo $((n1+100))
done
By using (( )) I could use while, for. But the documentations didnt follow this way. I mainly use this for datastructure programming.
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.
Code:
for ( x = 0; x < 70; x++ )
{
fputs ("CSN00", target);
fprintf (target, "%d", userid);
[Code].....
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 RelatedI want to loop through the records in the below file (homedir.temp)
/home/user1
/home/user2
/home/user3
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.
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 RelatedWhen 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
[code]...
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]
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.
Code:
check_ink_levels ()
{
for ID in $INKS
do
[Code].....
#!/usr/bin/perl -w
use strict;
my @files = `ls -1`;
[code]...
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 Relatedso far I have this
list = open('list.txt')
for x in list:
list.read(x)