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


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 :: 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 Script - Test Variable Against A Range?

Jan 28, 2010

I am fairly new to bash scripting, and I am trying to test a variable against a number range (1-3). This is what I have used so far: The user enters a value, then

while [ "$PROV" != "1" ] && [ "$PROV" != "2" ] && [ "$PROV" != "3" ] && [ $COUNTER -lt 4 ] || [ -z "$PROV" ] && [ $COUNTER -lt 4 ]; do


Is there a cleaner way to do this? Something like:

while [ "$PROV" != "1-3" ]&& [ $COUNTER -lt 4 ] || [ -z "$PROV" ] && [ $COUNTER -lt 4 ]; do

View 3 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 :: Bash Script To Ping A Range Or Own IP-range?

Apr 11, 2011

I want to build a bash script, which can ping a range IP adresses which will be filled in by the admin. If there is no IP-adress filled in, then the script must ping the subnet where the system is logged on. So if my ip is, then the script must ping from till Or else, if there is given a beginning and ending ip it must ping that!

The first part of the bash script is to ping a given range (see below). But there is one problem, how can I tell the script to ping from $begin till $end, [..] is of course wrong! But what must be filled in there???

echo "Enter beginning IP-adres:"
read begin
echo "Enter ending IP-adres:"
read end
ping -c 1 $begin [..] $end

The second part is to find my own ip and ping the whole range.. How to do that? I only can find my own IP, but I cant ping the whole range,, how to do that?

ifconfig | grep 'inet addr:' | grep -v '' |
cut -d: -f2 | awk '{ print $1 }'

View 11 Replies View Related

Server :: Get Variable From Bash From Mysql Or Pass From Php?

Apr 26, 2010

I am not parsing on a webserver so is it possible to have both

#! /usr/bin/php &

in the same script? Alternatively, I have a current bash script that I need to get some variables from mysql and not sure how to get mysql results in bash:


mysql -h -u username1 -paaa -e "USE squid; SELECT email, usern FROM TABLE WHERE blah blah;"
emailadd="resultfrom above"

View 11 Replies View Related

Programming :: Print A Range Of A Char Array Without A For Loop?

Nov 6, 2010

Program in CSay I have a char array of 1024 bytes called buf1.But I only want to print the chars in index 0 up to index 30. I know I could do this with a for loop. But is there any other way? What about maybe storing from 31-1024 to another char array say buf2 with strcpy and somehow popping 31+ out of the buf1 char array?

View 3 Replies View Related

General :: Bash - HISTSIZE - Readonly Variable -bash - HISTFILESIZE - Readonly Variable

Dec 8, 2009

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 Related

Software :: Get Variable From Text File Into Bash Variable?

Jun 10, 2009

I 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 Related

General :: Bash Variable Naming The Server - Monitor Trunk Failure?

May 30, 2010

I'm writing a script for asterisk to monitor trunk failure, i do a loop for every trunk it got nad would like to name variable like server1=, server2= naming the server upgoing as the trunk is. here is the scripts:


what i would like to do is name the variable server, username and status with the count variable, like this server$COUNT to have server1 when on trunk one, bu as soon as i add the $COUNT after the server, it seems to try to make it a command, it says that:

./ line 45: server1= command not found

View 7 Replies View Related

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 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

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

Networking :: Get The Available Bandwidth Rate From Variable Range

Nov 11, 2010

We have a DSL , how can we calculate the available bandwidth so that we can shape the packet, We can assume the bandwidth to be 100mbps on the ethernet interface
However, in DSL devices, the train rate(Bandwidth rate ) is varies according to different situation. How can i get the available bandwidth rate(from varies)?. Any method is there for getting the available bandwidth value..

View 1 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

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 :: Reading A Bash Variable In Bash Scripting ?

Nov 26, 2008

I have a config file that contains:



Now in my bash script, I want to get the output /home/user instead of $HOME once read. So far, I have managed to get the $HOME variable but I can't get it to echo the variable. All I get is the output $HOME.

Here is my parse_cmd script:


View 3 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

Fedora :: Bash: While Loop Will Not End

