Questions tagged [for]

A tag to denote any questions relating to for loops

Filter by
Sorted by
Tagged with
4
votes
4answers
341 views

Bash for loop with string var containing spaces

In my directory I have two files with space, foo bar and another file. I also have two files without space, file1 and file2. The following script works: for f in foo\ bar another\ file; do file "$...
0
votes
2answers
53 views

continue: only meaningful in a `for', `while', or `until' loop

I have a loop that checks for certain criteria for whether or not to skip to the next iteration (A). I realized that if I invoke a function (skip) that calls continue, it is as if it is called in a ...
0
votes
1answer
25 views

For loop and locate command with regular expression mismatched

when I change any configuration I made a copy of the original file with suffix .original. Now I am composing a simple script that will find all *.original files and work with both versions, ie with ...
0
votes
1answer
33 views

why I am not getting any outputs [closed]

for items in * do if [ -f items ] then echo $items fi done Look at my directory As you can see my directory isn't empty but, when I run above code it isn't printing anything.. ┌...
0
votes
1answer
53 views

Call for a HereDoc inside a Bash Shell loop?

Regarding this post How to include python script inside a bash script , I get used to work with python HereDoc. However, I want to include that python HereDoc inside a for loop, and so, it needs to be ...
-1
votes
2answers
34 views

return substring of a dynamic ping

When I run ping example.com, it prints the result of each exchange in the console. I'd like to filter each result so that, instead of showing all the information, it shows only a subset of it. So, ...
76
votes
4answers
75k views

In bash, is it possible to use an integer variable in a brace expansion

I have the following bash script: #!/bin/bash upperlim=10 for i in {0..10} do echo $i done for i in {0..$upperlim} do echo $i done The first for loop (without the variable upperlim in the loop ...
2
votes
3answers
135 views

Weird bash behavior when IFS is set for a for loop

I have a list of values, separated by ':' and I want to process them one by one. When the delimiter is space, there are no problems: nuclear@korhal:~$ for a in 720 500 560 130; do echo $a; done 720 ...
2
votes
2answers
100 views

changing IFS temporarily before a for loop [duplicate]

I know that the SHELL allows variable assignment to take place immediately before a command, such that IFS=":" read a b c d <<< "$here_string" works... What I was ...
6
votes
1answer
1k views

How do bash loops work precisely?

for i in {0..999999999}; do echo "hi"; done takes a long time to write it's output, as if all the echos are first executed and then sent to stdout. The effect is even more pronounced with ...
190
votes
8answers
25k views

Why is looping over find's output bad practice?

This question is inspired by Why is using a shell loop to process text considered bad practice ? I see these constructs for file in `find . -type f -name ...`; do smth with ${file}; done and for ...
0
votes
1answer
38 views

for loop in FreeBSD (pfSense) doesn't work

I've just noticed that the same code for for loop in bash doesn't work in FreeBSD wolf@linux:~$ echo $SHELL /bin/bash wolf@linux:~$ wolf@linux:~$ for i in {1..3}; do echo $i; done 1 2 3 wolf@linux:~$...
-1
votes
1answer
42 views

How to specify 2 variables in a for loop in bash shell?

I have multiple image files in a folder and have created a folder for each file. Now I want to cp one file in each folder. I used for i in `seq 1 9`; do mkdir $i; done and then to copy all the files ...
2
votes
2answers
69 views

How can I edit and combine multiple matching files with one loop?

*edit: I've decided to append all the outputs to a single file. I have 40 csv files that I need to edit. 20 have matching format and the names only differ by one character e.g., docA.csv, docB.csv, ...
0
votes
4answers
714 views

unrar'ing everything in subdirectories without getting into a loop

So I've been trying to unrar multiple .rar files in lots of different folders. Some are packed a little differently and looping is causing a problem. - folder 1 - file.part01.rar - ...
12
votes
4answers
13k views

bash loop with 0.02 increment

I want to make a for loop in bash with 0.02 as increments I tried for ((i=4.00;i<5.42;i+=0.02)) do commands done but it didn't work.
0
votes
0answers
16 views

Loop through files from current directory and subdirectories [duplicate]

I'm trying to loop through each files from current directory and subdirectories. But the problem is file names with blank-space are getting treated as separate files. How do I prevent that. I want to ...
0
votes
1answer
21 views

