Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [for]

A tag to denote any questions relating to for loops

-1
votes
0answers
41 views

Bash script that renames log file numbered? [duplicate]

for the filename ending .log.1, rename it to .log.2 for the filename ending .log.2, rename it to .log.3 and so on For example, given the following directory: access.log error.log error.log.1 error....
1
vote
1answer
71 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
29 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 ...
0
votes
2answers
43 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 ...
-2
votes
1answer
69 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
33 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
267 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
22 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
42 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
142 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
42 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
21 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
74 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
45 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
60 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
353 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
37 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
28 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
43 views

Understanding $() (and for-loops) in bash

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
64 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
33 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-...
1
vote
2answers
699 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
27 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 ...
1
vote
3answers
276 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
2k 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
76 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
123 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
52 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
37 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
97 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
50 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
314 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
76 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
51 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
134 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
364 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
18 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;...
0
votes
3answers
213 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
32 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
39 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
50 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
37 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
47 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 ...
0
votes
3answers
185 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 - ...
1
vote
1answer
57 views

The $@ variable is not working in a for loop, trying to iterate through a users list

PROBLEM: I can't get the $@ variable to do what I want in a for loop, the loop only sends one name into the file while looping, it should loop through all the arguments and write them to the file ...
0
votes
2answers
37 views

Properly feed paths to executables in bash script

Hello all I have a text file (1.txt) that contains full paths to binaries which I need to execute a command (val_bin) for each path in the file. 1.txt Example: /source/ed/dev/ed/ab/ed_len/comercial/...
0
votes
1answer
187 views

for loop logic porting from bash to csh

I have below code snippet working in bash: for i in `ps -eaf | grep -i <pattern> | awk '{print $3}'`; do kill -9 $i; done But I have a requirement to port this code to work in csh shell due to ...
1
vote
2answers
1k views

echo list / array to xargs [duplicate]

Say I have a list/array: list=(a b c) how can I echo each element to xargs? Something like: for v in list; do echo v; done; | xargs is there a less verbose way?
0
votes
1answer
72 views

Can an array be the first argument to a function? [duplicate]

I have a function that one of the arguments is an string that has space separated words. E.g. "foo bar ccc" I think this kind of string can be treated as an "array" and loop over each word. My ...
2
votes
2answers
72 views

Default variables in Bash

The default variable for loops in Perl is $_. Is there any equivalent of this in Bash?