Questions tagged [for]
A tag to denote any questions relating to for loops
407
questions
1
vote
1
answer
38
views
Delete folders from a directory once it's sbatch`ed in a for loop?
I am running a command using sbatch for a tool on the server, that takes a lot of space. The command uses some directories (some files) from a main directory(tq_first) in a for loop. I was wondering ...
1
vote
1
answer
41
views
cannot figure out why the file will not copy
I am trying to write a program which i can reuse for copying content into multiple directories. But for the life of it i cannot figure out why the program does not work and is throwing this error.
I ...
0
votes
1
answer
63
views
Grep inserting file names from working directory
I have script to do a few jobs:
list config XMLs.
list which of those XMLs have a match.
show what that match is.
JENKINS_HOME=/var/lib/jenkins
_ALL_JOBS=$( find $JENKINS_HOME/jobs -name config.xml ...
-1
votes
0
answers
31
views
loop through a list of files and copy them to their folders
I have 10 jvm application logs in gzip format with incrementing number from 1-9. I want to copy them to 9 different directories so I can untar them in their respective folders.
What command or script ...
0
votes
0
answers
10
views
How to use a variable in a if then timer loop [duplicate]
I want to have a timer count down to zero from a value determined by variable setTime in my bash script. However, when I define setTime and type it into my if then loop in the timer, the script doesn'...
2
votes
3
answers
45
views
How to pass argument to a for loop from a file and run in parallel, and create log for each argument
for test in "${a[@]}"
do
sh ansiblescript.sh -s $a
if [[ $? -eq 0 ]]; then
echo "Success"
else
echo "Failed"
exit 1
fi
done
...
0
votes
1
answer
49
views
how to stop while looping for bash
target=${1:-http://web1.com}
while true
do
for i in $(seq 10)
do
curl $target > /dev/null &
done
wait
done
I am just a beginner at writing in programming especially bash.
I ...
1
vote
2
answers
68
views
Use parent directory base name for making a new directories in a for loop
I am attempting to use a for loop to move files (e.g. .txt) into a new directory, where the original directory name as part of the name, but add an extra "with-new-files" onto the name so I ...
0
votes
2
answers
26
views
Edit a particular string in a file based on another file
Hello I have a file called users. In that file i have a list of users for example
user1
user2
user3
Now i have another file called searches where there is a specific string called owner = user for ...
0
votes
1
answer
26
views
how to remove/rename an extension from a folder
I have a few folders like name1@domain.com, name2@domain.com, name3@domain.com. i want to rename them so that it will be name1, name2, name3 etc. i figured out that if they are vise versa i.e name1, ...
1
vote
2
answers
38
views
Echo contents of for loop automatically
I often use a for loop to i.e. convert a bunch of file formats. In some cases, where text transformations or variables occur, it would be nice to check if the substitutions were performed correctly.
...
0
votes
1
answer
53
views
Trying to loop values into curl command from a file
I am not even sure if this is possible or not. I am trying to create a loop where i can curl with different values from a file.
for i in `cat id`;
do curl -X POST 'https:/myurl \
--header 'X-XX-...
2
votes
2
answers
55
views
sorting files using string
I have a directory of 1000+ files (follow the same formatting) example file would be
giroof-doas-ob10roof-L1-202007020832-10.std
Where year is 2020
month is 07
day is 02
hour is 08
min is 32
...
3
votes
1
answer
47
views
How to get the output file format(with defined spaces) as same as input file format when passing through loops [closed]
I need to get the output file same as the input file format after passing through the while loop.
eg:
cat samp.csv
a b c d
cat samp.csv | while IFS= read -r line
do
echo $...
0
votes
1
answer
30
views
How do i create a for loop to create 225 new users with 15 first names and 15 lastnames?
I need to create 225 new users with add user but i have only 15 first names and 15 last names. I also need to create an automatic password for each user. Please help.
0
votes
1
answer
40
views
Script to find x265 files then output name
I'm trying to make a script that crawls through a giving directory and list all files that are x265. Using mediainfo and can see if the file is x264 or x265, I then use sed to clean up the text. The ...
2
votes
2
answers
222
views
Script which will rename all files in specified by path directory
I have to make a script on Ubuntu, which will rename all of files in specified by path directory to be uppercase. I've already found a loop which will rename files, but I'm not sure how I can pass a ...
1
vote
1
answer
54
views
Here-document with two variables
Trying two variables with a here-document to generate a list of bash scripts. I ran into the problem that I cannot put the two variables into the script properly and the output file (only one file) ...
1
vote
2
answers
54
views
Iterate lines on csv to save columns as variables
I have a csv composed as follows:
Column1,Column2,Column3
A Existing text in Column1, A Date in Column2, A Integer in Column3
B Existing text in Column1, B Date in Column2, B Integer in Column3
C ...
0
votes
0
answers
99
views
Bash - Can I run a for loop in a one liner? [duplicate]
In PowerShell I can pipe output into a for-each loop like so:
C:> ls C:\ | % { $_.Length / 1024 }
Note that ls is an alias for Get-ChildItem and % is an alias for ForEach-Object.
Is there a ...
1
vote
2
answers
49
views
bash need to stop for loop adding in new line
I've been working on this two days now and I need some input.
I'm creating a universal script to check any drive on any devices I have.The only part that is tripping me up that I have not been able to ...
0
votes
2
answers
96
views
How can I do something for each line of a file using bash loop?
ckim@ckim-ubuntu:~/test$ cat tt
#define a b
#define c d
ckim@ckim-ubuntu:~/test$ for i in `cat tt`; do echo $i; done
#define
a
b
#define
c
d
The $i variable is taking every single word seperated by ...
0
votes
2
answers
77
views
For-loop - appending to arrays with iterator in the array name
I have the following problem. I have an array arr with some values. I want to sort each value into a set of different - and already declared - arrays earr$j, i.e. arr[0] into earr1, arr[1] into earr2 ...
2
votes
0
answers
68
views
for loop and appending over list of arrays
I am new to Bash scripting and presently find myself dealing with a small problem in working with for loops, arrays and variable assignment/substitution which I do not know how to solve. Since I am ...
0
votes
1
answer
145
views
Run for loop for multiple folders
I have been working with multiple folders, each folder have distinct name and each of them have multiple CSV files inside, i have a bash script that for each csv file inside the folder, makes a ...
0
votes
2
answers
40
views
Why am I getting unexpected output while trying to loop for the number of times entered by the user? [duplicate]
I am writing this bash shell script:
#!/bin/bash
declare -i N
read N
for i in {1..$N}
do
echo "Number: $i"
done
(I believe declare -i N makes the N an integer)
However on running this, I ...
1
vote
2
answers
448
views
loop through one file extension or the other in bash or zsh
This is syntactically correct:
for f in *bw;do echo $f;done
But How would I add an extension to loop through one or the other?
The following doesn't work:
for f in *bw|*txt;do echo $f;done
And this ...
0
votes
1
answer
126
views
awk command inside bash shell script loop
I have a bash script which is supposed to go through a series of files text file. I have written a for loop to do this job automatically for me but I am not getting any output files when the script ...
0
votes
1
answer
38
views
Add values to an array and loop based on that
I have 3 files like below:
CV_REF_DATA_09012021.txt
DB_ONLINE_CL_09012021.txt
DFR_CL_INS_09012021.txt
I want to check if the files are present in /NAS/CFG/ and then add it to an array.
It's not ...
-1
votes
2
answers
66
views
Loop a part of code for multiple variables
I am looking for an approach to build a script.
I have 3 files with the below pattern:
DailyLoad_Cust.txt
DailyLoad_Credit.txt
DailyLoad_Account.txt
Each of these files has an associated sub_system.
...
1
vote
1
answer
58
views
Pipe for loop with awk and if
I'm trying to create a cron job that checks the status of certain worker machines and triggers a webhook:
It works, but I'm not sure that this is the best approach:
for i in $(oc get nodes | awk 'FNR&...
-1
votes
1
answer
194
views
BASH syntax error near unexpected token 'done'
What is wrong here? I am getting error near `done'.
echo " Writing a program to print even numbers by adding 1 if the number is odd."
for i in {1..10}
do
d=$(($i % 2))
if [[$d = 1]]
...
3
votes
1
answer
366
views
Bash for loop, using grep
I have a folder full of zipped files, I would like to have these files piped to a second bash script containing commands for programs to be run on them using a for loop. So far I have:
targ_path='/...
1
vote
1
answer
36
views
Background for-loop only cycles once
I have the following setup:
x=0
y=1
z=2
task0(){
for file in "${files_array[$x]}"
do
sed -i "$var" $file
x=$((x+3))
done
}
task1(){
for file in "${...
3
votes
3
answers
659
views
I don't really understand expansion and quoting on the shell (zsh/bash)
I've read up on a few answers here in reference to quoting variables on the shell.
Basically, here's what I'm running:
for f in "$(tmsu files)"; do echo "${f}"; done
this has the ...
0
votes
1
answer
50
views
bash - Script to output domains MX-es using arguments
I am trying to make a bash script that shows me the domains MX-es of one or more domains given as arguments when running it. Example script call:
./domains.sh domain1.com domain-no-mx.com domain2.net ...
-1
votes
1
answer
194
views
Loop through MAC addresses, how to handle both numbers (0-9) and letters (a-f) in a "for" loop
I've a script with this code
for i in {2..9}
do
grep "Node${i}\|01, source address = 00:00:00:00:00:0${i}" t1.txt > t2.txt
done
Is it possible to expand the loop ...
0
votes
0
answers
31
views
bash script, accessing pre-defined variables using for loop substitution
Heres the bash script:
ALL="2 3"
VC2="https://someaddress.acme.com"
VC3="https://someotheraddress.acme.com"
hosts2='12
13
14'
hosts3='15
16
17'
for i in $ALL
do
...
1
vote
2
answers
33
views
Converting files in multiple directories from tab separated to comma separated
I am running Mac OS.
I have a directory /Users/sethparker/Documents containing several subdirectories /Users/sethparker/Documents/dir1,/Users/sethparker/Documents/dir2,/Users/sethparker/Documents/dir3....
0
votes
0
answers
31
views
How can I loop through a file and output its contents to a file in a new directory without the same extension?
Quite a basic question but I am new to this.
Example: Within my current directory, I have the folder looptest containing file1.txt, file2.txt, file3.txt
I wish to output their contents to the folder ...
2
votes
3
answers
570
views
How to read variable number of variables in bash
I want to read a number of strings from command line and read all strings in a loo. How to process (like Print) them in a loop, further. Sorry I am a newbie, please excuse my novice question.
#!/usr/...
0
votes
1
answer
53
views
looping through multiple files with bash command
I have a file with listed sample names
head sample_id.txt
PD26405a--PD26405b
PD26414a--PD26414d
PD26417a--PD26417b
...
I also have a directory with the outputs of preprocessing step of the program ...
3
votes
1
answer
981
views
In for loops in bash, is the counter variable local or global?
in the idiom
for i in $directories; do
# ...
done
... is the variable $i local or global?
And what if there happens to be a global variable of the same name. Does bash work with the global variable ...
2
votes
2
answers
54
views
How to remove four random characters before the .extension from various files using a for loop?
I am working on an undergraduate research project that is heavy in bioinformatics, and I am going down a pipeline of file processing. Some background: I am working with shotgun metagenomic data which ...
0
votes
2
answers
442
views
How to watch a for-loop in bash?
Is it possible to use a for-loop with the watch command?
I'm not really sure what to make of this error with what I've tried:
$ for i in 1 2 3; do echo $i; done
1
2
3
$ watch -n 10 for i in 1 2 3; do ...
0
votes
1
answer
122
views
Displaying number of columns and items in columns using a for loop
I'm writing a BASH script that asks a user for the number of columns and the number of items to put in the columns. The script will then put the numbers in the columns using for loops.
# Number of ...
1
vote
1
answer
127
views
Use For loop to extract certain columns from a series of files to write new tab-delimited files
I have a series of files located in a series of folder, for example:
~/BR2_1-3/bin.1.permissive.tsv
~/BR2_1-3/bin.2.permissive.tsv
~/BR2_1-3/bin.3.orig.tsv
~/BR2_2-4/bin.1.strict.tsv
~/BR2_2-4/bin.2....
0
votes
1
answer
482
views
Rename multiple files in multiple subdirectories using for loop
I am trying to rename files with the same names in different subdirectories using for loop.They should be renamed according to the name of subdirectory.
Subdirectory1/File.txt
Subdirectory2/File.txt
...
0
votes
2
answers
116
views
For loop - Bash isn't reading my variables
I am trying to make a little bash script, that calls a command on each of my files in have in a file, found with the find command.
I want to be able to keep track of where the script stopped (it tends ...
2
votes
1
answer
27
views
zip outputs in the wrong place when used in a loop
I have many directories and I want to zip them all.
$ mkdir -p one two three
$ touch one/one.txt two/two.txt three/three.txt
$ ls -F
one/ three/ two/
I use zip and it works as intended:
$ zip -r ...