setting number of integers for unlimited argumments

I'm not quite sure what I'm doing here but I need to be able to get an unlimited number of integers to be provided as arguments so far I have for sum in $@; do sum=$(($1 + $2 + $3)) done echo ...
0
votes
1answer
1k views

Make whiptail work in a for loop

I have a bash script that checks ping for 163 sites(stores around the country), which is this: #!/bin/bash #storestest.sh #version 0.9.2 clear; printf "Check stores procedure started; `date +"%d/%m/%...
0
votes
1answer
18 views

How to loop over subdirectories and perform action while ignoring spaces in file names [duplicate]

I have the below folder structure in Linux env: |-- Fixed_Zip | |-- ipython_notebooks | | |-- notebook editor for compute_0anXhGEj.ipynb | | |-- notebook editor for compute_aucScores.ipynb | ...
0
votes
2answers
111 views

Shell script with a for loop and an “array”

How can I use this sh script with for loop and an array. I would like to call create condition for quality gate creation of sonarqube with a for loop. Example: #!/bin/bash --login echo "Creating ...
0
votes
1answer
26 views

Creating directories inside a directory, from variable values, to redirect output from sed

I am trying to take a file, modify this file by using a value from a for loop (using sed) and redirecting it to a directory that has been created during the same for loop. Original file > Make ...
-1
votes
1answer
54 views

For loop in bash via ssh for parallel network

I have 4 CPUs in a parallel network and want to run N jobs on each of those 4 CPUs. I also want to divide N number of jobs into X jobs and run that as a loop on each of the 4 CPUs. The bash script ...
0
votes
2answers
154 views

How to sample without replacement from a script that randomly extracts 200characters using shuf?

I have this script that extracts 200 random characters from a set: #!/usr/bin/bash n=$(stat -c "%s" newfile.txt) r=$(shuf -i1-"$((n-200+1))" -n1) < newfile.txt tail -c+"$r&...
0
votes
4answers
85 views

Bash - looping through specified folders to find empty files

I want to loop through a set of folders, specified within the script and print all files that are empty. I use this script: array=("folderA" "folderX") for file in ./"${...
-2
votes
1answer
87 views

Move every 500 files in new directory [closed]

I have a directory that contains 10,665 jpeg files. I want to move 500 files to a new directory, and 500 to the next directory, etc. The largest files must be moved first: 500-1 contains the 500 ...
3
votes
1answer
140 views

What appropriate regex for recognizing a variable length file name?

I have a bash script that classifies and retrieves files from a remote server. I have trouble with the classification step, which is based on the file names. I am correctly able to identify the ...
7
votes
4answers
3k views

Get git branch from several folders/repos

I have a folder with several repositories inside. Is there any way I can run git branch or whatever git command inside each folder? $ ls project1 project2 project3 ...
4
votes
2answers
425 views

Bash script - variables in curly braces [duplicate]

The following bash script #!/bin/bash startNumber=$(( 1 )) endNumber=$(( $startNumber + 3 )) #for number in {$startNumber..$endNumber} for number in {1..4} do echo $number done exit 0 ...
-1
votes
1answer
35 views

bash: IF condition with keyworld within FOR loop

I am dealing with simple for loop, which loop the filles and execute on it some commands. I need to set up the filter with IF statement to consider only file started with the keyword "AllBoxes&...
1
vote
1answer
41 views

A variable inside an another variable

I have these variables a1=0.017 a2=0.2 a3=10.7 a4=20.9 a5=35.4 for ((x=1; x<=5; x++)) do for i in a${x} do echo "Welcome $i times" done done The output must be: "Welcome 0.017 ...
11
votes
2answers
26k views

for loop with multiline data

I need to create a script with a loop through a list of items. I want to insert a string in the script. I tried: for i in " a b c"; do echo "test "$i done But that only outputs one string: test a ...
0
votes
0answers
16 views

Taking input from user as ~/directory_name and use the input as argument in different commands which require ~ to be expanded to $HOME? [duplicate]

I have written the following script which takes the path of the "download" directory from the user and then displays the filenames one by one using a for loop . Now someone may wonder why ...
0
votes
1answer
79 views

For loop to catenate files with two variables

I am relatively new to Linux in general and I hope someone can help me. I would like to merge fastq.gz files from 4 different sequencing lanes. Each file has the following name: GC082_F4.lane1.1901.R1....
0
votes
3answers
54 views

