Questions tagged [for]

A tag to denote any questions relating to for loops

Filter by
Sorted by
Tagged with
0
votes
1answer
103 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
4answers
717 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
185 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
64 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
1k 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 ...
3
votes
2answers
5k 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
82 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
89 views

Default variables in Bash

The default variable for loops in Perl is $_. Is there any equivalent of this in Bash?
2
votes
1answer
470 views

Populate a CSV file from data file with nested loops in bash

I have a file, 4.txt that contains full paths to *.cfg files as well as additional data I need to strip for the final report (5.csv). For example /source/EDDG/env1/dom1/proj/config/test.cfg <...
-1
votes
1answer
632 views

sh: -c: line 1: syntax error: unexpected end of file

This command gives an error "sh: -c: line 1: syntax error: unexpected end of file" for file in /dev/DataStage/myProject/source/TEST/MyFile_*.csv; do echo "Testing" done What is wrong with the above ...
0
votes
1answer
3k views

Loop to create subdirectories in multiple directories

I want to create subdirectories into each of directories below. I used the following loop: #! bash /# dir m3z m3t m3t2 m3g m3g2 m3g3 ... n dir for d in m3z m3t m3t2 m3g m3g2 m3g3 ... n ...
0
votes
5answers
167 views

joining each line of the file with underscore

I have an input file named test.txt like this: Homo sapiens Mus musculus Rat rattus rattus I want to use a for loop here and loop through each line. Some thing like this: for i in "`cat test.txt`" ...
0
votes
2answers
135 views

Reverse-exchange file names of files in a folder from bottom up

I have a some jpg files in a certain folder: hg_test_spr.jpg hg_test00001.jpg hg_test00002.jpg hg_test00003.jpg hg_test00004.jpg hg_test00005.jpg hg_test00006.jpg . . . hg_test01200.jpg I want to ...
0
votes
1answer
302 views

ImageMagick - Incrementing file names over several for loops

This is a Bash question about renaming / incrementing file names with a series of 'for' loops. I don't understand the behavior... Working with the ImageMagick convert command If I do this with say, ...
2
votes
1answer
92 views

for loop folder list without expansion

I try to generate a command line for a backup script on bash shell. Simple example: EXCLUDES="/home/*/.cache/* /var/cache/* /var/tmp/* /var/lib/lxcfs/cgroup/*"; for FOLDER in $EXCLUDES; do printf --...
2
votes
2answers
45 views

Ensuring that subdirectories won't be affected by a loop (or by find)

