Questions tagged [for]

A tag to denote any questions relating to for loops

Filter by
Sorted by
Tagged with
-4
votes
1answer
45 views

Writing a bash shell for loop question [closed]

trying to write a Bash shell for loop that will iterate through each line of the confidential.hex file and add each line of the hex string to the name of target domain name server, ns.example.com and ...
0
votes
2answers
39 views

using sed in a for in loop to remove words from file

I have a file 'book'(not txt) which contains text. I also have a list of words which I need to delete from said file. I have written this script but for some reason when I try to check and see if the ...
1
vote
2answers
27 views

For loop for subfolders and flat folders at once

I use this code in my scripts to process files inside a folder, but it only works for subfolders. if [ -d "$1" ]; then for file in "${1%/}/"*/*(*.mkv|*.mp4|*.avi); do I know I can just remove /* ...
0
votes
0answers
26 views

Concatenating files with delimiter in for loop

I'm loading data from the filesystem to postgres. I have 500 millions of single files, 1 json in each file. In order to be more efficient with postgres I want to load those in batches not as single ...
0
votes
1answer
38 views

Bash script that runs parallel threads slows down substantially over several hours

I have a script I came up that simply executes another process into the background that attempts to control the max number of processes running (300 in this case). It initially executes scripts at ...
0
votes
1answer
26 views

Reference items in bash for loop from find command

Suppose I have this code: for i in $(find * -type f -name "*.txt"); do # echo [element by it's index] done How do I access, if possible, an element by it's index?
0
votes
1answer
43 views

for-loop simplification

I would like to understand the for-loop below and perhaps simplify it. For example, I'd like to concatenate the rem files for each sample in the directory. Files: file1.1.fq file1.rem.1.fq file1.2....
1
vote
3answers
32 views

How to ignore file in a for loop

I'm making a script to start up a number of services at server reboot. To do this, I'm looping over the directory, checking that each has a start.sh script, and calling that script if they do. ...
0
votes
0answers
45 views

bash: problem with looping through array of directory paths

Array arr1 contains two directory paths extracted with find command: echo "${arr1[@]}" /archive/logs/2020/02 /archive/raw/2020/02 I am testing if I can loop through the array arr1 with this code: ...
0
votes
1answer
44 views

Echo variable with index

I set var inside loop, like this: eval "PB_$i=`cat btc_pub_$i.key`" where $i is the index of loop. I want to do echo or printf of $PB_$i ($PB_0,$PB_1, etc) I tried with echo $PB_${i} but ...
0
votes
1answer
34 views

How to convert my For loop into sequential execution

I wrote the following for loop (spread over multiple lines here for better readability): for F in CLEAN_READS/*_1.fa; do R=${F%_*}_2.fa; BASE=${F##*/}; SAMPLE=${BASE%_*}; metascript assembly -1 ...
0
votes
0answers
44 views

bash: opening multiple files in for loop

I want to open all the images in a small directory containing a small number of arbitrary, each image in its own window that has rotate and zoom controls. I have tried eog,eom,imagemagick,gimp,...
0
votes
2answers
40 views

For loop with grep and spaces

I seem to have a little bit of an issue with spaces when trying to use for loops and grep together. The whitespace is important because, for example, I'd like to match 'k117_19650 ', but not '...
2
votes
3answers
145 views

csplit multiple files into multiple files