change filename based on current filename matched with separate file content

In one directory, I have several PNGs and one text file. The PNGs are named after UPC barcodes, like 052100029962.png, 052100045535.png, etc., and I have one text file upcs.txt where each line ...
2
votes
3answers
15k views

for loop for running a command for all files in a folder

Suppose I have a folder which contains a lot of audio files. How can I write a for loop so that for each file audioname.mp3 in the folder, these commads are run: convert -size 300x200 xc:lightblue ...
0
votes
2answers
24 views

How to create a column that sums other columns using loops?

I want to create a new column in my file that sums the values of the 2nd and 3rd column for each line. For example I want 1 2 3 4 5 6 7 8 9 to give me 1 2 3 5 4 5 6 11 7 8 9 17 I've seen other ...
0
votes
0answers
23 views

Passing directory path as arguments and extract columns from files to make a new file with horizontal concatenation

I need a shell script which takes in the path of directories as arguments. The directory contains similar named files for eg. file1.dat, file2.dat etc. I need to take file1.dat from all the ...
9
votes
2answers
1k views

How to loop for 3 times in bash script when docker push fails?

I have a bash script which simply docker pushes an image: docker push $CONTAINER_IMAGE:latest I want to loop for 3 times when this fails. How should I achieve this?
1
vote
1answer
35 views

remote for loop with awk

I have a little program that gets the pids of 3 processes that are listening on different ports: Admins-Mac-mini-2:~ gitlabuser$ for pn in 23 33 43; do lsof -nP -iTCP:47${pn} | grep LISTEN | awk '{...
0
votes
1answer
106 views

Move specific files to another folder based on partial pattern in .txt file

I have a folder with various file types, but I am interested in the files with .img as extension, with the following pattern: ppi_noTD_d0_P_76con_0001.img ppi_noTD_d0_P_104con_0001.img ...
5
votes
2answers
262 views

vifm: run command on each selected file individually

I want to for loop analog for vifm. When I don't select any file I can type :!echo %f and I see output of echo with current file name as argument. When I select several files :!echo %f yields output ...
3
votes
1answer
28 views

How to create a new file based on results from multiple files and keep the filenames as first column?

I wanted to extract specific lines from multiple bam (a binary file format) files. I can select the lines from a single bam file using this command: samtools view -c TCGA-BH-A0BW-11A.sorted.bam "...
0
votes
1answer
34 views

Rename and move files from one directory to another directory

Need your help on a command that run on a path1 to rename and move files from path2 to path3 Assume path1 = /data/run/ path2 = /data/output/ path3 = /data/archive/ path 2 contains few files like 'one....
2
votes
2answers
216 views

Return “continue” from function called from loop

I'm currently refactoring a script which has slowly grown beyond control. I'm trying to spin off repetition into functions. However, I have a repeated test being called from a loop, and want it to ...
0
votes
1answer
26 views

tar command in for loop starts from the wrong number

backup/ has the following files. ubuntu@ip-172-31-8-46:~/$ ls backup/ itrpl_dsm_10_1.tif itrpl_dsm_12_3.tif itrpl_dsm_2_3.tif itrpl_dsm_4_3.tif itrpl_dsm_6_3.tif itrpl_dsm_8_3.tif itrpl_dsm_10_2....
-1
votes
2answers
56 views

How do I get the required input for this 'for' loop?

I've written a small script made to backup downloads from TOR because occasionally TOR will try to merge its place holder file (a zero byte file with original file name) and its actual download file (...
5
votes
1answer
450 views

Adding suffix to filename during for loop in bash

This has probably been asked but the existing answers don't make sense to me. I am running a loop over multiple CSV files in a folder. This folder has non-csv files also and hence I must specify the ...
0
votes
1answer
66 views

finding the highest character length in each column using a loop and awk

I am trying to create a script that goes through a document and finds the highest character length in a column and return it. This script returns 78,78,78,78 when, what im aiming for is 10,11,14,51 ...
12
votes
4answers
7k views

How do I get 0-padded numbers in {} (brace expansion)?

I'm trying to write a simple script to retrieve mem and swap usage from a list of hosts. Currently the only way I've been able to achieve this is to write 3 seperate scripts: for a in {1..9}; do echo ...

1
2 3 4 5
8