Please review these codes that are aimed to do basically the same task (finding the main .htaccess of the site and changing it): for dir in "$HOME"/public_html/*.{com,co.il}/; do if pushd "$dir"; ...
2
votes
4answers
787 views

I need to create a loop in bash to join images vertically and ouput multiple joined Pages for print

I am running Ubuntu 18.04. I have a directory full of storyboard images in .jpg format as below. image-0000.jpg image-0001.jpg image-0002.jpg image-0003.jpg image-0004.jpg . . image-3898.jpg image-...
2
votes
1answer
55 views

For loop not working in a function with arguments

So I'm creating a function that does a for loop in all the files in a directory as a given argument and prints out all the files and directories: #!/bin/bash List () { for item in $1 ...
2
votes
1answer
63 views

How can I move multiple archives with different names using mv in a loop?

I have 2500 archives name LposK.dat with L ranging from 1 to 10 and K ranging from 0.00 to 49.80. I need to move those that have the same K to a folder. I manage to do this using: find . -name '*posK....
0
votes
2answers
309 views

bash: how for loop sorts files when iterating [duplicate]

What is the default behavior of for loop in terms of sorting when listing files in a directory? e.g. for sqlfile in *.sql; do mysql -u root -p pass < sqlfile; done Is this documented somewhere?
0
votes
1answer
57 views

Special content and line by line [duplicate]

commrads! I have a file: # cat file '\\.\Aktiv Co. Rutoken S 00 00\USER1@R69-20180109' '\\.\Aktiv Co. Rutoken S 00 01\USER2@R69-20180109' and i need execute line by line: for LINE in `cat file` do ...
1
vote
2answers
164 views

How to create a script to make a structure and sort files by moving them into directories?

I have a bunch of files which all have a first line being a date. All of them start with the same first 2 letters being "ff". I need to create a script which would use that first line in all files and ...
0
votes
2answers
720 views

for loop variable assignment with whitespaces

Im collecting the LPAR names and assigning to a variable on AIX , Then using multiple commands for processing. LPAR Names on the system are : ABCD56777 TSM Mobile CD CPT 2 for lparname in `...
0
votes
1answer
820 views

Multi-threading with for loop [duplicate]

This is part of a bash script code I wish to use multi-threading for DATABASE in $DATABASES ; do echo Converting $DATABASE # Check if the table is MyISAM (we don't want to convert InnoDB ...
0
votes
0answers
82 views

How to force bash star * wildcard expanding in ascending order for numerical values? [duplicate]

Please assume that we are now within a directory containing 13 files that were named as follows: p1 p2 p3 ... p11 p12 p13 Through bash, when we execute the following command: for i in p*; do echo $...
0
votes
1answer
833 views

Process one or multiple objects from a list [closed]

I wrote a script that will find objects that contain spaces in the name, and replaces each space with an underscore. The object type is based on a single object selection. How can I process all object-...
1
vote
2answers
90 views

Expression evaluates to false in for loop whereas it's true in if

This command outputs PID of process listening on port 8083: lsof -i4TCP:8083 -sTCP:LISTEN -t When there is no process, it returns empty string. No process is running on that port so I am checking if ...
0
votes
1answer
1k views

How does pushd work?

In Ubuntu 16.04 with Bash I had a problem when I didn't have a convenient way to upgrade all my WordPress components (core, translations, theme, plugin) and I used the following code to solve it: cat ...
0
votes
2answers
587 views

While read from file with sequence number

I want to write a file using echo command. The echo commend will get data from anther file. Here is the echo command: echo '{ name: $name id: $seq } I have a txt file: ...
0
votes
1answer
832 views

Print variable inside loop

1. Summary I want to print variables from loop. If I place echo $i after command from loop:     Travis CI build passed. elif I place echo $i before command from loop:     I get exit code 1. I ...
0
votes
2answers
1k views

Variable not incrementing in for loop

I am trying to print the number of directories in the working directory. For some reason my counter is not incrementing. Can you please tell me why? #!/bin/bash n=0 for afile in $(ls) do if [ -d $...
0
votes
1answer
506 views

Execute a block based on the output of a variable [closed]

Below is the script i drafted, that will work based on the SIDs it will get from ps -ef | grep pmon Once the SID is grepped, it will pass the SID to dbenv() to set the necessary parameters, and it ...
1
vote
2answers
71 views

Execute 2 or more remote scripts sharing the same curl pattern, without redundancy

I use Ubuntu 16.04 and I execute a list of remote scripts that are in the same directory (a GitHub repository): curl -s https://raw.githubusercontent.com/${user}/${repo}/master/1.sh | tr -d '\r' | ...
0
votes
2answers
81 views

AWK different result

I was searching for List of Open Files based on Ips amd Processes. I used below command for i in `sudo netstat -tulpna|awk '{print $5}'|grep -E "[0-9]{3}"|grep -v "^192\|10"|cut -f1 -d ":"`;do ...
1
vote
1answer
33 views

variable name contains a variable inside it [duplicate]

I am trying to echo some values using a for loop. hwEventType.txt contains this : 11000 11002 11004 11006 13025 13026 Code : for event in $(cat /path/hwEventType.txt) do eval miny_"$event"_2001=`...
0
votes
1answer
66 views

how to iterate installation in a row

I need a script in order to, giving a packages list as arguments, the script performs the installation of the packages in one row. For example launching the script in this way: script package1 ...
4
votes
2answers
10k views

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

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 ...
-3
votes
1answer
480 views

How to pass an argument to a Bash script variable without a for loop?

I have the following script I use to make webapp conf files (under /etc/nginx/sites-available/). This is the script: #!/bin/sh for domain; do cat <<-WEBAPPCONF > /etc/nginx/sites-...
0
votes
2answers
327 views

Bash: One argument per one undefined variable

Say I create a file ~/myScript.sh: #!/bin/sh for myVar; do echo "Hi" done If I execute bash myScript with no arguments I'll get nothing but if I'll execute it with one or more arguments I'll ...
3
votes
1answer
2k views

Bash - pause and resume a script (for-loop) with certain key

I am using the following counter functionality in a script: for ((i=1; i <= 100; i++ )); do printf "\r%s - %s" "$i" $(( 100 - i )) sleep 0.25 done is there a way I can pause and resume ...
4
votes
1answer
268 views

Why does bash replaces text from command substitution with text thereafter

I was trying to parse some nginx config λ tree sites-enabled/ sites-available/ sites-enabled/ ├── bank.cwrcoding.com.conf ├── calendar.cwrcoding.com.conf ├── cloud.cwrcoding.com.conf ├── cwrcoding....
2
votes
1answer
484 views

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 ...
1
vote
1answer
885 views

How to read multiples arguments per line for a bash command?

I have a file (ordered_names) which is in the format pub 000.html pub.19 001.html for about 300 lines, And I can't find a way to feed this to the mv command. I have read Provide strings stored in a ...
1
vote
1answer
116 views

Begin for loop every time where it ended last

I have roughly 1 million images on a directory. The files were numbered from 1 to n. I am using for loop to iterate over each image. Since each iteration is checked by individuals, only certain ...
3
votes
2answers
897 views

How can I create a Bash conditional script, based on output from a command?

I have a Java program that gets two arguments (a video file name and an image) and outputs a boolean (0 or 1) in the first line: java -jar myProgram video1.mp4 image.png > 0 >some extra ...
0
votes
1answer
464 views

while loop to check for user input not in for loop

I am listing files from a folder and taking an input from the user to select one of the files. files=$(ls ~/VideoExtract/*.avi) i=1 for j in $files do echo "$i.$j" file[i]=$j i=$(( i + 1 )) done echo ...
1
vote
2answers
729 views

Using awk to process multiple files need to count occurance of variable after pattern. How can I stop array resetting after each file?

I want to count all occurances of any variable in parenthesis after patterns, for files in a directory that have a specific extension. Each file may contain the pattern on multiple records/line. So ...
4
votes
3answers
2k views

Rename files using a WHILE loop instead of a FOR loop

Consider we have many photos with names like DSC_20170506_170809.JPEG. To rename the photos so that they follow the pattern Paris_20170506_170809.JPEG, I've wrote the following script that works ...
0
votes
1answer
625 views

How to use rename command with printf

As I was trying to fix the episode numbers of a TV series I toyed with the rename function and trying to pass the return from printf as the rename parameters, but could not do it. I tried just about ...
1
vote
2answers
18k views

using “for” loop read a file line by line continuously by considering each line as a input

Consider a file.txt file with below values one,two three,four five,six six,seven red,tree Need a shell script command to perform below activity For loop in which the loop should be continuously ...

1 2 3
4
5
8