Questions tagged [for]
A tag to denote any questions relating to for loops
462 questions
0 votes
1 answer
58 views
`xargs sh -c` not picking up last argument (vs `for i in $(...)`) [duplicate]
I just wanted to build a simple bash script that inverted a 'hex color code' (made up of 3 pairs of 2-digit hexadecimal numbers, in the format #RRGGBB). In my first code block I attempt to perform ...
2 votes
3 answers
604 views
append iterator value to variable in for loop
I have a bash variable as such HAP="$(echo ${d} | cut -f 7 -d '/' | sed 's/[A-Z,0-9]\+//' | sed 's/_//')" that when is run as part of a script return either ref or hap. Now, I have another ...
0 votes
1 answer
127 views
How to batch compress pdfs?
I'm looking to batch compress some pdfs, using the convert to/from ps trick outlined elsewhere. I have a function defined in an .zshrc file: function pdfcompress() { for f in "$1"; do ...
0 votes
1 answer
75 views
terraform resource dependency with count(loop)
I'm trying to create stack of multiple vms on KVM_HOST with terraform. The catch is, I'm using pxeboot & kickstart to do installation. Kickstart file needs to carry some dynamic information, like ...
2 votes
1 answer
189 views
Not working break in for loop in bash script for mounting a VHD
I'm not an experienced programmer. So maybe it's obvious for you why the break command in this script does not terminate the loop, but I can't see what is causing the problem. I want to use the script ...
0 votes
1 answer
99 views
using a variable which has content separated by spaces in shell in a for loop
I am trying to read a variable with spaces on prompt and trying to use in a for loop., For example: Enter the items to read separated by space... apple orange kiwi read items for i in "$...
-1 votes
1 answer
103 views
Output of loop variable shows different value than expected
Imagine I have two folders in my current working directory: back/ and front/. I am trying to enter inside each of them and do some stuff. From the simple script below: for dir in */ ; do echo &...
-3 votes
4 answers
264 views
How to catch all lines of a repeating pattern and do some actions with the subresults
I am looking for a possibility to catch in a repeating text pattern all variable amount of lines between them and then do an action with it in bash. Example text: Total: text1 text2 Total: text3 Total:...
4 votes
2 answers
496 views
SSH for loop: parameter passed to function captured in variable is not expanded
I have several servers which have several files with deploy date info I need to parse, and get a local copy of the files which are 2 months old or older. #!/bin/bash # split on new line not space # ...
0 votes
2 answers
398 views
How to run for loop over SSH on remote so that variables expand?
If I ls files without a for loop in a remote host everything is fine however, if I capture the ls in a variable and try to echo each file it fails as the variables don't expand/have a value. What I ...
3 votes
2 answers
2k views
How to extract and delete contents of a zip archive simultaneously?
I want to download and extract a large zip archive (>180 GB) containing multiple small files of a single file format onto an SSD, but I don't have enough storage for both the zip archive and the ...
0 votes
2 answers
333 views
Pass multiple arguments from single line from a text file to teminal
I have a python program that requires 2 arguments input-file and output-file. It reads input-file and creates a copy of that file with some modifications in the output-file. I created a text file with ...
1 vote
2 answers
3k views
Is there a syntax for a "for" loop over words or lines in a variable, that will work unmofdified in both bourne shell and zsh?
I have a variable that contains a list of strings, one per line, to be looped over in a for...in...do...done command. I move regularly between bourne shell and zsh. As far as I can understand it, zsh ...
0 votes
1 answer
1k views
Recursively fix corrupted video files with ffmpeg
I think I'm pretty close to having a code which will start at a directory, then spit out a fixed copy, using fd (instead of find) and ffmpeg. I have written the if statement here as psudo-code: for i ...
0 votes
3 answers
762 views
for loop through multiple directories and multiple files
I want to check if each .bam file is accompanied with a .bai file. So if clean_xyz_1.sorted.bam is present clean_xyx_1.sorted.bam.bai should also be present. Each file has a variable string in the ...