for

for loop parsing of ls and the magic behind *

for loop parsing of ls and the magic behind * I know that the following is bad: for i in `ls -1 *.MOV` ;do … and that the proper syntax is for i in *.MOV ;do … But what are the mechanics behind it? I mean, what part of *.MOV tells the for command that …

Total answers: 1

bash: different behaviour of script and in terminal (loop over files)

bash: different behaviour of script and in terminal (loop over files) I’m trying to loop over files with having different searching conditions based on the folder which I used with a case statement. basically it’s: #!/bin/bash case folder in "Testordner") search_filename=*_0_*.txt ;; "Ordner2") search_filename=*_65_*fg*.txt ;; *) ;; esac for files in $search_filename; do echo $files …

Total answers: 1

How to run a loop inside sh -c

How to run a loop inside sh -c I’m having trouble understanding what I need to escape when using sh -c. Let’s say I want to run the for loop for i in {1..4}; do echo $i; done. By itself, this works fine. If I pass it to eval, I need to escape the $: …

Total answers: 2

How to Store the Output of a for Loop to a Variable

How to Store the Output of a for Loop to a Variable I have the following shell code: for value in 10 5 27 33 14 25 do echo $value done But what if I want to manipulate the output later? I want to store the entire output in one variable. Is this possible? Asked …

Total answers: 1

Looping through variables which is an output of another command

Looping through variables which is an output of another command Hello I am learning Scripting here. I am trying to write a simple script using the ‘for’ loop. I have hundreds of folders in a folder called user. if i run this command i get a list of folders that i need to move to …

Total answers: 1

Bash for loop with string var containing spaces

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 "$f"; done This script also works: for f in ‘foo …

Total answers: 4

Adding suffix to filename during for loop in bash

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 csv files explicitly. I am modifying the csv …

Total answers: 1

Renaming multiple files using a loop

Renaming multiple files using a loop I need to rename some files using a loop but I can’t get it to work as I am still very new at Linux. the files that need to be renamed are: E9-GOWN33_multiplemap.bin.10.fa E9-GOWN33_multiplemap.bin.16.fa E9-GOWN33_multiplemap.bin.21.fa E9-GOWN33_multiplemap.bin.7.fa to a shorter name such as: E9.bin.10.fa E9.bin.16.fa E9.bin.21.fa E9.bin.7.fa I have used …

Total answers: 5

Vifm: run command on each selected file individually

Vifm: run command on each selected file individually I want a for loop analog for Vifm. When I don’t select any file, I can type :!echo %f and I see the output of echo with the current file name as the argument. When I select several files, :!echo %f yields output of echo with all …

Total answers: 2

Bash: Multiple for loops in Background

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 processed after each other for a given i, but all i should be processed simultaneously. Asked …

Total answers: 2

How to convert an input parameter to integer value in a for loop in bash?

How to convert an input parameter to integer value in a for loop in bash? in my bash script I try to use a number as an input variable for a for loop I run the script as ./script.sh InputFolder/ Number_of_iterations the script should work inside the given folder and run a for loop as …

Total answers: 2

Bash `sleep` outputs __bp_preexec_invoke_exec

Bash `sleep` outputs __bp_preexec_invoke_exec Background I’m running a larger one-line command. It is unexpectedly outputting (twice per iteration) the following: __bp_preexec_invoke_exec “$_” Here is the pared down command (removed other activity in loop): for i in `seq 1 3`; do sleep .1 ; done note: after i have played with this a few times it …

Total answers: 2

In what order does a bash FOR IN loop pick up files in a folder?

In what order does a bash FOR IN loop pick up files in a folder? Say I have a below for in loop: for i in /apps/textfiles/*.txt do do something done Now say I have 50 files inside /apps/textfiles/ In what order will the files be picked? Asked By: Koshur || Source Filename expansion in …

Total answers: 1

Loop through a folder and list files

Loop through a folder and list files I have a folder named ‘sample’ and it has 3 files in it. I want to write a shell script which will read these files inside the sample folder and post it to an HTTP site using curl. I have written the following for listing files inside the …

Total answers: 4

Shell variables in a for loop

Shell variables in a for loop I have a difficulty getting over this man bash passage. If the control variable in a for loop has the nameref attribute, the list of words can be a list of shell variables, and a name reference will be established for each word in the list, in turn, when …

Total answers: 1

Loop through files excluding directories

Loop through files excluding directories I need my script to do something to every file in the current directory excluding any sub-directories. For example, in the current path, there are 5 files, but 1 of them is a folder (a sub-directory). My script should activate a command given as arguments when running said script. I.e. …

Total answers: 4

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

How do I get 0-padded numbers in {} (brace expansion)? I’m trying to write a simple script to retrieve memory and swap usage from a list of hosts. Currently, the only way I’ve been able to achieve this is to write 3 separate scripts: for a in {1..9}; do echo "bvrprdsve00$a; $(ssh -q bvrprdsve00$a "echo …

Total answers: 4

Why is looping over find's output bad practice?

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 dir in $(find . -type d -name …); do …

Total answers: 8

How do I replace AND (&&) in a for loop?

How do I replace AND (&&) in a for loop? I’d like to find an equivalent of cmd 1 && cmd 2 && … && cmd 20 but with commands expressed within a for loop like for i in {1..20} do cmd $i done What would you suggest to change in the second expression to …

Total answers: 4