I'm in the habit of using the following type of loop structure:
While `seq $1 $2` is not exactly a huge resource hog, I would like to learn a better way. It seems that brace expansion is a good way to go:
The problem, though, is that it so far appears that there's no way to use a variable; that is, this doesn't work:
It appears that Bash's order of operations is 1) perform the expansion, then 2) substitute variables, if relevant. In fact, that appears to be a serious roadblock-- Bash expands in such a way that the variable cannot be made to represent a range parameter. So, what's one to do? Brace expansion seems like it should be the lowest-impact method, but it apparently can't be used here. Is there a better way than the seq (or jot, depending on what's available) method?
Using seq has the same problems as anything else that preloads and jams all arguments at once into a for loop: If the list gets too long it may be truncated. It also creates an extra process and waits for it to finish, a waste of time that's not trivial if you end up doing so dozens of times a second. Brace expansion avoids the process but can still be truncated. 1:10 works, 1:10000 could easily become too large. Never use it for anything where the size isn't known in advance. i.e. exactly what you're asking for
This C-style loop doesn't need to store the entire list of numbers or create any new processes. Classic, barebones bourne shell may not have this loop, most others should. You can put variables and math expressions in it where you please.
Even classic bourne shell can do this:
Last edited by Corona688; 02-10-2010 at 04:18 PM..
We have a script that runs in ksh on HP-UX 11.11. It takes three arguments. The last argument can be a filename or wildcard character. For example:
script -s hello -t goodbye '*.d*'
In a case such as this, I would wrap single quotes around the final argument because I dont want the shell to... (4 Replies)
#!/usr/bin/bash
if
then
echo "Not valid arguments entered. Just username should be entered."
else
USER_NAME=$1
FILE_NAME=$USER_NAME.info
UNN=STUDIN\\\\$1
echo $UNN
last STUDIN\\\\$1
last UNN
If I type `last STUDIN\\eip060` it works but if I try to expand it with variable it is... (5 Replies)
I have a script that takes an option for server pools to run the script against. The option is given as a comma separated list (ie, -p 201,204,301).
I'm using eval and brace expansion to get those pool numbers into an array. It works fine unless only 1 pool number is given. Here's the code:
... (5 Replies)
There is a file as:
....... some text
timing () {
capacitance : 9.0;
incap : 0.8;
cell_fall () {
values ("8.9","7.8");
}
}
........ some more text
#######
Is there a way to directly find closing brace "{" of timing () block "{" ? (2 Replies)
i write a batch file , here is the content.
dirname='date +%Y-%m-%d'
mkdir dirname
but it doen's work, it just create a folder named date and +%Y-%m-%d.
i have tried run the command seperately in the bash prompt. after the first
sentence executed , i use $dirname to watch the value of... (4 Replies)
The objective of the code below is to create sed script to be later executed. However, it bonks because $ARCHIVENAME expands to a directory specification so the forward slashes cause problems. I can think of a few solutions that would involve redesigning the process, but I'm hoping there might be... (4 Replies)
Hi forum,
in my bash script I've many lines executing commands with redirection to log files.
...
xyz_cmd 2>&1 > $BASENAME.$LINENO
The trailing part of these lines doesn't look nice and I like to put it into a variable.
The (not working) idea is something like that
... (3 Replies)
So, I was bored on the train today, and was thinking of ways to loop through elements of an array. I came up with the following simple script, but it doesn't work as brace expansion doesn't seem to work with variables. Is there something I'm missing, or does the shell just not work like this?
... (4 Replies)
Hello,
I use a lot this command to edit a bunch of files at once
find . -name filename" | xargs -ifoo sh -c 'echo foo ; sed "s/pattern1/pattern2/" foo > ./tmp ; mv -f ./tmp foo'
I'm trying to put a function on my .bashrc file.
function loopSed()
{
local filename=$1
local... (2 Replies)