Questions tagged [for]
A tag to denote any questions relating to for loops
361
questions
0
votes
0answers
18 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
778 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
329 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
84 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
93 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
450 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
1k 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 ...
0
votes
2answers
391 views
For loop to iterate over directory tree extracting results from files of the same name [closed]
I have a series of directories, all with list.txt in the same format, and I wish to put the results into a single file. I am looking to write a script that will iteratively move through each directory ...
1
vote
3answers
1k views
Progress bar to display progress based on number of files found/completed in for loop?
Is there a progress bar that can show visually completed progress based on the number of detected files found and completed in a for loop like the one below?
mkdir -p hflip; for i in *.mp4; do ffmpeg ...
0
votes
1answer
1k views
How to execute a script on several nodes using Slurm?
I have a R code that I want to execute on several nodes using Slurm, with each iteration of my paramater which goes on a node. This is my Slurm code :
#!/bin/bash
#SBATCH -o job-%A_task.out
#SBATCH -...
-2
votes
1answer
149 views
Bash Script general questions [closed]
The first part of what i am mucking around with simply creates a folder entitled command_manuals that contain text file copies of all the commands available at the bash shell, the filenames which are ...
3
votes
1answer
105 views
Iteratively adding value to column of text file
I have a 3 column text file (XYZ coordinates) and I need to iteratively add constants to the first column while appending to the end of the original file. I have tried several options but this is the ...
4
votes
1answer
2k views
Run script for 10 times or until it meets the condition
I have following shell script.
OUTPUT=$(systemctl is-active etcd)
if [[ $OUTPUT == active ]]; then
echo "The result is successfull"
else
echo "The result is unsuccessfull"
fi
I ...
1
vote
1answer
708 views
If condition on shell not working , not sure what is the mistake I am doing? [duplicate]
I have a requirement to perform specific set of tasks based on server , So I want to have the condition(s) defined based on server. Here is the script I came up with and I have read multiple blogs and ...
1
vote
2answers
872 views
For loop printing echo command only once
In this small for loop I created, I need the loop to print this message only once for all arguments.
for arg in $@
do
echo "There are $(grep "$arg" cis132Students|wc -l) classmates in this ...
4
votes
2answers
179 views
Inner for loop when run in background in bash spawns new bash process
I am executing below script
LOGDIR=~/curl_result_$(date |tr ' :' '_')
mkdir $LOGDIR
for THREADNO in $(seq 20)
do
for REQNO in $(seq 20)
do
time curl --verbose -sS http://dummy.restapiexample.com/...
0
votes
1answer
131 views
List with two values in every List Item
I want to store a list with name and email address and use it in the single for loop. And I want to do it in Shell.
Knowing that shell list has a white space separator
list = "john, john@gmail.com" "...
0
votes
1answer
576 views
Find file while other file exist in same directory
I'm trying to do "one line script" or really small bash script.
It have to find file (for example ./xxx/one.php) and if that file exist edit (with printf or echo) other file IN SAME directory (for ...
1
vote
1answer
180 views
How do I add this field?
I'm trying to print the signal strength of two wifi devices connected to a hotspot. I manage to do this script that prints the ip, hostname, mac and signal. In the script below the macs are stored in $...
1
vote
3answers
268 views
How can I grep each file in a directory for a keyword and output the keyword and the filename it was found in?
I have a directory with 2000+ text files. I am trying to make a script that:
Reads a list of IP addresses from ip.txt
Cats each file in the directory
Greps each file for the IP address
If keyword is ...
0
votes
3answers
443 views
Using sed command in for loop across multiple servers
I am trying to manipulate the same text file across multiple hosts. The command I currently have it:
for host in $(cat /etc/hosts | grep text | cut -d' ' -f 1 | sort -u); do
ssh $host \
sudo ...
1
vote
1answer
689 views
Run Rsync for multiple files in parallel from 2 different directories and find elapsed time for each file and all files
I have lots of dump files in 2 different directories that need to be uploaded using rsync, I want to run rsync for each dump file simultaneously to save time.
Dump Files:
Dir1:
- /u05/expdpdump/...
0
votes
1answer
926 views
Looping through a file with two columns and reading those two columns [duplicate]
I have a txt files with all the file names I need to analyze. I have this file (inputFile.txt) :
/path/file1a path/file1b
/path/file2a path/file2b
i have a code which needs two two input files in ...
0
votes
1answer
37 views
Bash script to link networked account directories to local account directories
I'm working on a script that will create a local user account, and link local account directories to networked account directories. The only directories I would like to be created and linked are the ...
0
votes
1answer
189 views
Understanding $() (and for-loops) in bash [duplicate]
I have a directory with git inside and git branch gives me the following output:
$ git branch
branch1
* branch2
master
The * marks the current branch. I would like to use this output in a script....
2
votes
2answers
184 views
Read from file then use for to split fields but honor quotes
This what I am trying to get in a bash shell script:
for a in "1" "2 3" "4 5 6"
do
echo "a: $a"
done
a: 1
a: 2 3
a: 4 5 6
But the quotes are treated differently when you use a variable:
echo "\"1\...
0
votes
1answer
44 views
Moving Specific Files in Specific Directory
I've some files in directory /app/me/all_off which filename like below :
access_log
alarm-batch
alarm-service-root
db_wrapper_access_log
db_wrapper_api_service
moqueue-job
mt_push_api_service
mtqueue-...
2
votes
2answers
12k views
Sum and count in for loop
I have five files each with a number in the first line.
I am attempting to write a bash script incorporating a for-loop that can calculate both the number (count) of the files and the sum of the ...
0
votes
2answers
124 views
How to pass arguments to a command from two files?
I'm trying to replicate the setup of 50 printers from one server to another server.
Command:
lpadmin -p printer_name -v printer_ip -E and some more parameters.
I have the printer names in one text ...
2
votes
3answers
3k views
Bash for loop on all files in folder except one [duplicate]
I have a folder filled with 9 Robot Framework test files. Only 8 of them have to be executed, as the last one is used to set up all the others (let's call it config.robot).
If I had to run all those ...
2
votes
4answers
14k views
Awk command inside a for loop
I try, with no success, to use an awk command inside a for loop.
I've got a variable which contains a series of strings that I want to cut with awk to get the data.
I know how to do that but what I ...
0
votes
2answers
255 views
Command substitution not working inside for loop condition
for (( a=1;a<=$(wc -l sedtest1);a++ ));do echo $a; done
Gives me an error:
-bash: ((: a<=21 sedtest1: syntax error in expression (error token is "sedtest1")
0
votes
1answer
391 views
Reading multiple files and operating on stored Arrays
I have 2 text files "${LinkP}" and "${QuestionP}. I want to read these files and store each complete line in the respective array,
IFS=$'\r\n' GLOBIGNORE='*' command eval "LinkA=($(cat "${LinkP}"))"
...
0
votes
4answers
440 views
Looping through all sub directories in a folder?
I have requirement where in I need print relative path of each folder.
Folder structure is
is there any way by using single for loop I can print absolute path.
output :-
image_script
...
0
votes
1answer
42 views
Start range from 1 onward
I am counting multiple log files with the range starting from 1 onward however it's starting from 0.
path=(/home/folder/text/*)
for 1 int ${!path}; do
printf "$s: $s\n" $i "${list[i]##*/}"...
0
votes
1answer
169 views
compare column of two files and print data accordingly
I would like to compare the 2nd column of file1- freememory with the 2nd column-usedmemory of file2. If file1(2nd column) that is "freememory" > file2 (2nd column) that is "usedmemory" then print ...
0
votes
2answers
153 views
Unix Script, problem with a 2d array
I have 2 arrays that refer to files for example
alpha=file 1
beta=file2
Charlie=file3
delta=file4
beta2=file5
beta3=file6
Charlie2=file7
charlie3=file8
delta2=file9
delta3=file10
array1=("$alpha" "$...
1
vote
1answer
2k views
I want to find if a word exists in a file
I have a directory with some files.
I want to know if a word exists in each file.
$1 is my directory that I give it as a parameter
e.g. ./myscript.sh /mnt/c/User/er/Desktop/shows
I have written ...
0
votes
1answer
215 views
How to search multiple files for a specific string one by one based on partial name
I'll try to explain my problem as best as I can, but please pardon my English ..
What I'm trying to do, seemed simple enough to me, however every solution I've found so far just won't work.
I wanna ...
2
votes
2answers
68 views
Need to iterate through subdirectories, concatenating files, with an iterative number
I'm trying to concatenate files from three different subdirectories into one file. The names of the files in each subdirectory are exactly the same. I'd like to use a loop to iterate through the ...
-1
votes
1answer
200 views
Why does this for loop ignore my variable? [duplicate]
I am trying to do a simple script which runs a few commands N number of times, determined by the user's input. However, when it comes to run the commands (in a for loop) - The variable is ignored:
...
2
votes
4answers
686 views
Force alphabetical order in for loop with if conditions
I want to create a large number of folders and do some operations in them. The folder names are based on permutations of several chemical elements which I define as variables in a for loop:
for ...
0
votes
1answer
24 views
Skip identical values in nestled for loops
I want to create directories in bash by iterating over values from multiple loops (here for simplicity just 2) while skipping identical values. A demonstrating example looks like this:
for i in 1 2 3;...
1
vote
3answers
2k views
GREP for pattern and remove all the junk characters before or after the pattern
I have a set of files in a directory. And every file will have a line called ---PUBG-xxxxx-- or ---PUBG-xxxxx, PUBG-yyyyy ----. Below is the output of the grep command.
grep "^--" FILE*.sql | grep "...
-1
votes
2answers
35 views
bash script looping two functions
I would like to order the 5th column by highest value and select the 4th if it matches the 5th column.
cat TABLE_LIST
C1 C2 C3 C4 C5 C6
3 No ...
0
votes
1answer
45 views
Why won't the for loop execute for more than 4 iterations?
In the following script, the for loop only outputs till unph4. I tried to write the value of $coils directly but doesn't work as well. I tried other iteration values to see the maximum I am getting is ...
0
votes
2answers
81 views
Excluding Bash File from modifying itself
I'm learning how to make script "fool-proof". I have some scripts that modify files in the current folder but they also modify the script itself. I know it all boils down to the "for" loop with "find" ...
2
votes
1answer
57 views
How can I sum the time based on usernames?
Basically, I want to see how much time every user spent logged in.
(username) pts/0 (IP adress) Tue Dec 12 17:51 - 18:14 (00:22) - this is how one line looks in the last command. The ...
0
votes
1answer
102 views
outputting 2 positions from same file into script and then echo
I have a file which looks like this:
Sender Bob IP 10.1.1.1
Sender Alex IP 10.1.1.2
Sender Jim IP 10.1.1.3
10 lines like this in same format.
I need to echo Name and IP.
While iterating through my ...