Skip to main content

Questions tagged [for]

A tag to denote any questions relating to for loops

Filter by
Sorted by
Tagged with
2 votes
1 answer
137 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 ...
Shakesbeer's user avatar
0 votes
1 answer
47 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 "$...
sabarish tr's user avatar
-1 votes
1 answer
55 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 &...
Rigel F. do C.'s user avatar
-3 votes
4 answers
109 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:...
André Letterer's user avatar
4 votes
2 answers
443 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 # ...
Nickotine's user avatar
  • 489
0 votes
2 answers
93 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 ...
Nickotine's user avatar
  • 489
2 votes
2 answers
757 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 ...
Kumaresh Balaji Sundararajan's user avatar
0 votes
2 answers
207 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 ...
Data Engineer's user avatar
0 votes
2 answers
423 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 ...
Stilez's user avatar
  • 1,271
0 votes
1 answer
460 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 ...
1toneboy's user avatar
  • 441
0 votes
3 answers
585 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 ...
user3637557's user avatar
0 votes
2 answers
82 views

For loop with Multiple IF condition's in Shell scripting

Firstly , In the /tmp/test file path I have the below directories amb bmb cmb By running the find command, I am getting the below list of files inside these three directories: amb/eng/canon.amb bmb/...
afrin's user avatar
  • 61
1 vote
0 answers
32 views

Do I have to write a shell script purely to access bash for loops inside of say watch? [duplicate]

I'd like to monitor apps that have inotify instance in real-time with watch using something similar to: watch "for foo in /proc/*/fd/*; do readlink -f $foo; done | grep inotify | sort | uniq -c | ...
Tomachi's user avatar
  • 111
2 votes
2 answers
466 views

For loop through a variable vector

I have a for loop and case statements. The for loop has quite a bit element list and the case statement will assign an 1D array or a vector. These values will be used in for loop after. I have the ...
BircanA's user avatar
  • 21
0 votes
3 answers
184 views

"for i in" loop with multiple vars

I have two variables VAR1="1 2 3" VAR2="Bob Tom Kate" I want to echo something like this but I don't know how to use multiple variables in a loop: 1 for Bob 2 for Tom 3 for Kate ...
trilisser's user avatar
  • 103
2 votes
3 answers
4k views

How to loop through all the files in a directory and print all the file names, like the command tree

How can I mimic the tree command and iterate through all the files and subdirectories of a directory and echo all the file names? I thought that a subdirectory within a directory is still counted as a ...
John Smith's user avatar
0 votes
2 answers
75 views

Unix. Run script across multiple dirs on specific files, where pathname has regex

I want to make a bash script (split.sh) that iterates across multiple dirs with same suffix, and then runs a function for specific files within them. I am almost there: #!/bin/bash path="/mypath/...
Lucas's user avatar
  • 99
0 votes
1 answer
32 views

Why does the test command apparently choke on a for loop variable?