folks- I'm a bit stumped, on this one. I'm trying to write a bash script that will use csplit to take multiple input files and split them according to the same pattern. (For context: I have multiple ...
0
votes
3answers
39 views

ssh multiple hosts with respective users

I am trying to loop through multiple remote servers with respective users and ssh and run some command. I tried, something like in shell script: #!/bin/bash Q_HOST=host1 host2 host3 #list of hosts ...
0
votes
4answers
99 views

Bash - move/rename numeric sorted files from input number onwards [duplicate]

I have a numbered list of files like this: 01_file_name.log 01_file_name.txt 02_file_name.log 02_file_name.txt 03_file_name.log 03_file_name.txt 04_file_name.log 04_file_name.txt 05_file_name.log ...
1
vote
1answer
45 views

How to replace Linux command with variable in for loop?

These are my sample files user@linux:~$ ls -l | cut -d ' ' -f 10- 01. old Name.txt 02. old Name.txt 03. old Name.txt test.txt user@linux:~$ This is what I want to do. user@linux:~$ for output in *...
1
vote
1answer
104 views

Changing a file with values from another file - Bash script

I am trying to write a script that takes two files as arguments and changes an .svg file with values from a .csv file. Csv file consists of lines with two values; id,colour. I need to find the id in ...
0
votes
2answers
55 views

Checking values consecutively - Bash script

I want to write a script that gets its arguments(study hour of every day) from stdin and checks if a student regularly studies. Regularly studies means that the student studies for at least 4 hours a ...
0
votes
3answers
133 views

Mathematical operations - Bash script

I am trying to write a script(script1.sh) that gives the sum of each digits in the first number, raised to the power of second number. So ./script1.sh 12345 2 should output 55 (because 1+4+9+16+...
-4
votes
3answers
62 views

How to write the following script in a single line?

#Gives a list of servers and their instance for server instance in `/swb/local/scripts/swdList -p SMARTS -a A5SV_2017.1b -e 1 --type=instance --function=SAM --priority=1 | grep -v sis` do echo "...
0
votes
2answers
31 views

awk command combining if and for

I have a csv file 20 columns. in the first part of the file is the geolocation info. What I am trying to do is to find the geolocation data for a certain record. First if identifies the user "ROSA". ...
0
votes
1answer
74 views

execute for loop on remote machine over ssh

i am trying to execute for loop on a remote machine using the below line ssh ubuntu@host -n "RPM=\`ls /var/www/html/tmp/* | for i in "${promotebuilds[@]}" ; do grep -E $i /var/www/html/tmp/*; done'...
1
vote
1answer
27 views

Loop rows and columns inside awk to count a specific substring for each cell

I have a .vcf file of 138 first header lines (starts with #) and the others of data (snp in rows(322045) and patients with some infos in columns(the first 10). I used a script bash to calculate for ...
0
votes
1answer
62 views

Attaching multiple files using bash and emailing it out using SWAKS or another program

I'm trying to: Attach multiple files into one email. Have the email sent out using a Gmail account with the current date and time in the subject header. I'm having trouble with the for a loop ...
0
votes
0answers
17 views

How to iterate more than 10 directories using “for dir in equil[0123]”? [duplicate]

for dir in equil[0123] could iterate over folders of equil0, equil1,equil2 and equil3. But if I have folders with name number more than 10, e.g. equil23, how to properly refer to them? How the ...
0
votes
1answer
46 views

Bash loop 1000x overhead over loop core

Here is a little experiment: t1=$(date +%s%N) ta=0 for i in `seq 1 1000` do t1a=$(date +%s%N) echo blabla t2a=$(date +%s%N) ((ta=ta+(t2a-t1a))) done t2=$(date +%s%N) echo diffb: $((t2-...
0
votes
2answers
54 views

Bash for loop executes multiple echo statements “out of order”

I have a bash script: for j in "$(ls -d */)"; do echo "$j" echo "$j" done and this outputs: dir1/ dir2/ dir3/ dir1/ dir2/ dir3/ What I want it to output is this: dir1/ dir1/ dir2/ dir2/ ...
0
votes
1answer
91 views

How to use bash variables in a awk command within bash for

I want to use a set of previous variables created to filter columns of a file called a.ped_snps.temp with awk within bash for loop. For this, I created bash variables: var_i_1, var_i_2, ... ...
0
votes
1answer
84 views

How to print output to look like stairs?

My code is this in my shell script file: for i in {0..3} do COLOR_Value="\033[1;3"$i"m \t Hello World" echo -e $COLOR_Value done The output is this: (Each Hello World changes color as it ...
3
votes
1answer
126 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 ...
8
votes
2answers
300 views

Bash: Multiple for loops in Background

Is this the correct way to start multiple sequential processings in the background? for i in {1..10}; do for j in {1..10}; do run_command $i $j; done & done; All j should be ...
1
vote
1answer
123 views

(Bash) For Loop Not Functioning Properly

I have a loop in a bash script, test.sh that reads as follows: #!/bin/bash CHOSEN_NQUEUE=0 foo(){ for chunk in $(seq 0 $((${CHOSEN_NQUEUE}-1))); do echo "CHUNK = $(($chunk+1))" done } ...
1
vote
2answers
63 views

Loop to run a program using multiple files from different directories

So I am trying to run a program iRep and usually it runs as- iRep -f Bins/10000A-01-01_bin.* -s sam/10000A-01-01.sam.sorted.sam --sort -o 10000A-01-01_iRep_output in sam folder - 10000A-01-01.sam....
3
votes
2answers
90 views

Is it possible to use && operator in terminal command template loop?

I wanted to make a list of similar clear commands clearer to read, so I've made a little terminal loop for what in \ cache \ thumbs \ ; do my template $what:clear; done It works great, however ...
1
vote
1answer
82 views

Show progress of a for loop after it was started

I started a for loop in an interactive bash session. For this question we can assume the loop was something like for i in dir/*; do program "$i" done > log The command takes a lot longer than ...
2
votes
3answers
85 views

Command ssh connection for 1000 linux servers

Please help me out from below situation. There are 1000 linux servers with me. i need to find out which all servers are accepting passwordless connection from my master server. Below is the command i ...
1
vote
0answers
31 views

Issue with replacing the value of a parameter based on some condition

I am working on a script where I am passing a file as a argument to it. The file has databases of two categories. One which starts with GDH_ and the others can be anything other than that. For ex: ...
1
vote
0answers
72 views

“ for vs find ”

I have alot of directories, the name of which are floating point numbers (0, 0.5 ,...,199.5,200). All of these directories contain a text file named p. I want to replace the word value with freeValue ...
7
votes
1answer
298 views

for loop not working in bash

i have the below code to replace some strigns in multiple files, but the for loop is checking for the first file and not executing the perl script. below is my code if [ -f zebu.work.post_opt/...
1
vote
2answers
29 views

looping through command to submit several jobs

I am submitting a job to a computer. It looks something like this: mpirun -np 12 example_S57 -o S57.results -r S57.final mpirun -np 12 example_S58 -o S58.results -r S58.final ... ... ... ...
1
vote
1answer
38 views

Pipe inputs from more than 1 process

I am trying to automate some make commands. Along with the target we have a variable that is passed to make (corresponding to set of a #ifdef's in code). The make command looks like this: make <...
0
votes
0answers
17 views

associative array execution order problem [duplicate]

in a bash script i'm using an associative array to create tables, tablespaces, indexes and partitions in my database. this is the script (the main script is too long with complicated queries so i ...
0
votes
2answers
217 views

Issue with `echo` in bash when looping over output of python script

Given the python script if __name__ == '__main__': print("first") print("second") print("third") the bash script #!/usr/bin/env bash declare -a choice=$( python3 test.py ) echo "You ...
-1
votes
1answer
108 views

What is correct syntax to for loop variable in PATH?

I have to execute following loop: root1=path/to/root1 root2=path/to/root2 for i in {1..2}; do ${root${i}}; done It doesn't give desired output: path/to/root1 path/to/root2 Admin suggested ...
2
votes
3answers
78 views

Shortcutting in a loop

Shortcutting: (expr1 && expr2), rather than an if ... fi statement. If expr1, expr2,... are to be generated by a loop, can you do it? Here's a toy example: $ for cond in {0,0,0}; do if [[ $...
-2
votes
3answers
81 views

bash if statement help

I am currently using 2 scripts. 1 script to filter the files by year. 2nd script into which sits inside the year subfolders to filter by month # (01-12). Is there something that would be better ...
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?
0
votes
1answer
217 views

for loop with indices [duplicate]

Let's suppose I have the following loop: for i in {1..3} do mkdir $i done Since I have many other loops in the main code and I am going to change regularly the size of the sequence, I would like ...
1
vote
1answer
629 views

bash for loop multiple number ranges

I want to run a bash numeric 'for' loop but I want to skip some numbers in between. Example: for num in {1..4, 7..11, 23..34}; do (echo num $num); done or for num in {17..24, 41..48}; do (echo num ...

1
2 3 4 5
7