Jan 9, 2010

This gives a selection menu where you choose an option, and it should continue. I have several of these as part of a much larger script, but something is wrong with this while loop.

if [ $VARIABLEINPUT == "1" ] ; then
echo -e "${YELLOW}How often would you like the automatic update and maintenance to occur after the installation?:
${RED}1)${WHITE} Hourly
${RED}2)${WHITE} Daily
${RED}3)${WHITE} Weekly
${RED}4)${WHITE} Monthly

A very simple loop that uses a variable from earlier in the script to adjust variables for later in the script. For some reason, this while loop will not end - I've noted where with echos.

View 4 Replies View Related

Security :: Bash: Can't Su Within While Loop

Jul 18, 2010

I have a while loop in a file that looks like:


while IFS=":" read name script


Where I redirect the file into the loop, for some reason, I can't do an su when I redirect a file like that. I get the error, "su: must be run from a terminal." Why is this? How can I fix it?

View 7 Replies View Related

General :: Bash For Loop Not Working?

Apr 21, 2010

I have bash 4.0. I used the following for loop example, but all it outputs is "{1..10} instead of the actual numbers one through ten. why?

for a in {1..10}
echo -n "$a "

View 5 Replies View Related

General :: How To Get Bash Shell History Range

Oct 4, 2010

How can I get/filter history entries in a specific range?I have a large history file and frequently usehistory | grep somecommandNow, my memory is pretty bad and I also want to see what else I did around the time I entered the command.For now I do this:get match, say 4992 somecommand, then I do history | grep 49[0-9][0-9]this is usually good enough, but I would much rather do it more precisely, that is see commands from 4972 to 5012, that is 20 commands before and 20 after. I am wondering if there is an easier way? I suspect, a custom script is in order, but perhaps someone else has done something similar before.

View 2 Replies View Related

Ubuntu :: Bash Menu Option Loop

Mar 19, 2010

I am trying to make a bash menu that loops with options but it does not work as I want:


I want to make it read an option and do the action then return to menu.

View 2 Replies View Related

Ubuntu :: Loop Commands In Bash Scripting?

May 15, 2010

What is the difference between For and While when creating loops in bash scripting?

View 1 Replies View Related

General :: Bash Infinite Loop Haunting?

Mar 17, 2010

I want to figure out what is going on in this small script.Its really strange.I think its the infinite loop again.All I want to do is to collect some data from the zenity dialog box in an array and then echo it.Here is the code

#export PS4='+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}: ' debugging info


View 5 Replies View Related

General :: Bash Script - Stops After While Loop?

May 13, 2010

Totally new to programming (as of this morning) so please bear with me I have a bash script that stops working after the first while loop.

#part 1
doThisThatThe Otherdone
#part 2

Every arguement above the first loop completes (including the first loop itself) but anything below (incuding the second while loop) doesn't finish. I know that it isn't a problem with an unending loop... it seems to be something about the While loop that I'm not understanding... but I can't figure it out. I just need arguments below the first while loop to take place.

View 7 Replies View Related

General :: Need For Loop To Get Dirs And Run Bash Script

Jul 7, 2010

I have to format 4 years worth of awstats data "static" for a client and then move it to their new server.I don't want to run the commend to do this 48 times. If possible I would like to use a bash script that uses the folders in a directory so the script knows which year-month to do this for me and which folder to place the output in.

View 5 Replies View Related

Software :: Bash Script: Ssh Breaks While Loop?

Dec 6, 2010

I ran across some odd behaviour using ssh in a bash script. I am hoping someone can explain it to me. I have a file called /home/user/hosts.

$ cat /home/user/hosts

What I want (for this example anyway) is to run a script that checks this file and for each entry in this file it should log into that host and run a command. I have a script called


$ cat /home/user/
#!/bin/bash -


The oddity that wasted an hour of my day and the question I have for this group is, what is it about ssh that breaks the while loop? It obviously runs through once and checks the first host, just never checks any of the others. I checked exit status's and all kinds of verbose data and I could not figure it out.

View 3 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

Copyrights 2005-15, All rights reserved