I want to determine all broken symlinks within a directory. In ignorance of a better solution, I tried this simple bash one-liner (bash version is 4.2.46, which may be worth mentioning because it's a ...
andreee's user avatar
  • 193
0 votes
1 answer
145 views

looping through directories in bash skipping a particular directory name

I am trying to loop through directories within a given folder and I want to skip a particular directory name. I have written this bash script to do it, but this is giving me errors. Please tell me ...
user3138373's user avatar
  • 2,559
-4 votes
1 answer
115 views

Awk commands for each arg

I have an awk implementation in a bash script which I want to run for each function arg using a do loop. Each arg is composed of a multiline string. for arg in "$@"; do done Here is the ...
Vera's user avatar
  • 1
1 vote
1 answer
85 views

How to iterate over a directory for files without including sub-directories in Bash

I need to parse through a directory that contains files and subdirectories however I am unable to use the find command. The for loop is supposed to go through each file and compare it to another ...
IMPNick's user avatar
  • 11
0 votes
3 answers
503 views

extracting multiple tar files in folders with their corresponding file names

Hi I'm having a multiple tar files like 1.tar 2.tar 3.tar..etc and i wanted to extract them to corresponding folders i.e 1.tar to a folder named 1 and so on... my trial was like the following: for ...
Ahmed Moselhi's user avatar
0 votes
2 answers
73 views

for loop with grep not returning record by record [closed]

I have the below for loop in my shell script: #!bin/ksh touch output.dat IFS=">>>" i=0 for stm in `grep "cpdctl dsjob run-pipeline --project " input_file` do echo "...
Jerry Jose James's user avatar
0 votes
1 answer
180 views

How to exit a FOR loop on detecton of "error" stream using TAIL

I have a "for" loop which passes the results to a file called "results" Occasionally an error stream is detected and the loop continues trying within the loop for a number of times ...
Leighton 's user avatar
0 votes
1 answer
90 views

How to copy some but not all of the files in a directory based on patterns in the file name?

I have a long list of files named according to the following pattern: S901_S1_fastq.gz S902_S2_fastq.gz S903_S3_fastq.gz S904_S4_fastq.gz etc. Because these files correspond to different experimental ...
Clayton Tracey's user avatar
0 votes
0 answers
24 views

for loop with a list ... entities with spaces [duplicate]

As a intermediate step I want to produce one line per string in '' with below script #!/usr/bin/env/ bash TOMCAT_OFF=('16:00 19.01.2023' '16:00 21.02.2023' '16:00 15.02.2023') for i in ${TOMCAT_OFF[@...
vrms's user avatar
  • 139
0 votes
1 answer
77 views

Errors appending text with `sed`

I have a podman-compose file: version: "3.8" services: kiwix-serve: image: docker.io/kiwix/kiwix-serve:3.3.0-1 volumes: - kiwix_data:/data - /home/meijin3/zim/...
meijin3's user avatar
  • 139
0 votes
0 answers
82 views

Nested for loop inside an ssh -t for loop command

I've been trying to save some time by creating a loop to edit ifcfg files using the sed command. For testing I started by trying the below loop: for k in `cat *FILE CONTAINING HOSTNAME*`; do echo $k; ...
jodell's user avatar
  • 1
1 vote
1 answer
685 views

Understanding timestamps in a GNU Parallel --joblog output

Does GNU Parallel start a batch of as many jobs as possible (the number of jobs started being governed by GNU Parallel internals or/and the -j option along with given parameters), and once complete, ...
Nikos Alexandris's user avatar
0 votes
1 answer
1k views

How to create a text file for each files found in a directory?

I'm new to unix and I'm having a hard time doing this loop. I have a directory with a list of files and I'm able to list all of those files in a text file. I have a text file that contains the ...
Philippe Javier's user avatar
0 votes
2 answers
121 views

Using variables inside the for loop in shell [duplicate]

I'd like to use variables inside the for loop in shell. My current code: VAA="1st_first" VAB="2nd_second" VAC="3rd_third" for i in VAA VAB VAC; do if [[ "${i}...
Feriman's user avatar
  • 989
2 votes
1 answer
1k views

GNU Parallel with -j -N still uses one CPU

How can I get reasonable parallelisation on multi-core nodes without saturating resources? As in many other similar questions, the question is really how to learn to tweak GNU Parallel to get ...
Nikos Alexandris's user avatar
7 votes
3 answers
769 views

How can I assign the output of a command to different variables in each loop iteration?

I have a script something like this: for chain in http https ssh do iptables -nvxL $chain | tail -1 | awk '{print $2}' done But what I actually want to do is capture the output of the iptables ...
Aditya K's user avatar
  • 2,140
0 votes
1 answer
803 views

For loop in bash to input a new file in directory

For context: I am running Prokka (as a novice bioinformatician with no Prokka and little Bash experience) and I have multiple FASTA files. I have been told I need to produce a Bash loop that will ...
notknowingyet's user avatar
0 votes
2 answers
109 views

Loop over space delimited PIDs, get number of open file handles

How may I loop over a set of PIDs delimited by spaces? I think I remember the first step is like making each element its own row? For my Firefox sessions they are quite a few PIDs: $ pidof firefox ...
von spotz's user avatar
  • 435
3 votes
4 answers
2k views

execute a command recursively on the files of a folder in the matching location, not in the original one

I want to recursively convert files from .docx to .doc in a folder. The problem is that all the output files are created in the folder where I run the following command, not in the location of the ...
fich's user avatar
  • 330
0 votes
0 answers
41 views

Autofill "y" in for loop in bash [duplicate]

I'm very new to bash and I'm trying to run script in python I have. When the script runs, the first thing it prompt is y/n question, and I would like to automate this part so I can run it ...
Reut's user avatar
  • 101
4 votes
2 answers
3k views

Looping over dirs using `find . -depth 1 -type d`

I've been given a script to run, but it produces an error when calling find . -depth 1 -type d. It produces the following error, find: paths must precede expression: `1' This is the line in which it ...
wardz's user avatar
  • 143
0 votes
1 answer
311 views

Variable is always empty regardless of the loop technique I use

I have a file called dependencies and its content is: blog forms geo ticketing And I have this build.sh script in which I need to loop over them. This build script would be run inside a docker ...
Saeed Neamati's user avatar
1 vote
1 answer
971 views

How to make a script to rename images and videos with the date of modification?

I have a gallery folder with images and videos in it named in unwanted format. I want to make a script that scans through each file in that directory and when it finds an image or a video to rename it ...
userbash654's user avatar
0 votes
2 answers
247 views

How to rename numerically titled file names by 1 digit?

This feels so simple, and yet I'm entirely stumped. Needless to say I am absolutely new to this. I have a directory with files numerically numbered from 000 to 020. I would like to rename these files ...
Doodling's user avatar
0 votes
1 answer
36 views

`for in` command filename expension issue [duplicate]

I have a bash script with:- for i in *.x *.y *.z *.a *.b *.c do echo "$i" done If there are no files matching the pattern *.y then the variable i has the value *.y. When this happens, ...
user223364's user avatar
1 vote
1 answer
1k views

Need to capture exit code of pylint command using find in Github actions

I am trying to implement a python linter using pylint. But I am getting the score of each python file and also displaying the suggestion to improve the score but I am also looking to terminate the ...
sidharth vijayakumar's user avatar
1 vote
1 answer
767 views

Create directory using filenames and move the files to its respective folder

My question is a bit different than: Create directory using filenames and move the files to its repective folder Since in the same folder I have two similar copy of each file like: 001.txt and 001(1)....
Maxfield's user avatar
  • 161
1 vote
2 answers
329 views

How to make a for loop to read directory names with spaces in them?

I need to create a script to scan folders in a directory and take the name of each folder, make a .txt file, name it with the name of the folder and put inside that folder. For example: A directory ...
guest456782's user avatar
0 votes
1 answer
138 views

for loop parsing of ls and the magic behind *

I know that the following is bad: for i in `ls -1 *.MOV` ;do ... and that the proper syntax is for i in *.MOV ;do ... But what are the mechanics behind it? I mean, what part of *.MOV tells the ...
mikem's user avatar
  • 875
0 votes
1 answer
71 views

bash: different behaviour of script and in terminal (loop over files)

I'm trying to loop over files with having different searching conditions based on the folder which I used with a case statement. basically it's: #!/bin/bash case folder in "Testordner") ...
Caro's user avatar
  • 3
8 votes
5 answers
1k views

bash loop to replace middle of string after a certain character

I have 120 files (genomes.faa) that all have headers between each sequence >GENOME1_00001 HYPOTHETICAL PROTEIN A NQFTIAQSQVGLEDALLDL >GENOME1_00002 HYPOTHETICAL PROTEIN B NQFTIAQSQVGLEDALLDL &...
Goodolgab's user avatar
4 votes
2 answers
1k views

How to run a loop inside sh -c

I'm having trouble understanding what I need to escape when using sh -c. Let's say I want to run the for loop for i in {1..4}; do echo $i; done. By itself, this works fine. If I pass it to eval, I ...
Christopher's user avatar
2 votes
1 answer
5k views

How to Store the Output of a for Loop to a Variable

I have the following shell code: for value in 10 5 27 33 14 25 do echo $value done But what if I want to manipulate the output later? I want to store the entire output in one variable. Is ...
Amarakon's user avatar
  • 333

1
2 3 4 5
10