Ubuntu :: Lost Variable Value Out Of Loop?
Jan 27, 2011I 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 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'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 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 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 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.
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.
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.
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 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]....
I have installed FC13 on my laptop and set it up as a development server. Here is my issue when passing variable from one page to the next it gets lost. My PHP includes work DB connect string works from the include.
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 have a mytext file with month and year as two separate fields. likemytext fil
08 2010
09 2010
10 2010
........
........
........
I want to read the values of each field i.e., month and year into an awk script.
how to assign a local variable value to a global variable....
View 2 Replies View Relatedmy 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#+}
I am supposed to create an environment variable with the PRINTER variable, which should resolve to the word sales. Would the command be like this?: env PRINTER - NAME=SALES (is this the command to create that variable with resolving the word sales to it?)
View 3 Replies View RelatedI have a text file i that has a mailTo: NAME in it. In a bash script i need to extract NAME and put it in a $variable to use. How do i do this?
View 2 Replies View Relatedhow 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 Relatedcan i use the value of one variable to generate a name for another variable? for example i want to use the counter from a "do while" loop to name and define a variable each time the loop executes. for example
objectnames1=`ls -a`
objectnames2=`ls -a`
etc.
i don't have a script yet but each time through the loop i intend to cd to a particular directory and then define a variable containing a list of each object in that directory as values. for the rest of the script to work, each variable generated has to be unique, and i can't think of a good way to accomplish this.
if using a value from one variable to name another isn't possible, can anyone think of a more elegant solution? i know limited syntax but i'm willing to read up...
I am trying to alter the character position of residue numbers above 999 in a pdb file.The following script is an attempt to:1) Get all unique pdb residue numbers (in column 5) using awk and assign it to a variable i.2) Loop through all the values in $i and if it is greater than 999, shift that number one character to the right using sed.However, the script only manages to alter the final residue numberCould anyone please advise how I can loop through all values in $i and shift it one character to the right?
#!/bin/bash
# Script to alter position of residue number in pdb file for resid above 999
i=$(awk '{print $5}' wt-test.pdb | uniq)
[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]...
On one of my servers I see this when I log in. What does this mean and how can I get it to go away? Everything seems to work fine, but none of my other machines give this error.
View 5 Replies View RelatedI have a script that changes my background to a real-time satellite shot of the earth.
Heres the script code...
For some reason, it will not repeat after the set 300 seconds (5 minutes).
How do I get it to loop properly?
I have received the ZaReason Teo netbook I have bought, all specs are here:I have had it for 3 days now and haven't really played around with it very much: I have installed a few apps (skype, synergy, dropbox, texlive) and I've been basically getting acquainted, and I'm generally satisfied. A couple of hours ago I came back from the office, turned on the computer again and.. gnome started to flicker in an impossible loop! In one second, it will go completely white, then I'll see the purple Ubuntu background, then the application bar on the top and then white again. I see for a tenth of a second two suspicious arrows in the top left corner.
I have taken pics and made a sequence of the loop to help you guys understand:this happens immediately after login. when it loads, as in when you get "Ubuntu" and the 5 bullet points below, it doesn't flicker.While the screen loops in the sequence, i can open a terminal (using Gnome DO) and do stuff. I have tried to stop GDM, using "sudo /etc/init.d/gdm stop"What I then get is a command line interface where I can type something, but if I press ENTER nothing happens, it just goes to the next line. To restart the thing I need to press the power button off and on again. Here's the image of where I get. When I restart, it starts to loop again. I don't know what to do. While it loops I can enter commands in a terminal (thanks Gnome DO). I removed synergy but it didn't help either. As per now, it's really difficult and slow to interact with the machine, this really make it useless for anything I'd like to do with it.
I am using Kubuntu. When I came back from the States -- where I stay for long while -- I had to update the system to Ubuntu 10.04 LTS. So I did.
I have now a loop problem for the login process.
On a terminal I can read such a message about BASH :
So I was wondering is it possible to loop a script until i hit the Esc key? I have seen the do or while true thing. here is the script
Code:
#!/bin/bash
sleep 5 &&
[code]....
what does desktop manager do? What does $ do? what is loop for in command line? What is the best way to switch user? Using command line?
View 7 Replies View RelatedI just upgraded to 10.04 Netbook Remix.When I boot, it gets past the splash screen and to the desktop. Before it can load anything, it acts as if it is looping. The wallpaper loads, the screen blinks white and then it loads the wallpaper again... then a white screen, then the wallpaper.I've tried to bring up the terminal and can't do that. I've also rebooted in recovery mode which didn't help.I'm at a loss. I'm on a windoze machine now since I can't get on my netbook.
View 1 Replies View Related