At times I find that I need to loop through a file where each value that I need to do something with is not on a separate line, but rather separated with a ":" or a ";". In this instance, I create a loop within which I define 'IFS' to be something other than a whitespace character. In this example, I iterate through a file which only has one line, and several fields separated with ":". The counter helps me define how many times I want to repeat the loop.
Without extra css, just run it
The effect is achieved by moving odd-numbered lines from right to left and even-numbered lines from left to right. For odd-numbered lines (with an index j), the ((j + i) % 2 == 0) condition is satisfied. In this case, the line width is set to i, resulting in the line moving from left to right. For even-numbered lines, the ((j + i) % 2 == 0) condition is not satisfied. The line width is set to $(tput cols) - i, causing the line to move from right to left. This alternating direction of movement creates a twisted visual effect as the lines appear to move in opposite directions. The code runs in a continuous loop, repeatedly updating the lines with changing background colors. There is a slight pause of 0.05 seconds between each iteration to control the speed of the animation.
Usually the MS-DOS cmd.exe processes in the whole FOR loop as one command and expands each var like %varname% in before (except the loop var of course).
This command enables expansion of other vars than only the loop var during the FOR loop. The syntax of the var to expand is then !varname! inside the FOR loop.
Use command
endlocal
to end the setlocal command.
E.g. (only works from batch files, not from commandline directly):
@echo off
setlocal enabledelayedexpansion
FOR %%A IN (*) DO (
set file=%%A
echo !file!
)
endlocal
iterating range of numer with for loop in shell or bash Show Sample Output
"seq" has an additional parameter to use as INCREMENT. # seq FIRST INCREMENT LAST https://linux.die.net/man/1/seq Show Sample Output
Loops over array of a system var, splits its values and puts the values into %A, %B, %C, %D, and so on.
Create array before, like
set ARRAY[0]=test1,100
and
set ARRAY[1]=test2,200
Be sure to replace %A, %B, etc. with %%A, %%B, etc. when using this from inside of batch files.
Show Sample Output
This command loops over all indexes of the system variable array ARRAY[] and puts its content into %A.
Create this array before, e.g. by
set ARRAY[0]=test1
and
set ARRAY[1]=test2
For using inside of a batch file, write %%A instead of %A.
Show Sample Output
You can use this to loop any command to periodically see the output.
while true; do [YOUR COMMAND HERE]; sleep [# of seconds]; done;
Alternatively, you can put it in a simple shell script to save typing!
#/!bin/bash
while true; do $1; sleep $2; done;
/path/to/script.sh "ifconfig eth0 | grep 'inet addr:'" 60
Show Sample Output
commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.
Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.
» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10
Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):
Subscribe to the feed for: